Using Stacked Widgets
-
@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 inMainWindow
. 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 inMainWindow
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 theMainWindow
class.@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. -
@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.@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-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);
@jsulm Thanks for your time and reply
So in my case I can create a structure like this :
MySharedStructure.hstruct 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)...?
-
@jsulm Thanks for your time and reply
So in my case I can create a structure like this :
MySharedStructure.hstruct 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)...?
@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-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.
@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
AllData.h
Interface1
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...? -
@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
AllData.h
Interface1
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...?@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-Dironne
Seems OK.I presume you want to save/load the all data structure, it's time to think about it ;)
@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. -
@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.@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-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@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. -
@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.@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-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...@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...?
-
@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...?
@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-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.
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.