(sharable) Dialog from a Component
-
Hi all -
I'm trying to put a Dialog into a Component, so I can reuse it.
ColumnLayout { Connections { target: equipmentModel function equipmentFunction () { var equipmentSavedAttention = attention.createObject() equipmentSavedAttention.title = "EQUIPMENT" equipmentSavedAttention.open() } } Connections { target: spaceModel function spaceFunction() { var spaceSavedAttention = attention.createObject() spaceSavedAttention.title = "SPACE" spaceSavedAttention.open() } } Component { id: attention Dialog { } } }
The instantiation seems to work, but when I try to open it, I get an error:
QML Dialog: cannot find any window to open popup in.
My root object is an ApplicationWindow, not a Window, if that makes a difference.
I looked at the docs for Dialog, and found a parentWindow property documented, but when I try to use it, I get this error:
Cannot assign to non-existent property "parentWindow"
So, I'm not sure where to go from here. Any suggestions?
Oh: Qt 6.5.3, Windows 11 (and eventually Android and Linux).
EDIT:
I neglected to mention that there will be occasions when I need both Dialogs to display at the same time (though it's OK if one overlays the other). This is why I'm trying to use a Component for it.
EDIT 2:
Importing QtQuick.Dialogs eliminated the non-existent property error, but I still have the original issue.
Thanks...
-
@mzimmers I remember I had to put it in main window qml and use its id to turn it on/off since it needs a parent window. If you put it anywhere, it may not have a parent window.
Or you may set parent of the dialog with root window explicitly. Try it out to see if it works.import QtQuick.Controls Dialog { property var parentWindow: null // Property to hold the parent window title: "Sample Dialog" modality: Qt.ApplicationModal // This makes the dialog modal parent: parentWindow // Set the parent property to the parent window Row { spacing: 10 Button { text: "Close" onClicked: dialog.close() } } }
-
@JoeCFD so, are you suggesting that I don't use a component? I don't see how that would work when I want to show multiple instances of the Dialog at the same time.
EDIT:
You gave me the idea for trying this, which does work:
ColumnLayout { ... Component { id: attention Attention { ... Connections { target: spaceModel function onSpaceSaved() { var spaceSavedAttention = attention.createObject(mainWindow, {messageText: "Success! Your SPACE configuration changes have been saved."}) spaceSavedAttention.open() } }
Using the createObject() call to set the parent and the properties seems to have been the difference maker. Thanks.
-
M mzimmers has marked this topic as solved on