Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Opening Window only once
Forum Update on Monday, May 27th 2025

Opening Window only once

Scheduled Pinned Locked Moved Solved QML and Qt Quick
10 Posts 3 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alecs26
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • A alecs26

      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

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @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.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      1
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @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();
            }
        }
        

        157

        A 1 Reply Last reply
        1
        • p3c0P p3c0

          @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();
              }
          }
          
          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @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. :)

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alecs26
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

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

              157

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alecs26
                wrote on last edited by
                #7

                @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

                A 1 Reply Last reply
                0
                • A alecs26

                  @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

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  @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. ;)

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    @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.

                    157

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      alecs26
                      wrote on last edited by
                      #10

                      @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

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved