4

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.

4

1 回答 1

8

可能是枚举参数在将参数绑定到 QML 信号处理程序上下文的过程中导致错误。由于这个枚举似乎没有作为一种类型暴露给 QML,我不相信它可以正确地将其转换为 qml,这可能会破坏整个过程。

鉴于您在发出信号时传递了两个浮点数,它实际上是否应该是两个浮点输入而不是浮点数和枚举?

于 2013-07-22T17:58:35.543 回答