QML - Q_INVOKABLE-Funktionen

QML - Q_INVOKABLE-Funktionen


Ich habe ein Problem mit QML beim Aufrufen von Q_INVOKABLE-Funktionen. Obwohl ich Funktionen als Q_INVOKABLE markiert habe, erhalte ich Fehler


TypeError: Result of expression 'azdownloader.setData' is not a function
TypeError: Result of expression 'azdownloader.perform' is not a function

Ich habe diese Klasse:


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*/

In 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();
}

in 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
}
}

Und in den Seiten


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*/
}

Der andere:


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*/

Was mache ich falsch?


Antworten:


Nach dem zu urteilen, was Sie hier eingefügt haben, zeigt eine flüchtige Inspektion, dass Sie es geschafft haben, das zu tun, womit wir alle beginnen:


Sie haben das Q_OBJECT-Makro in Ihrer QObject-basierten Klasse vergessen.


Ohne dieses erhalten Sie kein für Ihre Klasse generiertes Metaobjekt, und daher funktionieren Signale, Slots und andere ähnliche Funktionen (wie Q_INVOKABLE) nicht wie erwartet. Hoffe das hilft :)