Getting C ++ Signal in QML

I have a signal that is emitted and then gets into QML; however, when I try to read the parameters attached to the signal, I get "undefined". Below are some snippets of code. Thanks for the help in advance!

mymodel.h

class MyModel : public QObject { Q_OBJECT ... signals: void mySignal(float a, some::enum b) ... } 

mymodel.cpp

Do something to emit a signal (this is not a problem, just 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) is called when expected; however, I expect the output to be "1.0", but it just says "undefined" and I'm not sure why. I am using Qt 5.1 and Qt Quick 2.0.

+4
source share
1 answer

It is possible that the enum parameter causes an error in the process that binds the parameters in the context of the QML signal handler. Since this enumeration does not appear to display as a type for QML, I do not believe that it can correctly translate it into qml, and this can disrupt the whole process.

Given that you miss two floats when emitting a signal, should it really be two floating point inputs instead of float and enum?

+6
source

Source: https://habr.com/ru/post/1492755/


All Articles