如何连接来自C ++的附加信号?

时间:2017-06-21 16:07:31

标签: qt qml qtquick2 signals-slots qt-signals

我想从C ++连接到对象的Component.onCompleted信号。我应该使用什么语法?

2 个答案:

答案 0 :(得分:0)

因此,您可以使用此代码访问Component.completed和Component.destruction信号:

QObject* attached_component = qmlAttachedPropertiesObject<QQmlComponent>(qml_component);
QObject::connect(attached_component, SIGNAL(destruction()), this, SLOT(onDestruction()));
QObject::connect(attached_component, SIGNAL(completed()), this, SLOT(onCompleted()));

qml_component是您的QML组件指针,'this'是具有onDestruction和onCompleted插槽的对象。 也许这是一个黑客但现在它的工作原理(QT 5.8 VS2013)

答案 1 :(得分:0)

一种解决方案是从QML调用C ++函数:

Component.onCompleted:Qml_utils.connect_qmlobject(this)

C ++代码

Q_INVOKABLE void qml_utils::connect_qmlobject(QObject*obj) {
  //do something with QML QObject:
  connect(obj, SIGNAL(destroyed(QObject*)),
          this,SLOT(disconnect_qmlobject(QObject*)));
}