Qt World Summit: Register Today!

Executing QDialog from a class inherited from QObject

  • Hi,

    I have an Program class, which inherits from Engine, which inherits from QObject. The Engine class do some processing. The processing is started using a bool run() method implemented in the Program class. It returns true if it has been started or false if it failed. The error string can be read via QString errorString() method. When the processing is finished, the class sends a signal.

    Now I need to ask the user in the run() method whether to ignore some error and continue or quit. I know that I cannot inherit the Program from both Engine (aka QObject) and QWidget.

    The Program class is constructed using QWidgetparent object. Is it a good idea to cast this object in the run() method to initialize a QDialog (so it appears in the middle of the main window)?

    QDialog *dialog = new QDialog(qobject_cast<QWidget*>(this));

    Or should I use a different approach to ask a user from within a class inherited from QObject?

  • Lifetime Qt Champion


    Yes, you should use a different approach. The first thing to think about is: does it really make sense to have the dialog handling in that object ? Should it be the responsibility of e.g. the main window ? Like Program sending a signal for more inputs and main window get them before sending them to Program. Then the second thing is: will Program be used in a different thread ? If so, then you can't run GUI related stuff in a thread that is not the main thread a.k.a the GUI thread.

Log in to reply