[英]Signal and Slot between QML and c++
我的问题是,我无法在cpp和qml文件之间获得信号和插槽连接。 首先,我在网络上找到了一些解决方案,但这是行不通的。 我敢肯定,这个错误是我的,但是我没有找到它。
main.cpp中:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQmlContext>
#include "Hotfolder.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QScopedPointer<cReadJson> jsonReader(new cReadJson);
QScopedPointer<cHotfolder> hotfolder(new cHotfolder);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.rootContext()->setContextProperty("jsonReader", jsonReader.data());
engine.rootContext()->setContextProperty("hotfolder", hotfolder.data());
QObject *topLevel = engine.rootObjects().at(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
QObject::connect(&cHotfolder, SIGNAL(sigNewOrder()), window, SLOT(
// Here is the mistake, that I can't find the Slot in QML
return app.exec();
}
这是我的main.cpp文件。 在此文件中,我从cpp文件中找到了Signal,但没有从QML中找到Slot。
main.qml:
function bla()
{
console.log("bla")
}
这是我的main.qml文件中的函数。
那么哪里有错误呢?
提前谢谢了!
本
解决方案是:
只需从QML而不是C ++连接到信号
例:
Component.onCompleted: hotfolder.sigNewOrder.connect(bla)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.