Best code organization?
Lionel last edited by
I'm a total beginner in Qt and I didn't use C++ since multiple years. I recently found an opportunity to refresh and improve my knowledge by developing a small application (a quiz).
I created a first class for my main window, and I created two toolbars:
- One vertical which will allow the user to access to the different "sections" of my software (Question editor, Quiz editor, and Statistics, users management etc).
- One horizontal used to put the available actions (add, edit, remove, ...), which depends on the section where the user is;
Moreover, the main content of my software will be totally different depending on the current section.
For my first try, I left all the code for the different sections in the class of my main window. It works perfectly, but it may become hard to maintain.
This is why I'd like to create one separate class for each section.
My issue is that I don't know how to do that:
- Should each “section class” extend QWidget, and change the central widget of my main window each time that the user goes to another section? Or keep the same central widget all the time, add a layout, and include the widget of my subclass into this layout?
- How could those classes update my horizontal toolbar? I can of course pass the pointer of the main window to my widget class constructor, and use this pointer to access to the menu via some functions, but isn't there some better alternative?
Thanks in advance for your advices!!
Hi and welcome to devnet,
If you want to factor out independent widget, then yes they have to subclass QWidget (or your own widget subclass if you have a common set of functionalities they should all have) As for the central widget part, a QStackedWidget would be a good solution.
As for the toolbar, you should rather have some logic in your MainWindow that will pull information from your widget to update its content. You child widgets don't need to know anything from the MainWindow because it's not their task to update the widget that will contain them. Take for example that you change MainWindow for something else. It means that you would have to modify each of these widgets to accommodate the new MainWindow.
Hope it helps
Lionel last edited by
And thank you for your answer! :-)
You are right regarding QStackedWidget, it could represent a good solution for this scenario.
Regarding the toolbar it's a bit more complicated: the actions of the toolbar are linked with the content of the widget: for example, if you click on Edit or Delete, the action will be performed on the item of the QTreeView that you selected in the widget.
An option would be to create signals in the mainWindow which would call slots on those widgets:
connect(myButton, SIGNAL(triggered()), mySubWidget, SLOT(myFunction()));
But again I'm not sure if this is a valid solution...
In that case shouldn't rather your sub widgets provide the toolbar so all the connection can be made there and no need for the main window to handle the connection.