How to show Dialog window from QML plugin
-
I have QML plugin and qml-file inside it like
import QtQuick 2.0 import QtQuick.Controls import QtQuick.Dialogs Window { id: errorDialog objectName: "errorDialog" width: 300 height: 200 modality: Qt.ApplicationModal visible: true property string errorMessage: "" Text { text: errorMessage anchors.centerIn: parent } Button { text: "OK" anchors.bottom: parent.bottom anchors.horizontalCenter: parent.horizontalCenter onClicked: errorDialog.close() } }
This plugin has some class (FileListModel) inside and some method that tries to load this window and show it in modal mode:
Q_INVOKABLE void FileListModel::ShowDlg(const QString& name) QQmlComponent component(&m_Engine, QUrl(QStringLiteral("qrc:/ErrorDialog.qml"))); if (component.isError()) { qWarning() << component.errors(); } QObject *dialog = component.create(); QQuickWindow* window = qobject_cast<QQuickWindow*>(dialog); if (window) { window->setProperty("errorMessage", QString("Unknown file type: %1").arg(name)); window->setModality(Qt::ApplicationModal); qWarning() << "modaity " << window->isModal(); // true here window->show(); } else { qWarning() << "Failed to cast to QQuickWindow"; }
Here m_Engine has type QQmlApplicationEngine.
And I have a main application uses this plugin. This app has its own qml and on selecting some element calls FileListModel::ShowDlg.
The problem is this window is not modal. I also tried to use Dialog instead of Window in qml and call "open" but in this case dialog is not shown at all.
So, is it possible to make this window modal or use Dialog in the right way?