Executing QDialog from a class inherited from QObject
-
Hi,
I have an
Programclass, which inherits fromEngine, which inherits fromQObject. TheEngineclass do some processing. The processing is started using abool run()method implemented in theProgramclass. It returns true if it has been started or false if it failed. The error string can be read viaQString 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 theProgramfrom bothEngine(akaQObject) andQWidget.The
Programclass is constructed usingQWidgetparent object. Is it a good idea to castthisobject in therun()method to initialize aQDialog(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? -
Hi,
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.