[SOLVED]Best way to create a QML Dialog with QQuickView, to be run before main QML window
-
OK, so what I am doing is I have a QQuickView set to a 'main.qml' file, which is my main app window, then I have QGuiApplication, now I've come to realize that as soon as QGuiApplication::exec() is called my main.qml file is run along with all it's javascript, even if I don't call QQuickView::show(), what I need is the best way to create a QML dialog that will show before my main.qml, and before any of it's javascript is executed, I was thinking of having another QGuiApplication just for my dialog, which would then force everything else to wait until it's destroyed, but that doesn't seem like a very elegant way of handling this. What's the best way of creating a "pre-run" dialog in QML with Qt Quick???
This is going to be a "first run" dialog, to set up app configurations when the app is run for the first time. Please let me know if my question doesn't make sense and I'll try to clarify.
-
You can split your Dialog and App into two .qml files and have something like this maybe:
File Dialog.qml:
@
import QtQtuick 2.0
Rectangle{
signal done()
Text{
anchors.centerIn: parent
text: "This is my dialog"
}
MouseArea{
anchors.centerIn: parent
onClicked: parent.done()
}
}
@
File MyApp.qml:
@
import QtQtuick 2.0
Rectangle{
Text{
anchors.centerIn: parent
text: "This is my app"
}
}
@
and main.qml:
@
import QtQtuick 2.0Rectangle{
width: 640
height: 480Dialog{
id: dialog
anchors.fill: parent
onDone: {
dialog.visible = false;
}
}MyApp{
id: app
anchors.fill: parent
visible: !dialog.visible
}}
@
-
Interesting, I'm very new to QML, and it seems I'm learning new things about it every day, I never realized I could use a component I created just by calling it in QML (MyComponent.qml then in a QML file use MyComponent{}), and I didn't really think to make my own signal, one of the issues was I was relying on Component.onCompleted for a lot of my Javascript, which is why main.qml's Javascript was running even when it wasn't being shown.
So, I'm getting ideas now, using the example you posted above, could I do something like this?:
@
MyApp {
id: app
dialog.onDone: {
//Javascript goes here
}
}
@Or for a reverse, something like this?:
@
MyApp {
id: app
signal ready()
onReady(): {
//Javascript here
}
}
Dialog {
id: dialog
MouseArea {
anchors.centerIn: parent
onClicked: app.ready()
}
}
@Does that look like it would work?
-
MyApp in first example that you've posted, doesn't know what dialog is. You can pass dialog as property and connect to its signal using Connections:
@
MyApp {
id: app
property Dialog dialog
Connections{
target: dialog
onDone: {
console.log("done")
}
}
}
@
In second example, when adding handler to signal, dont use "()", right syntax is:
@
MyApp {
id: app
signal ready()
onReady: {
//Javascript here
}
}
@
I suggest you to go through examples that can be found in Qt SDK, you can find a lot from them. -
Ok, cool, this is putting me in the right direction, thanks for the help. And that was actually a typo when I added the '()' on the 'onReady', I already knew about that syntax :P But I wouldn't have thought about passing the dialog as a property.