[英]MessageDialog freezes when created from Slot qt
I am trying to show message dialog from worker thread. 我正在尝试从工作线程显示消息对话框。 Using slots and signals is standard way of communicating with/calling QML objects, but when dialog appears it's button is unclickable/not responding.
使用插槽和信号是与QML对象通信/调用QML对象的标准方法,但是当对话框出现时,它的按钮不可点击/没有响应。
main.cpp 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 BlockingDialog.qml
import QtQuick 2.7
import QtQuick.Dialogs 1.2
MessageDialog {
id:dialog
}
You're creating the QML engine on the stack in showSlot()
, so it will be destroyed when the function finishes. 您正在
showSlot()
在堆栈上创建QML引擎,因此当函数完成时它将被销毁。 The typical approach to loading QML files is to create the QML engine on the stack in main()
. 加载QML文件的典型方法是在
main()
的堆栈上创建QML引擎。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.