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 3.2k 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.
  • jsulmJ jsulm

    @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);
    
    Juan de DironneJ Offline
    Juan de DironneJ Offline
    Juan de Dironne
    wrote on 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
    0
    • Juan de DironneJ Juan de Dironne

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

      Juan de DironneJ 1 Reply Last reply
      1
      • M mpergand

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

        Juan de DironneJ Offline
        Juan de DironneJ Offline
        Juan de Dironne
        wrote on 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
        0
        • Juan de DironneJ Juan de Dironne

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

          Juan de DironneJ 1 Reply Last reply
          0
          • M mpergand

            @Juan-de-Dironne
            Seems OK.

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

            Juan de DironneJ Offline
            Juan de DironneJ Offline
            Juan de Dironne
            wrote on 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
            0
            • Juan de DironneJ Juan de Dironne

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

              Juan de DironneJ 1 Reply Last reply
              1
              • M mpergand

                @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

                Juan de DironneJ Offline
                Juan de DironneJ Offline
                Juan de Dironne
                wrote on 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
                0
                • Juan de DironneJ Juan de Dironne

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

                  Juan de DironneJ 1 Reply Last reply
                  0
                  • jsulmJ jsulm

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

                    Juan de DironneJ Offline
                    Juan de DironneJ Offline
                    Juan de Dironne
                    wrote on 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
                    0
                    • Juan de DironneJ Juan de Dironne

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

                      Juan de DironneJ 1 Reply Last reply
                      1
                      • jsulmJ jsulm

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

                        Juan de DironneJ Offline
                        Juan de DironneJ Offline
                        Juan de Dironne
                        wrote on 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

                        • Login

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