4
我有QML的問題,調用Q_INVOKABLE函數。雖然我標誌着作爲Q_INVOKABLE遇到錯誤QML - Q_INVOKABLE函數
TypeError: Result of expression 'azdownloader.setData' is not a function
TypeError: Result of expression 'azdownloader.perform' is not a function
我有這個類:
typedef QString lyricsDownloaderString;
class lyricsDownloader : public QObject
{
public:
Q_INVOKABLE virtual short perform() = 0;
Q_INVOKABLE inline void setData(const string & a, const string & t); // set artist and track
// some other data
protected:
lyricsDownloader(const string & a, const string & t) : artist(a), track(t) {}
/*other data*/
};
class AZLyricsDownloader : public lyricsDownloader
{
public:
AZLyricsDownloader() : lyricsDownloader("", "") {}
AZLyricsDownloader(const string & a, const string & t) : lyricsDownloader(a, t) {}
Q_INVOKABLE short perform();
Q_INVOKABLE inline void setData(const string & a, const string & t);// set artist and track
/*other data*/
在main.cpp中
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QApplication app(argc, argv);
mainWindow viewer;
qmlRegisterUncreatableType<lyricsDownloader>("MaeLyrica", 1, 0, "lyricsDownloader", "");
qmlRegisterType<AZLyricsDownloader>("MaeLyrica", 1, 0, "AZLyricsDownloader");
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/maelyrica/main.qml"));
viewer.showFullScreen();
return app.exec();
}
在main.qml
import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.0
import MaeLyrica 1.0
//property color fontcolor: "white"
PageStackWindow
{
id: pagestackwindow
visible: true
MainPage
{
id: mainview
}
initialPage: mainview
AZLyricsDownloader
{
id: azdownloader
}
}
並在
import QtQuick 1.1
import com.nokia.meego 1.0
Page
{
/*some gui elements*/
Button
{
id: go
text: "Go!"
width: parent.width
onClicked:
{
goLoader.source = "ShowLyricsPage.qml"
pageStack.push(goLoader.item)
azdownloader.perform()
showLyricsPage.busyind.visible = false
}
}
}
/*dialogs and toolbar definitions*/
}
另一種:
import QtQuick 1.1
import com.nokia.meego 1.0
Sheet {
id: sheet
acceptButtonText: "Save"
rejectButtonText: "Cancel"
onAccepted:
{
if (artistfield.text == "" || trackfield.text == "") // check whether empty
{
emptyfieldsdialog.open()
}
else
{
selecttrack.text = artistfield.text + " - " + trackfield.text
azdownloader.setData(artistfield.text, trackfield.text)
}
}
content: Rectangle { /*some content here*/ }
/*dialog definition*/
我在做什麼錯?
現在我得到moc_lyricsDownloader.cpp:76:錯誤:未定義引用到'lyricsDownloader :: setData(std :: basic_string,std :: allocator > const& ,std :: basic_string ,std :: allocator > const&)' –
marmistrz
2012-07-18 13:50:27
編輯:將函數體添加到類定義後一切正常。 – marmistrz 2012-07-18 14:25:15
我有同樣的問題,併爲我解決它:-)謝謝! – 2013-10-31 10:54:40