Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access the UI, from multiple windows.
Qt 6.12 LTS is out! See what's new in the release blog

Access the UI, from multiple windows.

Scheduled Pinned Locked Moved Solved General and Desktop
53 Posts 4 Posters 58.8k Views 2 Watching
  • 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.
  • L Loc888

    @mrjj Sorry, but i rly dont understand... I can pass the object here

    void MainWindow :: GetWindowData(Settings_Window* S_Window_Temp)   
     {
    
         S_Window_Temp =  & S_Window;
    
    }
    

    So why i cant make a reference to it from another window, if i have the access to pass it??

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #10

    @Loc888
    Hi
    Im not sure what part is causing your problems.
    Maybe we talk about different things.

    Yes, you can make a reference to a window.
    or give a pointer / ref to a window to another window.

    Like create settings_win in main.cpp
    give it to mainwindow.
    Mainwindow create a new Window and give settings_win to that also.

    L 1 Reply Last reply
    0
    • mrjjM mrjj

      @Loc888
      Hi
      Im not sure what part is causing your problems.
      Maybe we talk about different things.

      Yes, you can make a reference to a window.
      or give a pointer / ref to a window to another window.

      Like create settings_win in main.cpp
      give it to mainwindow.
      Mainwindow create a new Window and give settings_win to that also.

      L Offline
      L Offline
      Loc888
      wrote on last edited by Loc888
      #11

      @mrjj Yes, and that's the problem. Maybe i name this topic title a little bit bad, but i dont have any problem with access, because i can access them all.

      The problem is, when i pass the window with that function, it's seems like another copy, not reference, so when i change something, nothing happens because it change the copy variables, and i dont know why it happend.

      If i pass ther the window object, UI stuff should be ther??

      jsulmJ 1 Reply Last reply
      0
      • L Loc888

        @mrjj Yes, and that's the problem. Maybe i name this topic title a little bit bad, but i dont have any problem with access, because i can access them all.

        The problem is, when i pass the window with that function, it's seems like another copy, not reference, so when i change something, nothing happens because it change the copy variables, and i dont know why it happend.

        If i pass ther the window object, UI stuff should be ther??

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #12

        @Loc888 Let's clean up your code.

        // You need to declare S_Window_Temp as reference to pointer, else you cannot change it inside GetWindowData!
        void MainWindow :: GetWindowData(Settings_Window* &S_Window_Temp)   //<< Here i send Object from Settings_Window created in Window1
        {  
         S_Window_Temp =  &S_Window; 
        
         //  So i expect C (Object form Window1)  should be equal to S_Window (Object form MainWindow ), but it's not.
        }
        

        Actually you should simply return the pointer from GetWindowData as your current API design is bad (it isn't a good idea to change method parameters from inside the method):

        Settings_Window* void MainWindow :: GetWindowData()
        {  
            return &S_Window; 
        }

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        L 1 Reply Last reply
        2
        • jsulmJ jsulm

          @Loc888 Let's clean up your code.

          // You need to declare S_Window_Temp as reference to pointer, else you cannot change it inside GetWindowData!
          void MainWindow :: GetWindowData(Settings_Window* &S_Window_Temp)   //<< Here i send Object from Settings_Window created in Window1
          {  
           S_Window_Temp =  &S_Window; 
          
           //  So i expect C (Object form Window1)  should be equal to S_Window (Object form MainWindow ), but it's not.
          }
          

          Actually you should simply return the pointer from GetWindowData as your current API design is bad (it isn't a good idea to change method parameters from inside the method):

          Settings_Window* void MainWindow :: GetWindowData()
          {  
              return &S_Window; 
          }
          L Offline
          L Offline
          Loc888
          wrote on last edited by
          #13

          @jsulm If i just copy and paste, your code, it gives me an error.

          The best solution I found is just create the definition of the object in mainwindow.cpp under include, i test it with my own class, and it works exacly how i wanted it.

          Unfortunately when i do the same thing with the Window, it crash the application and gives a message:

          "QWidget: Must construct a QApplication before a QWidget"

          Any way to fix it?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #14

            Hi,

            The usual fix to this one is: don't create static QWidget based objects.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            L 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              The usual fix to this one is: don't create static QWidget based objects.

              L Offline
              L Offline
              Loc888
              wrote on last edited by
              #15

              @SGaist I dont know what you mean.

              mrjjM 1 Reply Last reply
              0
              • L Loc888

                @SGaist I dont know what you mean.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #16

                @Loc888
                Hi
                You cannot have global Widgets as they are NOT
                allow to be constructed before QApplication in main.
                They must be pointers and you can first new them After application is created.

                L 1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #17

                  If you have anything that looks like static MyClass thingy where MyClass is derived from QWidget, then remove it.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @Loc888
                    Hi
                    You cannot have global Widgets as they are NOT
                    allow to be constructed before QApplication in main.
                    They must be pointers and you can first new them After application is created.

                    L Offline
                    L Offline
                    Loc888
                    wrote on last edited by
                    #18

                    @mrjj I tried by reference, and this happend:

                    Error: 'QWidget& QWidget::operator=(const QWidget&)' is private
                    Class &operator=(const Class &) Q_DECL_EQ_DELETE;
                    ^

                    mingw48_32\include\QtWidgets\qwidget.h:728: in expansion of macro 'Q_DISABLE_COPY'
                    Q_DISABLE_COPY(QWidget)
                    ^

                    It happend in this line Class &operator=(const Class &) Q_DECL_EQ_DELETE;

                    I try later with pointers, and will see what happend.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #19

                      For more information why, read this.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      L 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        For more information why, read this.

                        L Offline
                        L Offline
                        Loc888
                        wrote on last edited by Loc888
                        #20

                        @SGaist

                        Now i tried with pointers, error :

                        error: invalid operands of types 'Settings_Window*' and 'Settings_Window*' to binary 'operator*'
                        A* &Settings_Widget;
                        ^

                        void MainWindow::Get_Window_Data(Settings_Window* A)
                        {

                        A* &Settings_Widget;
                        

                        }

                        What i did wrong?

                        mrjjM 1 Reply Last reply
                        0
                        • L Loc888

                          @SGaist

                          Now i tried with pointers, error :

                          error: invalid operands of types 'Settings_Window*' and 'Settings_Window*' to binary 'operator*'
                          A* &Settings_Widget;
                          ^

                          void MainWindow::Get_Window_Data(Settings_Window* A)
                          {

                          A* &Settings_Widget;
                          

                          }

                          What i did wrong?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #21

                          @Loc888
                          Hi
                          Why not just
                          Settings_Window* MainWindow::Get_Window_Data()
                          {
                          return Settings_Widget; // Settings_Widget is * (pointer) ?
                          }

                          L 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @Loc888
                            Hi
                            Why not just
                            Settings_Window* MainWindow::Get_Window_Data()
                            {
                            return Settings_Widget; // Settings_Widget is * (pointer) ?
                            }

                            L Offline
                            L Offline
                            Loc888
                            wrote on last edited by
                            #22

                            @mrjj

                            It run at least, but is not working. If i check something, then press the button with this code, nothing happened. Probably is another copy.
                            I am tired, tomorrow i try something else.

                            Is this the way how i should use it?

                            MainWindow B;
                            
                            
                            if(B.Get_Window_Data()->ui->Checker_Box_01->isChecked())
                            {
                            
                                //Do something
                            
                            }
                            
                            jsulmJ 1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #23

                              Hi
                              Yes but im old school and always check pointers
                              so i would do

                              MainWindow B; // this should not be a second instance but the one you (might) create in main.cpp

                              Settings_Window * win=B.Get_Window_Data();

                              if (!win) {
                              qDebug() << "NULL ptr from Get_Window_Data";
                              return;
                              }
                              if(win->ui->Checker_Box_01->isChecked())
                              {
                                  //Do something
                              }
                              if(win->ui->Checker_Box_XX->isChecked())
                              {
                                  //Do something
                              }
                              
                              

                              However, the UI variable is private so unless the using class is friend, its not allowed.
                              So you make make UI public ( bad design )
                              or provide access functions for the widgets.

                              1 Reply Last reply
                              0
                              • L Loc888

                                @mrjj

                                It run at least, but is not working. If i check something, then press the button with this code, nothing happened. Probably is another copy.
                                I am tired, tomorrow i try something else.

                                Is this the way how i should use it?

                                MainWindow B;
                                
                                
                                if(B.Get_Window_Data()->ui->Checker_Box_01->isChecked())
                                {
                                
                                    //Do something
                                
                                }
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #24

                                @Loc888 said in Access the UI, from multiple windows.:

                                B.Get_Window_Data()->ui->Checker_Box_01->isChecked()

                                From software design point of view this is so bad!
                                You should not expose internal details of your MainWindow like that.
                                Add public methods to MainWindow which return needed invormation without exposing internal implementation details.
                                Example:

                                class MainWindow...
                                {
                                public:
                                    bool isSomethingActivated()
                                    {
                                        return ui->Checker_Box_01->isChecked();
                                    }
                                }
                                
                                MainWindow B;
                                if(B.isSomethingActivated())
                                {
                                    //Do something
                                }
                                

                                Now, the user of MainWindow does not have to know anything about how the MainWindow is designed (what UI elements it has for example) - it just calls a public interface method to get the information. Usn't this much nicer? One more advantage of this approach: if you later change your MainWindow UI the caller of the MainWindow will not be affected.
                                Example:

                                class MainWindow...
                                {
                                public:
                                    bool isSomethingActivated()
                                    {
                                        // You decided that the condition should be different
                                        return ui->Checker_Box_01->isChecked() && ui->Checker_Box_02->isChecked();
                                    }
                                }
                                
                                // No need to change the caller of MainWindow
                                MainWindow B;
                                if(B.isSomethingActivated())
                                {
                                    //Do something
                                }
                                

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                mrjjM 1 Reply Last reply
                                2
                                • jsulmJ jsulm

                                  @Loc888 said in Access the UI, from multiple windows.:

                                  B.Get_Window_Data()->ui->Checker_Box_01->isChecked()

                                  From software design point of view this is so bad!
                                  You should not expose internal details of your MainWindow like that.
                                  Add public methods to MainWindow which return needed invormation without exposing internal implementation details.
                                  Example:

                                  class MainWindow...
                                  {
                                  public:
                                      bool isSomethingActivated()
                                      {
                                          return ui->Checker_Box_01->isChecked();
                                      }
                                  }
                                  
                                  MainWindow B;
                                  if(B.isSomethingActivated())
                                  {
                                      //Do something
                                  }
                                  

                                  Now, the user of MainWindow does not have to know anything about how the MainWindow is designed (what UI elements it has for example) - it just calls a public interface method to get the information. Usn't this much nicer? One more advantage of this approach: if you later change your MainWindow UI the caller of the MainWindow will not be affected.
                                  Example:

                                  class MainWindow...
                                  {
                                  public:
                                      bool isSomethingActivated()
                                      {
                                          // You decided that the condition should be different
                                          return ui->Checker_Box_01->isChecked() && ui->Checker_Box_02->isChecked();
                                      }
                                  }
                                  
                                  // No need to change the caller of MainWindow
                                  MainWindow B;
                                  if(B.isSomethingActivated())
                                  {
                                      //Do something
                                  }
                                  
                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #25

                                  @jsulm
                                  And we come full circle \o/
                                  I told OP that 4 days ago, but he seems unwilling to
                                  create access functions :)
                                  Maybe seeing your good example, OP will feel the joy of good design
                                  and do it the right way.

                                  L 3 Replies Last reply
                                  0
                                  • mrjjM mrjj

                                    @jsulm
                                    And we come full circle \o/
                                    I told OP that 4 days ago, but he seems unwilling to
                                    create access functions :)
                                    Maybe seeing your good example, OP will feel the joy of good design
                                    and do it the right way.

                                    L Offline
                                    L Offline
                                    Loc888
                                    wrote on last edited by
                                    #26

                                    @mrjj

                                    It makes me a little bit mad, why everything need's to be complicated.
                                    So, in main window can i use something like that:

                                      B.Get_Window_Data()->ui->Checker_Box_01->isChecked()
                                    

                                    But in other window, i need to do it in other way?

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • L Loc888

                                      @mrjj

                                      It makes me a little bit mad, why everything need's to be complicated.
                                      So, in main window can i use something like that:

                                        B.Get_Window_Data()->ui->Checker_Box_01->isChecked()
                                      

                                      But in other window, i need to do it in other way?

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #27

                                      @Loc888 You should no access internal implementation details of one window (class) from another. It is not related to Qt, this are simply software design basics...
                                      And what is complicated about it? Is my example really complicated? What you're trying to do is much more complicated and error prone. Why do you want to know in window A how window B is constructed? Why not simply define simple APIs to communicate between windows? To be honest code like what you're trying to write would not pass code reviews in the company where I'm working.

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      L 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        @jsulm
                                        And we come full circle \o/
                                        I told OP that 4 days ago, but he seems unwilling to
                                        create access functions :)
                                        Maybe seeing your good example, OP will feel the joy of good design
                                        and do it the right way.

                                        L Offline
                                        L Offline
                                        Loc888
                                        wrote on last edited by Loc888
                                        #28

                                        @mrjj Listen, i show you my code, cuz i am getting a little bit crazy, i can't create it in main.cpp, becasuse then how can i show it?

                                        In MainWindow.h i have

                                            Settings_Window* Get_Window_Data();
                                        
                                             Settings_Window* Settings_Widget = new Settings_Window;
                                        

                                        MainWindow.cpp

                                        Settings_Window* MainWindow::Get_Window_Data()
                                        {
                                        
                                            return Settings_Widget;
                                        
                                        }
                                        

                                        Window1

                                        MainWindow B;
                                        
                                        Settings_Window S;
                                        
                                        
                                        if(B.Get_Window_Data()->ui->Checker_01->isChecked())
                                        {
                                        
                                          
                                        }
                                        
                                        1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @Loc888 You should no access internal implementation details of one window (class) from another. It is not related to Qt, this are simply software design basics...
                                          And what is complicated about it? Is my example really complicated? What you're trying to do is much more complicated and error prone. Why do you want to know in window A how window B is constructed? Why not simply define simple APIs to communicate between windows? To be honest code like what you're trying to write would not pass code reviews in the company where I'm working.

                                          L Offline
                                          L Offline
                                          Loc888
                                          wrote on last edited by
                                          #29

                                          @jsulm I never gonna work for anybody from software design. I am doing it more like hobby.

                                          1 Reply Last reply
                                          0

                                          • Login

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