简体   繁体   中英

Receiving C++ signal in QML

I have the signal being emitted and then caught in QML; however, when I try to read the parameters attached to the signal I get "undefined." Following are some code snippets. Thanks for the help ahead of time!

mymodel.h

class MyModel : public QObject
{
    Q_OBJECT

    ...

    signals:
        void mySignal(float a, some::enum b)

    ...
}

mymodel.cpp

Do something to emit the signal (this isn't a problem, simply emit mySignal(1.0, 2.0); )

someotherclass.cpp

void SomeOtherClass::setupQML() { ...

QQuickView *view = new QQuickView();

QWidget *container = QWidget::createWindowContainer(view);

...

QmlRootData = new RootData();

view->rootContext()->setContextObject(QmlRootData);
view->rootContext()->setContextProperty("MyModel", model);
view->setSource(QUrl("main.qml"));
view->setResizeMode(QQuickView::SizeRootObjectToView);

QObject* rootObj = view->rootObject();

...

}

main.qml

Rectangle {
    Connections {
        target: MyModel
        onMySignal: console.log(a)
    }
}

The above console.log(a) gets called when expected; however, I would expect the output to be "1.0" but it simply says "undefined" and I'm not sure why. I am using Qt 5.1 and Qt Quick 2.0.

It may be that the enum param is causing an error in the process that binds the parameters into the QML signal handler's context. Since it doesn't appear that this enum is exposed as a type to QML I don't believe it can correctly translate it into qml and this might break the whole process.

Given that you are passing two floats when emitting the signal, is it actually supposed to be two float inputs instead of a float and an enum?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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