Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access the UI, from multiple windows.

Access the UI, from multiple windows.

Scheduled Pinned Locked Moved Solved General and Desktop
53 Posts 4 Posters 15.9k 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.
  • L Offline
    L Offline
    Loc888
    wrote on last edited by Loc888
    #1

    I have 3 windows, and i need to access the UI.

    Ther is MainWindow, Window1, and Settings_Window.

    In mainwindow.h i have a declaration of object, so it's

    Settings_Window S_Window;
    

    In MainWindow i have one button to show S_Window.

     void MainWindow::on_Show_Window_Button()
    
    S_Window.activateWindow;
    S_Window.show;
    

    And this is the problem, because this is the settings window, and window1 need to read some data from ther.

    If i create a new object in window1 so:

            Window1.cpp file:
    
    Settings_Window* C = new Settings_Window;
    

    And then activate it and show the window from MainWindow, if i check some options in S_Window , they dont affect Window1, they just affect MainWindow, it's probably because ther i create it, and i can use it directly.

    I create a function, to send from Window1 the object of Settings_Window, and make it equal to S_Window, because ther i am gonna change the options,but when i check some CheckBox it still affecting only MainWindow.

    I need help to make Settings_Window C created in Window1, equal to S_Window created and show from MainWindow.

    I try this code:

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

    Then:

    This is Window1:

    I include mainwindow.h and make friend class and:

    MainWindow MW;
    
    Settings_Window* C = new Settings_Window;   // (This is the copy created in Window1)
    
    MW.SendWindowData(& C);    // Here i call MainWindow object, call the function, send the copy of window, and trying to get the data below:
    
    
    if(C.ui->CheckerBox->isChecked())
    {
    
       //Do something
    
    }
    

    I think, when i am changing the options for window1, they are changing but in another copy

    Any help? I am not gonna tell you how mutch time i loss, just trying to fix that stuff, i check a lot of topic's, forums, but nothing work for me, need help here.

    I dont have time to watch another tutorials, because i almost end my tools, setting window is the last thing what i need to do, and i finish some simple but very usefull tools for me.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      The most common way is to provide data access functions to get the data from the
      widgets without letting rest of program know the exact type. ( as in direct access to UI object)

      so for your sample
      MainWindow MW;

      Settings_Window* C = new Settings_Window; // (This is the copy created in Window1)
      MW.SendWindowData(& C); // Here i call MainWindow object, call the function, send the copy of window,

      if( MW.getCheckerBoxStatus() )
      {
      //Do something
      }

      So getCheckerBoxStatus() returns the ui->CheckerBox status. The rest of the program do not know
      its in a checkbox and that is better design as if you later read it from a file, you need only to change
      getCheckerBoxStatus to return status from the data and rest of program do not need to change at all.
      Still works the same.

      L 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        The most common way is to provide data access functions to get the data from the
        widgets without letting rest of program know the exact type. ( as in direct access to UI object)

        so for your sample
        MainWindow MW;

        Settings_Window* C = new Settings_Window; // (This is the copy created in Window1)
        MW.SendWindowData(& C); // Here i call MainWindow object, call the function, send the copy of window,

        if( MW.getCheckerBoxStatus() )
        {
        //Do something
        }

        So getCheckerBoxStatus() returns the ui->CheckerBox status. The rest of the program do not know
        its in a checkbox and that is better design as if you later read it from a file, you need only to change
        getCheckerBoxStatus to return status from the data and rest of program do not need to change at all.
        Still works the same.

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

        @mrjj But ther is no way to just simply send the widnows object as reference ? Becuse ye, 75% of that setting window is just CheckerBoxe's, but some of stuff is different.
        It can help me, but i would like to find 100% solution

        I would like to have the same flexibility like in MainWindow.

        mrjjM 1 Reply Last reply
        0
        • L Loc888

          @mrjj But ther is no way to just simply send the widnows object as reference ? Becuse ye, 75% of that setting window is just CheckerBoxe's, but some of stuff is different.
          It can help me, but i would like to find 100% solution

          I would like to have the same flexibility like in MainWindow.

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

          @Loc888
          Well u can move the UI object to public section of the class and its accessible from outside but
          it leads to spaghetti code and really not recommended.
          External users of a class should not know the inner details and hence providing access functions beats
          letting everybody know what widgets a window has.

          L 1 Reply Last reply
          1
          • mrjjM mrjj

            @Loc888
            Well u can move the UI object to public section of the class and its accessible from outside but
            it leads to spaghetti code and really not recommended.
            External users of a class should not know the inner details and hence providing access functions beats
            letting everybody know what widgets a window has.

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

            @mrjj No, i dont like it, even if it helps, i mean make it public. Friend class should help, ye?

            And i am not talking about the class, i am talking about object, because like i said, i can access the class from Window1, but i need to access object S_Window from Setting_Window class, and that object is created in MainWindow, that's the problem.

            mrjjM 1 Reply Last reply
            0
            • L Loc888

              @mrjj No, i dont like it, even if it helps, i mean make it public. Friend class should help, ye?

              And i am not talking about the class, i am talking about object, because like i said, i can access the class from Window1, but i need to access object S_Window from Setting_Window class, and that object is created in MainWindow, that's the problem.

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

              @Loc888
              Ok, and its not possible to use signals and hook object up
              in mainwindow ?
              There is nothing stopping you from giving other class a pointer to some widget.
              like in mainwindow
              Local *local = new Local()
              Other *other= new Other()
              UseClass *useclass= new UseClass (this, local, other);

              L 1 Reply Last reply
              0
              • mrjjM mrjj

                @Loc888
                Ok, and its not possible to use signals and hook object up
                in mainwindow ?
                There is nothing stopping you from giving other class a pointer to some widget.
                like in mainwindow
                Local *local = new Local()
                Other *other= new Other()
                UseClass *useclass= new UseClass (this, local, other);

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

                @mrjj I create it with pointer already, i mean the object.
                I dont know how to use signal and slots in this example, what and wher should i type to pass the object??
                I use it with timers, but not with Windows objects.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi
                  With signals and slots your would pass the data not the objects.
                  Normally you hook things up in mainwindow as it knows most other class.
                  But really depends on where its created etc.

                  L 1 Reply Last reply
                  2
                  • mrjjM mrjj

                    Hi
                    With signals and slots your would pass the data not the objects.
                    Normally you hook things up in mainwindow as it knows most other class.
                    But really depends on where its created etc.

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

                    @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 1 Reply Last reply
                    0
                    • 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 Online
                          jsulmJ Online
                          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

                                          • Login

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