Important: Please read the Qt Code of Conduct -

Design: relations of classes

  • Hi. I've got two classes - MainWindow and Updater (inherited from QObject).

    Option 1:

    • MainWindow calls Updater::checkForUpdates();
    • Updater emits signal if updates are available;
    • MainWindow catches signal and shows corresponding QMessageBox.

    OR option 2:

    • MainWindow calls Updater::checkForUpdates();
    • Updater shows QMessageBox from itself (without a parent, as a consequence).

    The first option allows to easily set MainWindow as a QMessageBox parent.
    But the second one is more logical as an object-oriented design abstraction.

    The overall question: which option is better?

  • I think the first option is more logical from a design patterns point of view as it makes a clear difference between UI objects and others. The QMessageBox being a UI Object it is more logical that it is constructed by the QMainWindow. In that way the Updater only does the updating job and doesn't worries about UI.

    Moreover, I suppose that the comunication back to the QMainWindow is necessary to update also the QMainWindow itself to show data send by the Updater class.

    Hope it helps.

  • Seba, your post really clarifies the situation, thanks a lot!

Log in to reply