更准确地说,如何使用信号卸载 QObject,以便接口在单独的线程中执行,而 Ccode 在单独的线程中执行。反馈将通过信号或回调或任何其他方式进行调整,但不会与代码中的接口绑定。
现在通过qmltypes以这种形式卸载(通过setContextProperty卸载时,同样的事情)
class Ccode : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_NAMED_ELEMENT(Ccode)
QML_SINGLETON
public slots:
void start();
signals:
void started();
}
接口挂起调用Ccode.start()
如果你把它放在一个线程中
QQmlEngine: Illegal attempt to connect to Ccode(0x7fff658c3e20) that is in a different thread than the QML engine QQmlApplicationEngine(0x7fff658c3e00.
我想在 QML 中连接
ApplicationWindow {
Connections {
target: Ccode
function onStarted() {
console.log("started")
}
}
Component.onCompleted: {
Ccode.start()
}
}
在启动功能中,使用熨斗。好吧,为简单起见:
void Ccode::start() {
QThread::sleep(5);
emit started();
}
它变成了一个外部类,其中信号已经重新发送到一个新的流:
我稍微更改了代码,而不是单例,我在 qml 中创建了一个常规类及其经典注册:
主.cpp:
现在 main.qml:
一切都像发条一样工作(除了信号
Ccode::started / qml::onStarted
- 我还没有测试过)