gpt4 book ai didi

c++ - QML - Q_INVOKABLE 函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:23 26 4
gpt4 key购买 nike

我在调用 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*/

我做错了什么?

最佳答案

根据您在此处粘贴的内容判断,粗略的检查表明您已经成功完成了我们一开始所做的事情:

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

没有它,您将无法获得为您的类生成的元对象,因此信号、槽和其他类似功能(例如 Q_INVOKABLE)将无法按预期运行。希望有所帮助:)

关于c++ - QML - Q_INVOKABLE 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11538404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com