Reputation: 13
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.
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
}
Upvotes: 0
Views: 93
Reputation: 24386
You're creating the QML engine on the stack in showSlot()
, so it will be destroyed when the function finishes. The typical approach to loading QML files is to create the QML engine on the stack in main()
.
Upvotes: 2