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. Best code organization?
Qt 6.11 is out! See what's new in the release blog

Best code organization?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.8k Views 2 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.
  • L Offline
    L Offline
    Lionel
    wrote on last edited by
    #1

    Hello,

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

    Lionel

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

      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

      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
      • L Offline
        L Offline
        Lionel
        wrote on last edited by
        #3

        Hello SGaist,

        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...

        Regards,

        Lionel

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

          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.

          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

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved