我正在尝试从工作线程显示消息对话框。使用插槽和信号是与QML对象通信/调用QML对象的标准方式,但是当对话框出现时,它的按钮无法点击/无响应。
的main.cpp
tester* test = new tester;
QtConcurrent::run(test,tester::testFunction);
tester.cpp
#include "tester.h"
#include <QQmlEngine>
#include <QQmlComponent>
tester::tester(QObject *parent) : QObject(parent) {
QObject::connect(this,SIGNAL(show()),this,SLOT(showSlot()));
}
void tester::testFunction() {
emit show();
}
void tester::showSlot(){
QQmlEngine engine;
QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/BlockingDialog.qml")));
QObject *object = component.create();
QMetaObject::invokeMethod(object, "open");
}
tester.h
#include <QObject>
class tester : public QObject{
Q_OBJECT
public:
explicit tester(QObject *parent = 0);
void testFunction();
signals:
void show();
public slots:
void showSlot();
};
BlockingDialog.qml
import QtQuick 2.7
import QtQuick.Dialogs 1.2
MessageDialog {
id:dialog
}
答案 0 :(得分:2)
您正在showSlot()
中在堆栈上创建QML引擎,因此在函数完成时它将被销毁。加载QML文件的典型方法是在main()
。