在 QML 中接收带有 C++ 参数的信号时未定义

问题描述 投票:0回答:1

我正在尝试从 C++ QObject 类接收带有 QML 参数的信号。
信号看起来被触发,但参数未定义:

我的班级.h

class MyClass : public QObject
{
    Q_OBJECT
public:
    explicit MyClass(QObject * parent = Q_NULLPTR);
    virtual ~MyClass();

    Q_INVOKABLE void monitorResult();

signals:
    void resultChanged(QString newResult);
}

myclass.cpp

#include "myclass.h"

MyClass::MyClass(QObject *parent) : QObject(parent)
{
    
}

MyClass::~MyClass()
{
}

void MyClass::monitorResult()
{
    QString str = "test";
    
    emit resultChanged(str);
}

主.cpp

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    QQmlContext *context = engine.rootContext();
    context->setContextProperty("MyClass", new MyClass());

    const QUrl url(QStringLiteral("qrc:/my.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);
)

QML

ApplicationWindow {
    id: screen
    visible: true
    width: 1920
    height: 93
    flags: Qt.FramelessWindowHint
    title: qsTr("screen")
    color: "transparent"

    Image {
        id: homeScreen_footer_image
        y: 70
        width: 1920
        height: 93
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 0
        opacity: 1
        source: "images/12_Footer.png"

        Timer {
            interval: 250
            running: true
            repeat: true
            onTriggered: MyClass.monitorResult()
        }

        Connections {
            target: MyClass

            function onResultChanged(result) {
                console.log("onResultChanged triggerred")
                console.log("result: " + result)
            }
        }
    }
}

我在 QML 上收到以下消息:

qml:onResultChanged 触发红色
qml:newGestureResult:未定义

有人可以帮助我吗?

提前欣赏。

我尝试在 Qt 文档和 stackoverflow 上找到解决方案,但仍然不知道发生了什么。

c++ qt qml qt5 qt-signals
1个回答
0
投票

我确实从 onResultChanged 函数中获取了 str。这是我的代码:

我的班级.h

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>

class MyClass : public QObject
{
    Q_OBJECT
public:
    explicit MyClass(QObject * parent = Q_NULLPTR);
    virtual ~MyClass();
public Q_SLOTS:
    void monitorResult();

Q_SIGNALS:
    void resultChanged(QString newResult);
};

#endif // MYCLASS_H

我的班级.cpp

#include "myclass.h"

MyClass::MyClass(QObject *parent) : QObject(parent)
{

}

MyClass::~MyClass()
{
}

void MyClass::monitorResult()
{
    QString str = "test";

    Q_EMIT resultChanged(str);
}

主.cpp

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;

    qmlRegisterType<MyClass>("app.MyClass", 1, 0, "MyClass");

    gst_init (&argc, &argv);
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);
    return app.exec();
}

QML

import app.MyClass 1.0

ApplicationWindow {
    id: mainItm
    MyClass{
        id: myClass
    }
    
    Timer {
        interval: 250
        running: true
        repeat: true
        onTriggered: myClass.monitorResult()
    }
    
    
    Connections {
        target: myClass
        
        function onResultChanged(result) {
            console.log("onResultChanged triggerred")
            console.log("result: " + result)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.