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

Using Stacked Widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 2.3k 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.
  • J Offline
    J Offline
    Juan de Dironne
    wrote on 15 Feb 2023, 09:13 last edited by
    #1

    I recently created a Post about an application that I have to make with many interfaces.
    As the number of interfaces is important I decided to separate my files (cpp and h) according to the interfaces.

    I had opted for a solution that was not ideal and @jsulm and @JonB therefore advised me to use "Stacked Widgets".
    So I searched and found some information, but very few refer to "Stacked Widgets" and a "Menu" (in a Main Window).
    But I finally managed to create a project with what I need.

    But is it correct...?
    Is the structure with file separation correct...?
    And if so, how could I modify a member present in my main window from an interface...? (example with "stringTest")

    main.cpp

    #include<QApplication>
    #include"MyMainWindow.h"
    
    int main(int nbArg, char* listeArg[])
    {
      QApplication myApp(nbArg,listeArg);
    
      MyMainWindow myWindow;
      myWindow.show();
    
      myApp.exec();
    }
    

    MyMainWindow.cpp

    #include"MyMainWindow.h"
    
    MyMainWindow::MyMainWindow()
    {
      setWindowTitle("Stacked Widgets");
      setMinimumHeight(600);
      setMinimumWidth(600);
    
      m_actionQuit = new QAction("Quit",this);
      m_actionInterfaceA = new QAction("Interface A",this);
      m_actionInterfaceB = new QAction("Interface B",this);
      m_actionInterfaceC = new QAction("Interface C",this);
    
      m_menuFile = menuBar()->addMenu("File");
      m_menuFile->addAction(m_actionInterfaceA);
      m_menuFile->addAction(m_actionInterfaceB);
      m_menuFile->addAction(m_actionInterfaceC);
      m_menuFile->addAction(m_actionQuit);
    
      // Definition of Widget Central
      centralZone = new QWidget;
    
      // Definitions of the Different Widgets
      widgetHome        = new Home;               widgetHome->setObjectName("home");
      widgetInterfaceA  = new InterfaceA;         widgetInterfaceA->setObjectName("interfaceA");
      widgetInterfaceB  = new InterfaceB;         widgetInterfaceB->setObjectName("interfaceB");
      widgetInterfaceC  = new InterfaceC;         widgetInterfaceC->setObjectName("interfaceC");
    
      // Definition of Stacked Widget
      m_stackedWidget = new QStackedWidget(centralZone);
      m_stackedWidget->setMinimumHeight(minimumHeight());
      m_stackedWidget->setMinimumWidth(minimumWidth());
      m_stackedWidget->addWidget(widgetHome);
      m_stackedWidget->addWidget(widgetInterfaceA);
      m_stackedWidget->addWidget(widgetInterfaceB);
      m_stackedWidget->addWidget(widgetInterfaceC);
    
      // Creation Layout and Placement
      QVBoxLayout *layoutCentralZone = new QVBoxLayout;
      centralZone->setLayout(layoutCentralZone);
      setCentralWidget(centralZone);
    
      // Creation Connection Signal / Slot
      connect(m_actionQuit,SIGNAL(triggered(bool)),qApp,SLOT(quit()));
      connect(m_actionInterfaceA,SIGNAL(triggered(bool)),this,SLOT(showInterfaceA()));
      connect(m_actionInterfaceB,SIGNAL(triggered(bool)),this,SLOT(showInterfaceB()));
      connect(m_actionInterfaceC,SIGNAL(triggered(bool)),this,SLOT(showInterfaceC()));
    }
    
    
    
    void MyMainWindow::showInterfaceA(){  m_stackedWidget->setCurrentIndex(m_stackedWidget->indexOf(widgetInterfaceA)); }
    
    void MyMainWindow::showInterfaceB() { m_stackedWidget->setCurrentIndex(m_stackedWidget->indexOf(widgetInterfaceB)); }
    
    void MyMainWindow::showInterfaceC() { m_stackedWidget->setCurrentIndex(m_stackedWidget->indexOf(widgetInterfaceC)); }
    

    MyMainWindow.h

    #ifndef MYMAINWINDOW_H
    #define MYMAINWINDOW_H
    
    #include<QtWidgets>
    #include"Home.h"
    #include"InterfaceA.h"
    #include"InterfaceB.h"
    #include"InterfaceC.h"
    
    class MyMainWindow : public QMainWindow
    {
      Q_OBJECT
    
      public:
      MyMainWindow();
      QString m_stringTest = "ABCDEF";
    
      public slots:
      void showInterfaceA();
      void showInterfaceB();
      void showInterfaceC();
    
      private:
      QMenu *m_menuFile;
      QAction *m_actionInterfaceA;
      QAction *m_actionInterfaceB;
      QAction *m_actionInterfaceC;
      QAction *m_actionQuit;
    
      QWidget *centralZone;
    
      QStackedWidget *m_stackedWidget;
      Home *widgetHome;
      InterfaceA *widgetInterfaceA;
      InterfaceB *widgetInterfaceB;
      InterfaceC *widgetInterfaceC;
    };
    
    #endif // MYMAINWINDOW_H
    

    Home.cpp

    #include"Home.h"
    
    Home::Home()
    {
      // Initialization
      QGridLayout *layoutPage;    layoutPage    = new QGridLayout();          layoutPage->setAlignment(Qt::AlignCenter);
    
      layoutPage->addWidget(new QLabel("Accueil"));
    
      // Definition of the "Layout" of the "Widget"
      setLayout(layoutPage);
    }
    

    Home.h

    #ifndef HOME_H
    #define HOME_H
    
    #include<QtWidgets>
    
    class Home : public QWidget
    {
      public:
      Home();
    
      private:
    };
    
    #endif // HOME_H
    

    InterfaceA.cpp

    #include"InterfaceA.h"
    
    InterfaceA::InterfaceA()
    {
      // Initialization
      QGridLayout *layoutPage;    layoutPage    = new QGridLayout();          layoutPage->setAlignment(Qt::AlignCenter);
    
      layoutPage->addWidget(new QLabel("Interface A"));
    
      // Definition of the "Layout" of the "Widget"
      setLayout(layoutPage);
    }
    

    InterfaceA.h

    #ifndef INTERFACEA_H
    #define INTERFACEA_H
    
    #include<QtWidgets>
    
    class InterfaceA : public QWidget
    {
      public:
      InterfaceA();
    
      private:
    };
    
    #endif // INTERFACEA_H
    

    The complete example is visible on gitHub at this adress :
    https://github.com/JuanDeDironne/qt_example_stacked_widgets

    JonBJ 1 Reply Last reply 15 Feb 2023, 09:22
    0
    • J Juan de Dironne
      15 Feb 2023, 09:13

      I recently created a Post about an application that I have to make with many interfaces.
      As the number of interfaces is important I decided to separate my files (cpp and h) according to the interfaces.

      I had opted for a solution that was not ideal and @jsulm and @JonB therefore advised me to use "Stacked Widgets".
      So I searched and found some information, but very few refer to "Stacked Widgets" and a "Menu" (in a Main Window).
      But I finally managed to create a project with what I need.

      But is it correct...?
      Is the structure with file separation correct...?
      And if so, how could I modify a member present in my main window from an interface...? (example with "stringTest")

      main.cpp

      #include<QApplication>
      #include"MyMainWindow.h"
      
      int main(int nbArg, char* listeArg[])
      {
        QApplication myApp(nbArg,listeArg);
      
        MyMainWindow myWindow;
        myWindow.show();
      
        myApp.exec();
      }
      

      MyMainWindow.cpp

      #include"MyMainWindow.h"
      
      MyMainWindow::MyMainWindow()
      {
        setWindowTitle("Stacked Widgets");
        setMinimumHeight(600);
        setMinimumWidth(600);
      
        m_actionQuit = new QAction("Quit",this);
        m_actionInterfaceA = new QAction("Interface A",this);
        m_actionInterfaceB = new QAction("Interface B",this);
        m_actionInterfaceC = new QAction("Interface C",this);
      
        m_menuFile = menuBar()->addMenu("File");
        m_menuFile->addAction(m_actionInterfaceA);
        m_menuFile->addAction(m_actionInterfaceB);
        m_menuFile->addAction(m_actionInterfaceC);
        m_menuFile->addAction(m_actionQuit);
      
        // Definition of Widget Central
        centralZone = new QWidget;
      
        // Definitions of the Different Widgets
        widgetHome        = new Home;               widgetHome->setObjectName("home");
        widgetInterfaceA  = new InterfaceA;         widgetInterfaceA->setObjectName("interfaceA");
        widgetInterfaceB  = new InterfaceB;         widgetInterfaceB->setObjectName("interfaceB");
        widgetInterfaceC  = new InterfaceC;         widgetInterfaceC->setObjectName("interfaceC");
      
        // Definition of Stacked Widget
        m_stackedWidget = new QStackedWidget(centralZone);
        m_stackedWidget->setMinimumHeight(minimumHeight());
        m_stackedWidget->setMinimumWidth(minimumWidth());
        m_stackedWidget->addWidget(widgetHome);
        m_stackedWidget->addWidget(widgetInterfaceA);
        m_stackedWidget->addWidget(widgetInterfaceB);
        m_stackedWidget->addWidget(widgetInterfaceC);
      
        // Creation Layout and Placement
        QVBoxLayout *layoutCentralZone = new QVBoxLayout;
        centralZone->setLayout(layoutCentralZone);
        setCentralWidget(centralZone);
      
        // Creation Connection Signal / Slot
        connect(m_actionQuit,SIGNAL(triggered(bool)),qApp,SLOT(quit()));
        connect(m_actionInterfaceA,SIGNAL(triggered(bool)),this,SLOT(showInterfaceA()));
        connect(m_actionInterfaceB,SIGNAL(triggered(bool)),this,SLOT(showInterfaceB()));
        connect(m_actionInterfaceC,SIGNAL(triggered(bool)),this,SLOT(showInterfaceC()));
      }
      
      
      
      void MyMainWindow::showInterfaceA(){  m_stackedWidget->setCurrentIndex(m_stackedWidget->indexOf(widgetInterfaceA)); }
      
      void MyMainWindow::showInterfaceB() { m_stackedWidget->setCurrentIndex(m_stackedWidget->indexOf(widgetInterfaceB)); }
      
      void MyMainWindow::showInterfaceC() { m_stackedWidget->setCurrentIndex(m_stackedWidget->indexOf(widgetInterfaceC)); }
      

      MyMainWindow.h

      #ifndef MYMAINWINDOW_H
      #define MYMAINWINDOW_H
      
      #include<QtWidgets>
      #include"Home.h"
      #include"InterfaceA.h"
      #include"InterfaceB.h"
      #include"InterfaceC.h"
      
      class MyMainWindow : public QMainWindow
      {
        Q_OBJECT
      
        public:
        MyMainWindow();
        QString m_stringTest = "ABCDEF";
      
        public slots:
        void showInterfaceA();
        void showInterfaceB();
        void showInterfaceC();
      
        private:
        QMenu *m_menuFile;
        QAction *m_actionInterfaceA;
        QAction *m_actionInterfaceB;
        QAction *m_actionInterfaceC;
        QAction *m_actionQuit;
      
        QWidget *centralZone;
      
        QStackedWidget *m_stackedWidget;
        Home *widgetHome;
        InterfaceA *widgetInterfaceA;
        InterfaceB *widgetInterfaceB;
        InterfaceC *widgetInterfaceC;
      };
      
      #endif // MYMAINWINDOW_H
      

      Home.cpp

      #include"Home.h"
      
      Home::Home()
      {
        // Initialization
        QGridLayout *layoutPage;    layoutPage    = new QGridLayout();          layoutPage->setAlignment(Qt::AlignCenter);
      
        layoutPage->addWidget(new QLabel("Accueil"));
      
        // Definition of the "Layout" of the "Widget"
        setLayout(layoutPage);
      }
      

      Home.h

      #ifndef HOME_H
      #define HOME_H
      
      #include<QtWidgets>
      
      class Home : public QWidget
      {
        public:
        Home();
      
        private:
      };
      
      #endif // HOME_H
      

      InterfaceA.cpp

      #include"InterfaceA.h"
      
      InterfaceA::InterfaceA()
      {
        // Initialization
        QGridLayout *layoutPage;    layoutPage    = new QGridLayout();          layoutPage->setAlignment(Qt::AlignCenter);
      
        layoutPage->addWidget(new QLabel("Interface A"));
      
        // Definition of the "Layout" of the "Widget"
        setLayout(layoutPage);
      }
      

      InterfaceA.h

      #ifndef INTERFACEA_H
      #define INTERFACEA_H
      
      #include<QtWidgets>
      
      class InterfaceA : public QWidget
      {
        public:
        InterfaceA();
      
        private:
      };
      
      #endif // INTERFACEA_H
      

      The complete example is visible on gitHub at this adress :
      https://github.com/JuanDeDironne/qt_example_stacked_widgets

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 15 Feb 2023, 09:22 last edited by
      #2

      @Juan-de-Dironne
      Looks like reasonable QStackedWidget usage to me.

      And if so, how could I modify a member present in my main window from an interface...? (example with "stringTest")

      Don't know what this means.

      void MyMainWindow::showInterfaceA(){ m_stackedWidget->setCurrentIndex(m_stackedWidget->indexOf(widgetInterfaceA)); }

      You can simplify this, since QStackedWidget offers

       m_stackedWidget->setCurrentWidget(widgetInterfaceA);
      

      connect(m_actionQuit,SIGNAL(triggered(bool)),qApp,SLOT(quit()));

      Old style connect()s with SIGNAL/SLOT macros were deprecated a decade ago. Please change over to:

      connect(m_actionQuit, &QAction::triggered, qApp, &QApplication::quit);
      
      J 1 Reply Last reply 15 Feb 2023, 09:49
      3
      • JonBJ JonB
        15 Feb 2023, 09:22

        @Juan-de-Dironne
        Looks like reasonable QStackedWidget usage to me.

        And if so, how could I modify a member present in my main window from an interface...? (example with "stringTest")

        Don't know what this means.

        void MyMainWindow::showInterfaceA(){ m_stackedWidget->setCurrentIndex(m_stackedWidget->indexOf(widgetInterfaceA)); }

        You can simplify this, since QStackedWidget offers

         m_stackedWidget->setCurrentWidget(widgetInterfaceA);
        

        connect(m_actionQuit,SIGNAL(triggered(bool)),qApp,SLOT(quit()));

        Old style connect()s with SIGNAL/SLOT macros were deprecated a decade ago. Please change over to:

        connect(m_actionQuit, &QAction::triggered, qApp, &QApplication::quit);
        
        J Offline
        J Offline
        Juan de Dironne
        wrote on 15 Feb 2023, 09:49 last edited by
        #3

        @JonB
        Thanks a lot for your answers.
        For the old style of connection, I confess that I find out.

        And when I say "And if so, how could I modify a member present in my main window from an interface...? (example with "stringTest")".
        This means that my "Interface A", for example, could retrieve input data that I would like to store in a variable of my "MainWindow" Class.
        So that this variable is, for example, used on other Interfaces.

        JonBJ 1 Reply Last reply 15 Feb 2023, 09:53
        0
        • J Juan de Dironne
          15 Feb 2023, 09:49

          @JonB
          Thanks a lot for your answers.
          For the old style of connection, I confess that I find out.

          And when I say "And if so, how could I modify a member present in my main window from an interface...? (example with "stringTest")".
          This means that my "Interface A", for example, could retrieve input data that I would like to store in a variable of my "MainWindow" Class.
          So that this variable is, for example, used on other Interfaces.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 15 Feb 2023, 09:53 last edited by JonB
          #4

          @Juan-de-Dironne said in Using Stacked Widgets:

          This means that my "Interface A", for example, could retrieve input data that I would like to store in a variable of my "MainWindow" Class.

          If the "interface" classes are modal dialogs you could return whatever was entered after QDialog::exec() to be stored in MainWindow. If they are modeless widgets (as you show) then I would probably write a signal which they emit with a parameter of the data, and have a slot in MainWindow forward the data to other interface widgets if required. Or possibly save it in some "shared" application/UI object (e.g. perhaps a model) which all interfaces can see. What I would not do is have something which allows the interface widgets to make direct calls into the MainWindow class.

          J 1 Reply Last reply 15 Feb 2023, 13:50
          2
          • JonBJ JonB
            15 Feb 2023, 09:53

            @Juan-de-Dironne said in Using Stacked Widgets:

            This means that my "Interface A", for example, could retrieve input data that I would like to store in a variable of my "MainWindow" Class.

            If the "interface" classes are modal dialogs you could return whatever was entered after QDialog::exec() to be stored in MainWindow. If they are modeless widgets (as you show) then I would probably write a signal which they emit with a parameter of the data, and have a slot in MainWindow forward the data to other interface widgets if required. Or possibly save it in some "shared" application/UI object (e.g. perhaps a model) which all interfaces can see. What I would not do is have something which allows the interface widgets to make direct calls into the MainWindow class.

            J Offline
            J Offline
            Juan de Dironne
            wrote on 15 Feb 2023, 13:50 last edited by
            #5

            @JonB Thanks again for your reply.
            You had already mentioned the "signals/slots" solution to communicate between the different classes in my Previous Post.
            So I have to look into this solution.
            But you mention the possibility of a shared object...? I don't see how to set this up...?
            It would be a class...? Inherited from QOject...? But if so, how do you share it...?
            I admit I don't know how to implement this solution.

            jsulmJ 1 Reply Last reply 15 Feb 2023, 14:05
            0
            • J Juan de Dironne
              15 Feb 2023, 13:50

              @JonB Thanks again for your reply.
              You had already mentioned the "signals/slots" solution to communicate between the different classes in my Previous Post.
              So I have to look into this solution.
              But you mention the possibility of a shared object...? I don't see how to set this up...?
              It would be a class...? Inherited from QOject...? But if so, how do you share it...?
              I admit I don't know how to implement this solution.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 15 Feb 2023, 14:05 last edited by
              #6

              @Juan-de-Dironne said in Using Stacked Widgets:

              It would be a class...? Inherited from QOject...? But if so, how do you share it...?

              What ever you want to share. It can be a variable of some basic type or an instance of a struct/class. To share it you simply pass a pointer or reference to this object to all objects which needs to use it:

              struct MyData
              {
              ...
              };
              
              MyData data;
              MyClass myClass(&data);
              MyClass myClass2(&data);
              

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

              J 1 Reply Last reply 15 Feb 2023, 15:17
              0
              • jsulmJ jsulm
                15 Feb 2023, 14:05

                @Juan-de-Dironne said in Using Stacked Widgets:

                It would be a class...? Inherited from QOject...? But if so, how do you share it...?

                What ever you want to share. It can be a variable of some basic type or an instance of a struct/class. To share it you simply pass a pointer or reference to this object to all objects which needs to use it:

                struct MyData
                {
                ...
                };
                
                MyData data;
                MyClass myClass(&data);
                MyClass myClass2(&data);
                
                J Offline
                J Offline
                Juan de Dironne
                wrote on 15 Feb 2023, 15:17 last edited by
                #7

                @jsulm Thanks for your time and reply
                So in my case I can create a structure like this :
                MySharedStructure.h

                struct MySharedStructure
                {
                  QString myString = "123";
                };
                

                That I instantiate in "MyMainWindow.cpp" and pass a pointer to the constructor of my "InterfaceA" like this :

                MyMainWindow.cpp

                #include"MyMainWindow.h"
                #include"MySharedStructure.h"
                
                MyMainWindow::MyMainWindow()
                {
                  setWindowTitle("Stacked Widgets");
                  setMinimumHeight(600);
                  setMinimumWidth(600);
                
                  MySharedStructure myData;
                ...
                ...
                  // Definitions of the Different Widgets
                  widgetHome        = new Home;                 widgetHome->setObjectName("home");
                  widgetInterfaceA  = new InterfaceA(&myData);  widgetInterfaceA->setObjectName("interfaceA");
                ...
                ...
                

                InterfaceA.h

                #include<QtWidgets>
                #include"MySharedStructure.h"
                
                class InterfaceA : public QWidget
                {
                  Q_OBJECT
                
                  public:
                  InterfaceA(MySharedStructure*);
                }
                

                With all this code in my constructor of my InterfaceA I can display if I want my character string stored in my structure like this :
                InterfaceA.cpp

                #include"InterfaceA.h"
                
                InterfaceA::InterfaceA(MySharedStructure* dataFonc)
                {
                  qDebug() << dataFonc->myString;
                }
                

                In principle, this is how I should set things up to have a shared structure (in this case)...?

                M 1 Reply Last reply 15 Feb 2023, 18:06
                0
                • J Juan de Dironne
                  15 Feb 2023, 15:17

                  @jsulm Thanks for your time and reply
                  So in my case I can create a structure like this :
                  MySharedStructure.h

                  struct MySharedStructure
                  {
                    QString myString = "123";
                  };
                  

                  That I instantiate in "MyMainWindow.cpp" and pass a pointer to the constructor of my "InterfaceA" like this :

                  MyMainWindow.cpp

                  #include"MyMainWindow.h"
                  #include"MySharedStructure.h"
                  
                  MyMainWindow::MyMainWindow()
                  {
                    setWindowTitle("Stacked Widgets");
                    setMinimumHeight(600);
                    setMinimumWidth(600);
                  
                    MySharedStructure myData;
                  ...
                  ...
                    // Definitions of the Different Widgets
                    widgetHome        = new Home;                 widgetHome->setObjectName("home");
                    widgetInterfaceA  = new InterfaceA(&myData);  widgetInterfaceA->setObjectName("interfaceA");
                  ...
                  ...
                  

                  InterfaceA.h

                  #include<QtWidgets>
                  #include"MySharedStructure.h"
                  
                  class InterfaceA : public QWidget
                  {
                    Q_OBJECT
                  
                    public:
                    InterfaceA(MySharedStructure*);
                  }
                  

                  With all this code in my constructor of my InterfaceA I can display if I want my character string stored in my structure like this :
                  InterfaceA.cpp

                  #include"InterfaceA.h"
                  
                  InterfaceA::InterfaceA(MySharedStructure* dataFonc)
                  {
                    qDebug() << dataFonc->myString;
                  }
                  

                  In principle, this is how I should set things up to have a shared structure (in this case)...?

                  M Offline
                  M Offline
                  mpergand
                  wrote on 15 Feb 2023, 18:06 last edited by mpergand
                  #8

                  @Juan-de-Dironne
                  Hi,
                  One of the OOP principles is to only give each object the food they need :)

                  So you can give your data a more structured form, like that:

                  
                  struct Interface1Data
                      {
                      int i=1;
                      };
                  
                  struct Interface2Data
                      {
                      int i=2;
                      };
                  
                  struct AllData
                  {
                      int i=0;    // global
                      Interface1Data data1;
                      Interface2Data data2;
                  };
                  
                  class Interface1
                  {
                      public:
                          Interface1(Interface1Data* d) : data(d)
                          {
                              qDebug()<<Q_FUNC_INFO<<data->i;
                          }
                  
                      private:
                          Interface1Data* data;
                  };
                  
                  class Interface2
                  {
                      public:
                          Interface2(Interface2Data* d) : data(d)
                          {
                              qDebug()<<Q_FUNC_INFO<<data->i;
                          }
                  
                      private:
                          Interface2Data* data;
                  };
                  
                  int main(int argc, char *argv[])
                  {
                      AllData data;
                      qDebug()<<data.i<<data.data1.i<<data.data2.i;
                      Interface1 i1(&data.data1);
                      Interface2 i2(&data.data2);
                      return 0;
                  

                  This way each interface deals with its own data and can't corrupt other interface ones.

                  J 1 Reply Last reply 16 Feb 2023, 11:05
                  1
                  • M mpergand
                    15 Feb 2023, 18:06

                    @Juan-de-Dironne
                    Hi,
                    One of the OOP principles is to only give each object the food they need :)

                    So you can give your data a more structured form, like that:

                    
                    struct Interface1Data
                        {
                        int i=1;
                        };
                    
                    struct Interface2Data
                        {
                        int i=2;
                        };
                    
                    struct AllData
                    {
                        int i=0;    // global
                        Interface1Data data1;
                        Interface2Data data2;
                    };
                    
                    class Interface1
                    {
                        public:
                            Interface1(Interface1Data* d) : data(d)
                            {
                                qDebug()<<Q_FUNC_INFO<<data->i;
                            }
                    
                        private:
                            Interface1Data* data;
                    };
                    
                    class Interface2
                    {
                        public:
                            Interface2(Interface2Data* d) : data(d)
                            {
                                qDebug()<<Q_FUNC_INFO<<data->i;
                            }
                    
                        private:
                            Interface2Data* data;
                    };
                    
                    int main(int argc, char *argv[])
                    {
                        AllData data;
                        qDebug()<<data.i<<data.data1.i<<data.data2.i;
                        Interface1 i1(&data.data1);
                        Interface2 i2(&data.data2);
                        return 0;
                    

                    This way each interface deals with its own data and can't corrupt other interface ones.

                    J Offline
                    J Offline
                    Juan de Dironne
                    wrote on 16 Feb 2023, 11:05 last edited by
                    #9

                    @mpergand
                    Whaouh
                    At first my brain made knots when I read your code.
                    Once your code understood (which works perfectly) I adapted it to separate the classes in file.
                    And it gives that.
                    main.cpp
                    main.png

                    AllData.h
                    all_data.png

                    Interface1
                    interface_1.png

                    I made screenshots because I think it's easier to show the structure of the project that I put in place.
                    And before going further, is this project structure correct by applying your principle...?

                    M 1 Reply Last reply 16 Feb 2023, 13:45
                    0
                    • J Juan de Dironne
                      16 Feb 2023, 11:05

                      @mpergand
                      Whaouh
                      At first my brain made knots when I read your code.
                      Once your code understood (which works perfectly) I adapted it to separate the classes in file.
                      And it gives that.
                      main.cpp
                      main.png

                      AllData.h
                      all_data.png

                      Interface1
                      interface_1.png

                      I made screenshots because I think it's easier to show the structure of the project that I put in place.
                      And before going further, is this project structure correct by applying your principle...?

                      M Offline
                      M Offline
                      mpergand
                      wrote on 16 Feb 2023, 13:45 last edited by
                      #10

                      @Juan-de-Dironne
                      Seems OK.

                      I presume you want to save/load the all data structure, it's time to think about it ;)

                      J 1 Reply Last reply 16 Feb 2023, 15:55
                      0
                      • M mpergand
                        16 Feb 2023, 13:45

                        @Juan-de-Dironne
                        Seems OK.

                        I presume you want to save/load the all data structure, it's time to think about it ;)

                        J Offline
                        J Offline
                        Juan de Dironne
                        wrote on 16 Feb 2023, 15:55 last edited by
                        #11

                        @mpergand
                        Thank you again for your answers and the time spent :)
                        And "to save/load the all data structure". That's to say...?

                        Because I actually have a question. I have shared data but basically my "Interfaces" files should allow me to separate each Graphical Interface (each Stacked Widget).
                        And let's imagine that my "Interface 1" is a page with input of information and validation of it.
                        How could I save this information in my "AllData" structure so that it can be shared later...?
                        Hope to be clear in my explanations.

                        M 1 Reply Last reply 16 Feb 2023, 16:24
                        0
                        • J Juan de Dironne
                          16 Feb 2023, 15:55

                          @mpergand
                          Thank you again for your answers and the time spent :)
                          And "to save/load the all data structure". That's to say...?

                          Because I actually have a question. I have shared data but basically my "Interfaces" files should allow me to separate each Graphical Interface (each Stacked Widget).
                          And let's imagine that my "Interface 1" is a page with input of information and validation of it.
                          How could I save this information in my "AllData" structure so that it can be shared later...?
                          Hope to be clear in my explanations.

                          M Offline
                          M Offline
                          mpergand
                          wrote on 16 Feb 2023, 16:24 last edited by mpergand
                          #12

                          @Juan-de-Dironne said in Using Stacked Widgets:

                          How could I save this information in my "AllData" structure so that it can be shared later...?

                          Currently each interface receives a pointer to the data, not a copy, that means:
                          Interface1Data* data
                          points to allData.data1
                          (same addresses)

                          so in interface1
                          data->i=xx
                          is the same as
                          allData.data1.i=xx

                          J 1 Reply Last reply 17 Feb 2023, 13:01
                          1
                          • M mpergand
                            16 Feb 2023, 16:24

                            @Juan-de-Dironne said in Using Stacked Widgets:

                            How could I save this information in my "AllData" structure so that it can be shared later...?

                            Currently each interface receives a pointer to the data, not a copy, that means:
                            Interface1Data* data
                            points to allData.data1
                            (same addresses)

                            so in interface1
                            data->i=xx
                            is the same as
                            allData.data1.i=xx

                            J Offline
                            J Offline
                            Juan de Dironne
                            wrote on 17 Feb 2023, 13:01 last edited by
                            #13

                            @mpergand
                            I'm trying to understand what you're explaining.
                            And if I understood correctly, you are telling me that I can access all the data present in my "Interface" classes from my "allData" class.
                            But in principle, I would like from my interface to be able to update "global" data.
                            And with this principle I do not see how to do it in fact.

                            jsulmJ 1 Reply Last reply 17 Feb 2023, 13:31
                            0
                            • J Juan de Dironne
                              17 Feb 2023, 13:01

                              @mpergand
                              I'm trying to understand what you're explaining.
                              And if I understood correctly, you are telling me that I can access all the data present in my "Interface" classes from my "allData" class.
                              But in principle, I would like from my interface to be able to update "global" data.
                              And with this principle I do not see how to do it in fact.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 17 Feb 2023, 13:31 last edited by
                              #14

                              @Juan-de-Dironne said in Using Stacked Widgets:

                              I would like from my interface to be able to update "global" data

                              "global data" is bad design.
                              But if you pass pointer to your "global data" like @mpergand wrote you can modify this global data because you're not passing copies of the global data, but pointer to it. So, don't know what the problem is...

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

                              J 1 Reply Last reply 20 Feb 2023, 09:16
                              0
                              • jsulmJ jsulm
                                17 Feb 2023, 13:31

                                @Juan-de-Dironne said in Using Stacked Widgets:

                                I would like from my interface to be able to update "global" data

                                "global data" is bad design.
                                But if you pass pointer to your "global data" like @mpergand wrote you can modify this global data because you're not passing copies of the global data, but pointer to it. So, don't know what the problem is...

                                J Offline
                                J Offline
                                Juan de Dironne
                                wrote on 20 Feb 2023, 09:16 last edited by
                                #15

                                @jsulm
                                In fact the principle is not to set up "global" data which could be accessible and shared between several interfaces...? Because...?
                                This concept is not good...?

                                Because that's what I had actually envisioned.
                                An interface (interface 1) allowed me for example to display a connection screen and manage the identification.
                                Once logged in the "User ID" was stored in a "global structure".
                                This ID could be used by another interface (interface 2, interface 3, ...)

                                But with pointer passing, we can access data from different interfaces. But no shared data in fact..? But no global data...?

                                jsulmJ 1 Reply Last reply 20 Feb 2023, 09:48
                                0
                                • J Juan de Dironne
                                  20 Feb 2023, 09:16

                                  @jsulm
                                  In fact the principle is not to set up "global" data which could be accessible and shared between several interfaces...? Because...?
                                  This concept is not good...?

                                  Because that's what I had actually envisioned.
                                  An interface (interface 1) allowed me for example to display a connection screen and manage the identification.
                                  Once logged in the "User ID" was stored in a "global structure".
                                  This ID could be used by another interface (interface 2, interface 3, ...)

                                  But with pointer passing, we can access data from different interfaces. But no shared data in fact..? But no global data...?

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 20 Feb 2023, 09:48 last edited by
                                  #16

                                  @Juan-de-Dironne said in Using Stacked Widgets:

                                  But no shared data in fact..?

                                  What do you mean? If you pass pointer to some variable to different parts of your code then you're sharing this variable.

                                  "Once logged in the "User ID" was stored in a "global structure"." - can you explain what exactly you mean if you write "global data"?

                                  There should not be global data in a properly designed application. There should be clear ownership for each piece of data. For example your user id should be stored in some class which then provides it to whoever needs it.

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

                                  J 1 Reply Last reply 22 Feb 2023, 09:37
                                  1
                                  • jsulmJ jsulm
                                    20 Feb 2023, 09:48

                                    @Juan-de-Dironne said in Using Stacked Widgets:

                                    But no shared data in fact..?

                                    What do you mean? If you pass pointer to some variable to different parts of your code then you're sharing this variable.

                                    "Once logged in the "User ID" was stored in a "global structure"." - can you explain what exactly you mean if you write "global data"?

                                    There should not be global data in a properly designed application. There should be clear ownership for each piece of data. For example your user id should be stored in some class which then provides it to whoever needs it.

                                    J Offline
                                    J Offline
                                    Juan de Dironne
                                    wrote on 22 Feb 2023, 09:37 last edited by
                                    #17

                                    Thank you all for your past and your answers. Indeed I learned through your answers to set up a sharing of data and especially to understand how to organize the data and divide them well into classes. Thank you again :) After... It's not won, I have to get used to this gymnastics of the mind but I have grasped the principle.

                                    1 Reply Last reply
                                    0

                                    1/17

                                    15 Feb 2023, 09:13

                                    • Login

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