Opening Window only once



  • Hello,

    I want to open a .qml window from a push button.
    I use QML Controls 2 on Windows with:

                    onClicked:
                    {
                        var component = Qt.createComponent("\Configur.qml");
                        var win = component.createObject();
                        win.show();
                    }
    

    This works but the problem is that I don't want the user to be able to open several copy of this window. With the code above, each time the user presses on the button, a new instance of the window is generated.

    What could I do to allow only a single instance of the window to be opened ?

    Thank you very much,

    Alex



  • @alecs26 Don't really know QML, I'm purely C++, but from the docs I read real quick it looks like you can make that variable a property at your item/class level with something like:

    MyClass {
       property var win = Qt.createComponent("\Configur.qml").createObject();
    
       // ...
       onClicked:
       {
          // since you only have 1 copy it will just re-show itself here
          win.show();
       }
    }
    

    With those code (which may not be syntactically correct as I don't know QML) it should create a single object win that will just be shown when someone clicks whatever button. With your code it creates a new window every time it is clicked rather than just showing the old one.


  • Moderators

    @alecs26 Or if you want to create it onClick then:

    property QtObject obj
    
    Button {
        text: "Click"
        onClicked: {
            if(!obj) {
                obj = Qt.createComponent("Configur.qml").createObject();
            }
            obj.show();
        }
    }
    


  • @p3c0 said in Opening Window only once:

    @alecs26 Or if you want to create it onClick then:

    property QtObject obj
    
    Button {
        text: "Click"
        onClicked: {
            if(!obj) {
                obj = Qt.createComponent("Configur.qml").createObject();
            }
            obj.show();
        }
    }
    

    Nice, that's what I wanted to tell him how to do but I didn't know scoping in QML. :)



  • Hello @p3c0 and @ambershark
    Thank you so much for your help. This code works very well, thank you !
    Just a quick question though. I would like the window to be created when the main window is created and not when the button is clicked. I tried something but I have errors or warnings:

    ApplicationWindow
    {
       property QtObject obj
       obj = Qt.createComponent("Configur.qml").createObject(); //The problem is here
       Button 
       {
        text: "Click"
        onClicked: 
        {
            obj.show();
        }
       }
    }
    

    I tried other things than "obj = Qt.createComponent("Configur.qml").createObject();"
    such as "obj:Qt.createComponent("Configur.qml").createObject();" but it didn't work...

    Thank you so much,

    Alex


  • Moderators

    @alecs26 At that scope only binding works which is created by using :, so replace = with :



  • @p3c0 Thank you again. It works. I thought I tried that before but I must have done something wrong, sorry for that.
    The last thing is that whit this line "obj:Qt.createComponent("\MenuQuit.qml").createObject();", the window opens.
    So this windows opens at the program start. However, I would only like it to show when I do "obj.show();" with the button.

    I thought of setting the window visibility to false and play with visibility but I guess its not the best way...

    Thank you again,

    Alex



  • @alecs26 Weird in Qt C++ you have to explicitly show the window with show(). I expected the same in QML. You could try just calling obj.hide() right after you create it as long as it doesn't flicker the window on and off it will be ok.

    If it does then we'll have to wait for someone who knows QML to help ya with that one. ;)


  • Moderators

    @alecs26

    The last thing is that whit this line "obj:Qt.createComponent("\MenuQuit.qml").createObject();", the window opens.
    So this windows opens at the program start. However, I would only like it to show when I do "obj.show();" with the button.

    Set the initial state as hidden for MenuQuit inside its file. And then later on click show it.



  • @ambershark @p3c0
    Thank you it works !
    I wasn't able to do obj.hide() since that must be in a condition (for instance onClicked), I can't do that in the Application Window. However, I set window "visibility:false" and it works.

    Thank you so much for your help, it's really appreciated !

    Alex


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.