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.
Forum Updated to NodeBB v4.3 + New Features

Access the UI, from multiple windows.

Scheduled Pinned Locked Moved Solved General and Desktop
53 Posts 4 Posters 15.9k 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.
  • 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
                • 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
                  #30

                  @mrjj This method is not working

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

                  If i press the checkerbox, it should be setted to true

                  void Settings_Window::on_CheckerBox_clicked()
                  {
                  
                      B->Get_Window_Data()->ui->CheckerBox->setChecked(true);
                  
                  }
                  

                  Then when i go to window one, and press the button

                  void Window1::on_Button001_clicked()
                  {
                  
                  MainWindow* B = new MainWindow;
                  
                  
                  if(B->Get_Window_Data()->ui->Auto_Reset_Data_Timer->isChecked())
                  {
                  
                      //do something
                  
                    }
                  
                  }
                  

                  I don't understand all your methods, so if anyone can, please correct this stuff, cuz i have enough.

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

                    In what way not working ?
                    it just returns the pointer to your
                    Settings_Widget so only way it can fail is to return null.

                    I wonder here
                    void Window1::on_Button001_clicked()
                    {

                    MainWindow* B = new MainWindow; << you create new one. ?

                    so you dont have one already ?
                    Or is that on purpose ?
                    So each time you click you make a new window ?

                    L 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      In what way not working ?
                      it just returns the pointer to your
                      Settings_Widget so only way it can fail is to return null.

                      I wonder here
                      void Window1::on_Button001_clicked()
                      {

                      MainWindow* B = new MainWindow; << you create new one. ?

                      so you dont have one already ?
                      Or is that on purpose ?
                      So each time you click you make a new window ?

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

                      @mrjj I have another button to activate and show that window.

                      MainWindow* B = new MainWindow;
                      

                      No,i have already one window.
                      I just use this to get MainWindow method, and try to set checkerBox as true but from ther.

                      If i copy the same code from that button, and paste it to Window1, it's working, but is not the way how it should work, i dont want to set it ther....

                      I mean:

                      MainWindow* B = new MainWindow;
                      
                      B->Get_Window_Data()->ui->CheckerBox->setChecked(true);
                      
                      
                      if(B->Get_Window_Data()->ui->Auto_Reset_Data_Timer->isChecked())
                      {
                      
                          //do something
                      
                        }
                      

                      When i say "Is not working", i mean when i click the button, checkBox should return true, and when i compare it in another window, checkBox returns false, i think because i set it to another copy... How,i don't know. It should return Settings_Widget in window1.

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

                        Hi
                        Im sorry if we frustrated you. We just know from experience
                        what works bad over time so that is why we seem so keen to do it the right way.
                        Lets start over.
                        ok, so you have a normal main.cpp

                        int main(int argc, char* argv[]) {
                          QApplication a(argc, argv);
                          MainWindow w;
                          w.show();
                        
                          return a.exec();
                        }
                        

                        so w is mainwindow
                        and it has the true Settings_Window embedded.

                        So we need Window1 to have access to MainWins Settings_Window and not a new copy.
                        So Window1 should NOT new one it self but get the one from Mainwin.

                        I assume Mainwindow also creates and show Window1
                        You can change its constructor to take a Settings_Window * and it can then store it for accessing.

                        Like

                        #include "Settings_Window.h"  // so it knows the TYPE ( make to use right name if not that)
                        
                        class Window1 : public QMainWindow
                        {
                            Q_OBJECT
                        Settings_Window *GivenToMe;
                        public:
                            explicit Window1 (QWidget *parent = 0, Settings_Window *TheTrueOne) {
                        GivenToMe = TheTrueOne; // store the pointer for later
                        }
                            ~Window1 ();
                        ....
                        };
                        
                        // then in MainWin where you show the Window1
                        void MainWindow::ShowSettings() {
                        Window1 * Win1= new Window1(this,   & S_Window ); // the & to make it pointer
                        Win1->show();
                        }
                        

                        now inside
                        void Window1::SomeButtonClick() {
                        // you now have GivenToMe that is the Settings_Widget from MainWindow
                        now you can call public functions in it
                        if ( GivenToMe -> SomeFunc() ) ...

                        }

                        If you do not want to create public access functions but go directly via UI you have to make it public
                        so in Settings_Window.h

                        private:
                        Ui::Settings_Windowui;
                        must be
                        public:
                        Ui::Settings_Window
                        ui;

                        to allow
                        if ( GivenToMe -> ui->CheckerBox->isChecked() ) ...

                        --

                        What we tried to make you do was to add public access function in mainwindow
                        so you would say
                        if ( GivenToMe ->GetSettingForX() ) ( and it just returns the ui->Checker_01->isChecked() )

                        but if you really not want that, then you can make ui public and the syntax you want is then possible
                        even if its not best practice/bad idea/design.

                        Hope this helps. :)

                        L 2 Replies Last reply
                        3
                        • mrjjM mrjj

                          Hi
                          Im sorry if we frustrated you. We just know from experience
                          what works bad over time so that is why we seem so keen to do it the right way.
                          Lets start over.
                          ok, so you have a normal main.cpp

                          int main(int argc, char* argv[]) {
                            QApplication a(argc, argv);
                            MainWindow w;
                            w.show();
                          
                            return a.exec();
                          }
                          

                          so w is mainwindow
                          and it has the true Settings_Window embedded.

                          So we need Window1 to have access to MainWins Settings_Window and not a new copy.
                          So Window1 should NOT new one it self but get the one from Mainwin.

                          I assume Mainwindow also creates and show Window1
                          You can change its constructor to take a Settings_Window * and it can then store it for accessing.

                          Like

                          #include "Settings_Window.h"  // so it knows the TYPE ( make to use right name if not that)
                          
                          class Window1 : public QMainWindow
                          {
                              Q_OBJECT
                          Settings_Window *GivenToMe;
                          public:
                              explicit Window1 (QWidget *parent = 0, Settings_Window *TheTrueOne) {
                          GivenToMe = TheTrueOne; // store the pointer for later
                          }
                              ~Window1 ();
                          ....
                          };
                          
                          // then in MainWin where you show the Window1
                          void MainWindow::ShowSettings() {
                          Window1 * Win1= new Window1(this,   & S_Window ); // the & to make it pointer
                          Win1->show();
                          }
                          

                          now inside
                          void Window1::SomeButtonClick() {
                          // you now have GivenToMe that is the Settings_Widget from MainWindow
                          now you can call public functions in it
                          if ( GivenToMe -> SomeFunc() ) ...

                          }

                          If you do not want to create public access functions but go directly via UI you have to make it public
                          so in Settings_Window.h

                          private:
                          Ui::Settings_Windowui;
                          must be
                          public:
                          Ui::Settings_Window
                          ui;

                          to allow
                          if ( GivenToMe -> ui->CheckerBox->isChecked() ) ...

                          --

                          What we tried to make you do was to add public access function in mainwindow
                          so you would say
                          if ( GivenToMe ->GetSettingForX() ) ( and it just returns the ui->Checker_01->isChecked() )

                          but if you really not want that, then you can make ui public and the syntax you want is then possible
                          even if its not best practice/bad idea/design.

                          Hope this helps. :)

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

                          @mrjj Thank you, i like exemples rly :)

                          Because this is the only thing can help at least when you rly dont know what to do. Tomorrow i will try in this way.

                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            Hi
                            Im sorry if we frustrated you. We just know from experience
                            what works bad over time so that is why we seem so keen to do it the right way.
                            Lets start over.
                            ok, so you have a normal main.cpp

                            int main(int argc, char* argv[]) {
                              QApplication a(argc, argv);
                              MainWindow w;
                              w.show();
                            
                              return a.exec();
                            }
                            

                            so w is mainwindow
                            and it has the true Settings_Window embedded.

                            So we need Window1 to have access to MainWins Settings_Window and not a new copy.
                            So Window1 should NOT new one it self but get the one from Mainwin.

                            I assume Mainwindow also creates and show Window1
                            You can change its constructor to take a Settings_Window * and it can then store it for accessing.

                            Like

                            #include "Settings_Window.h"  // so it knows the TYPE ( make to use right name if not that)
                            
                            class Window1 : public QMainWindow
                            {
                                Q_OBJECT
                            Settings_Window *GivenToMe;
                            public:
                                explicit Window1 (QWidget *parent = 0, Settings_Window *TheTrueOne) {
                            GivenToMe = TheTrueOne; // store the pointer for later
                            }
                                ~Window1 ();
                            ....
                            };
                            
                            // then in MainWin where you show the Window1
                            void MainWindow::ShowSettings() {
                            Window1 * Win1= new Window1(this,   & S_Window ); // the & to make it pointer
                            Win1->show();
                            }
                            

                            now inside
                            void Window1::SomeButtonClick() {
                            // you now have GivenToMe that is the Settings_Widget from MainWindow
                            now you can call public functions in it
                            if ( GivenToMe -> SomeFunc() ) ...

                            }

                            If you do not want to create public access functions but go directly via UI you have to make it public
                            so in Settings_Window.h

                            private:
                            Ui::Settings_Windowui;
                            must be
                            public:
                            Ui::Settings_Window
                            ui;

                            to allow
                            if ( GivenToMe -> ui->CheckerBox->isChecked() ) ...

                            --

                            What we tried to make you do was to add public access function in mainwindow
                            so you would say
                            if ( GivenToMe ->GetSettingForX() ) ( and it just returns the ui->Checker_01->isChecked() )

                            but if you really not want that, then you can make ui public and the syntax you want is then possible
                            even if its not best practice/bad idea/design.

                            Hope this helps. :)

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

                            @mrjj

                            I have few errors:

                            default argument missing for parameter 2 of 'Window1::Window1(QWidget*, Settings_Window*)'
                            explicit Window1(QWidget *parent = 0, Settings_Window *TheTrueOne)
                            ^

                            redefinition of 'Window1::Window1(QWidget*, Settings_Window*)'
                            Window1::Window1(QWidget *parent, Settings_Window *TheTrueOne = 0) :
                            ^

                            'Window1::Window1(QWidget*, Settings_Window*)' previously defined here
                            explicit Window1(QWidget *parent = 0, Settings_Window *TheTrueOne)
                            ^

                            Can you correct it? Because i tried what i could, but doesn't help.

                            Window1.h

                            #ifndef WINDOW1_H
                            #define WINDOW1_H
                            
                            #include <QWidget>
                            #include "Settings_Window.h"
                            
                            namespace Ui {
                            class Window1;
                            }
                            
                            class Window1 : public QWidget
                            {
                                Q_OBJECT
                            
                                Settings_Window *GivenToMe;
                            
                            public:
                                explicit Window1(QWidget *parent = 0, Settings_Window *TheTrueOne)
                            {
                            
                                GivenToMe = TheTrueOne; // store the pointer for later
                            
                            }
                            
                            
                            ~Window1();
                            
                            private:
                            Ui::Window1 *ui;
                            };
                            
                            #endif // WINDOW1_H
                            

                            And this is Window1.cpp

                            #include "window1.h"
                            #include "ui_window1.h"
                            #include "settings_window.h"
                            
                            Window1::Window1(QWidget *parent, Settings_Window *TheTrueOne = 0) :
                                QWidget(parent),
                                ui(new Ui::Window1)
                            {
                                ui->setupUi(this);
                            }
                            
                            Window1::~Window1()
                            {
                                delete ui;
                            }
                            
                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #36

                              Hi,

                              Default arguments can only be set in the function declaration. So move all = 0 to the header.

                              As a matter of good practice, the parent parameter should always be last. Question of coherency with the Qt library and what other developers expects.

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

                              L 2 Replies Last reply
                              0
                              • SGaistS SGaist

                                Hi,

                                Default arguments can only be set in the function declaration. So move all = 0 to the header.

                                As a matter of good practice, the parent parameter should always be last. Question of coherency with the Qt library and what other developers expects.

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

                                @SGaist Ok, i try that.

                                1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  Hi,

                                  Default arguments can only be set in the function declaration. So move all = 0 to the header.

                                  As a matter of good practice, the parent parameter should always be last. Question of coherency with the Qt library and what other developers expects.

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

                                  @SGaist I still have some errors the:

                                  Error: redefinition of 'Window1::Window1(Settings_Window*, QWidget*)'
                                  Window1::Window1(Settings_Window *TheTrueOne,QWidget *parent) : QWidget(parent),
                                  ^

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

                                    Do you have matching declaration and implementation ?

                                    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

                                      Do you have matching declaration and implementation ?

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

                                      @SGaist

                                      Header :

                                          explicit Window1(Settings_Window *TheTrueOne = 0, QWidget *parent = 0)
                                          {
                                      
                                              GivenToMe = TheTrueOne;
                                      
                                          }
                                      

                                      Source:

                                      Window1::Window1(Settings_Window *TheTrueOne,QWidget *parent) :         
                                      QWidget(parent),ui(new Ui::Window1)
                                      {
                                      ui->setupUi(this);
                                      }
                                      

                                      error: redefinition of 'Window1::Window1(Settings_Window*, QWidget*)'
                                      Window1::Window1(Settings_Window *TheTrueOne,QWidget *parent) : QWidget(parent),ui(new Ui::Window1)
                                      ^

                                      I i remove ther Settings_Window, then i have no type specified error.

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

                                        You wrote the method twice: once in the header and a second time in the implementation file. That's what the compilers is complaining about. You can have only one implementation.

                                        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

                                          You wrote the method twice: once in the header and a second time in the implementation file. That's what the compilers is complaining about. You can have only one implementation.

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

                                          @SGaist Men, this stuff is default... I just add some extra parameters... What should i remove?

                                          Ps. Ok, i fixed it.

                                          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