Important: Please read the Qt Code of Conduct -

[Solved] QDialog as UserControl?

  • Hi
    I have a QTreeWidget and a QStackedWidget on my dialog. Navigation occurs when user select's an item in QTreeWidget (QStackedWidget's currentIndex property is updated) and user sees another view.
    Now my problem is that, I have many view's in one dialog, which makes code maintenance a bit more difficult, because, all the view's declared in QStackedWidget's pages are declared and defined in one dialog class (it will become huge in time). So I thought about UserControl. If you are unfamiliar with .NET's UserControl's concept, it's a class which hosts several widgets and ui logic inside.
    But the problem is that (as far as I know) qt widgets doesn't support UserControls. So I made a little improvisation and created a new Dialog derived from QDialog, added my widgets and ui logic and hosted that dialog inside main window.

    @UserDialog *d = new UserDialog(this);
    As you can see, UserDialog is not a widget, but a QDialog (still QDialog derives from QWidget). If I use this approach, my problems are easily fixed, but I don't know if it's safe, I mean, if this approach is discouraged or it has some drawbacks. What do you think? Is it ok to host QDialog as a widget in another dialog?


  • I don't get your question? What are your concerned about? Is it about subclassing QDialog at all? If so, go ahead! If a class is not supposed to be subclassed, it's stated in the docs (or does not make sense at all). Btw.. if you look at the docs of QDialog you will see:

    bq. Inherited by: Q3FileDialog, Q3ProgressDialog, Q3TabDialog, Q3Wizard, QAbstractPrintDialog, QColorDialog, QErrorMessage, QFileDialog, QFontDialog, QInputDialog, QMessageBox, QPageSetupDialog, QPrintPreviewDialog, QProgressDialog, and QWizard.

  • No, that's not my question. I'm not fluent in English, so sorry for that :)
    My question is if I can use QDialog, just like I use any other widgets. For example, I can add QPushButton, QTreeView or any other widget on my dialog. I want to know, instead of adding QPushButton or any other widget, what if I add another dialog in a dialog :D just like I do with other widgets. I already tested this approach and it works fine, but I want to know if this approach is not recommended/discouraged.

    Thanks again :)

  • You can - in the sense of the compiler will not moan. But you're not supposed to. A QDialog ist always a toplevel widget, it does not make sense to add it into another toplevel widget.

    I don't know the concept of that UserControl - never got my hands dirty at .NET :-) if you would like to describe that functionality a bit more, we may come with an alternative solution for you.

  • I think, that instead of using a QDialog as your base class, you should use a plain QWidget or perhaps a QFrame or something like that. That will allow you create a composite widget that is very easy to embed in a larger composition. QDialog is mend for toplevel use, as Volker already mentioned.

  • Thanks andre. Is it possible to use the designer with QWidget or QFrame controls? If so, how? :(
    sorry for the stupid question

  • Yes, you can. When you start a new file with designer, you can choose QMainWindow, QDialog or QWidget based. Take the QWidget based one, and you're all set.

  • @Andre, ouch, how could I miss that :)
    Thank you very much, I appreciate your help :)

Log in to reply