Code Design - Sharing data between classes (QWidget)
-
wrote on 21 Nov 2013, 19:01 last edited by
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! -
wrote on 21 Nov 2013, 19:29 last edited by
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
-
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
-
wrote on 21 Nov 2013, 20:14 last edited by
Hello maximus,
There are many possible solutions to share data between to objects, but for now I would choose one of these:
- Use slots/signals mechanism to share data
- Create "data model" structure which is common for both of Your objects
hope thiswill help You,
Robert -
wrote on 21 Nov 2013, 20:19 last edited by
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 otherIn 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
-
wrote on 21 Nov 2013, 21:57 last edited by
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!
1/6