簡體   English   中英

QML和C ++之間的信號和插槽

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM