繁体   English   中英

QML - Q_INVOKABLE函数

[英]QML - Q_INVOKABLE functions

我有QML的问题,调用Q_INVOKABLE函数。 虽然我将功能标记为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*/

我究竟做错了什么?

根据你在这里粘贴的内容来判断,粗略的检查表明你已经成功地做了我们所做的一切:

您忘记了基于QObject的类中的Q_OBJECT宏。

如果没有那个,你就不会得到为你的类生成的元对象,因此信号,插槽和其他类似功能(如Q_INVOKABLE)将无法按预期运行。 希望有帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM