Code Design - Sharing data between classes (QWidget)



  • 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!



  • 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


  • Lifetime Qt Champion

    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



  • 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



  • 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



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.