简体   繁体   English

QML和C ++之间的信号和插槽

[英]Signal and Slot between QML and c++

my problem is, that I can't get a signal and slot connections between a cpp and a qml file. 我的问题是,我无法在cpp和qml文件之间获得信号和插槽连接。 First of all I've found some solutions in the web, but it doesn't work. 首先,我在网络上找到了一些解决方案,但这是行不通的。 I'm sure, that the mistake is mine, but I didn't find it. 我敢肯定,这个错误是我的,但是我没有找到它。

main.cpp: 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();
}

This is my main.cpp file. 这是我的main.cpp文件。 In this file I found the Signal from the cpp file, but not the Slot in QML. 在此文件中,我从cpp文件中找到了Signal,但没有从QML中找到Slot。

main.qml: main.qml:

function bla()
{
    console.log("bla")
}

This is the function in my main.qml file. 这是我的main.qml文件中的函数。

So where ist mistake? 那么哪里有错误呢?

Many thanks in advance! 提前谢谢了!

Ben

The solution is: 解决方案是:

Just connect to the signal from within QML and not from C++ 只需从QML而不是C ++连接到信号

example: 例:

Component.onCompleted: hotfolder.sigNewOrder.connect(bla)

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

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