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. Code Design - Sharing data between classes (QWidget)
Forum Updated to NodeBB v4.3 + New Features

Code Design - Sharing data between classes (QWidget)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.9k Views 1 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    Hi,

    To make my MainWindow smaller, I have made some other child Widgets (Qt Designer form class), each representing one page interface in my application, that way the code is decomposed and no longer a huge block of code in my MainWindow.

    The downside of that, is that I have not figured a good way to share data between 2 child widget.

    "Here is my class schema":https://www.dropbox.com/s/6t37ecgfhnc8rb7/schemaClass.png

    Let say that my class "WorkoutPage" needs to access some attribute of the class "AntGearPage".
    Usually I could access it directly because it was all in the MainWindow, but now it his out of reach. I have tought of using QSettings, but It looked really bad practice to save in settings for in-class communication.

    Also tought of using the .parent() method to get back to my MainWindow, then reaching the other class, but looks complicated
    ex :
    @void Main_WorkoutPage::on_tableView_workout_doubleClicked(const QModelIndex &index) {

    qDebug() << this->parent()->parent()->parent()->parent()->parent()->parent()->parent()->objectName();
    

    // MainWindow w = this->parent()->parent()->parent()->parent()->parent()->parent()->parent();@

    Is there some other way that i'm not seeing ?
    Thank you!


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on last edited by
      #2

      I think I'm on the way to fixing it.

      Added pointer to the class in MainWindow
      @ Main_WorkoutPage* workoutPage;
      Main_AntGearPage* antGearPage;
      @

      Then I can retrieve with :
      @ qDebug() << this->parent()->parent()->parent()->parent()->parent()->parent()->parent()->objectName();
      MainWindow w = (MainWindow)this->parent()->parent()->parent()->parent()->parent()->parent()->parent();
      qDebug() << w->antGearPage->HR_SENSOR_ID;@

      P.S : have to create getters for private members


      Free Indoor Cycling Software - https://maximumtrainer.com

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

        Hi,

        I don't know the rest of the design but if your AntGearPage is needed only by your WorkoutPage, why not make it a children of it ?

        In the other case, you can use signals and slots to communicate between the two

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          poorBob
          wrote on last edited by
          #4

          Hello maximus,

          There are many possible solutions to share data between to objects, but for now I would choose one of these:

          1. Use slots/signals mechanism to share data
          2. Create "data model" structure which is common for both of Your objects

          hope thiswill help You,
          Robert

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maximus
            wrote on last edited by
            #5

            All those child QWidget are on the same level, they are all child of QMainWindow only to decompose the code.

            But all the child QWidget should be able to share data across each other (like brothers!)
            I'm thinking adding pointers to all the other brother page on each child instead of keeping the pointer in the parent, and then putting the class friend of each other

            In QMainWindow:

            @ Main_WorkoutPage *tmpWorkout = ui->tab_workout1;
            Main_AntGearPage *tmpGearPage = ui->tab_gear1;
            tmpGearPage->setWorkoutPage(tmpWorkout); //keep a pointer to WorkoutPage directly on AntGearPage
            tmpWorkout.setGearPage(tmpGearPage)@

            Thank you


            Free Indoor Cycling Software - https://maximumtrainer.com

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maximus
              wrote on last edited by
              #6

              Thanks for your help guys,

              Finally I just added a pointer to "GearPage" in "WorkoutPage" in QMainWindow:
              @ui->tab_workout1->setAntGearPage(ui->tab_gear1); //keep a pointer to AntGearPage directly on WorkoutPage@

              and in GearPage, I defined Workout page as friend.
              @friend class Main_WorkoutPage;@

              Now I can access every member in GearPage from WorkoutPage, don't know if it's good design but works for me!


              Free Indoor Cycling Software - https://maximumtrainer.com

              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