Important: Please read the Qt Code of Conduct -

How to use QObject in derived class

  • I have a MainWindow : public QMainWindow class that instanciates OtherClass : public QObject. I want OtherClass to have MainWindow as its parent, and only be able to be instantiated with a MainWindow. Here's the class I got but it doesn't compile:

    class OtherClass : QObject{
    OtherClass(Ui::MainWindow* parent) :QObject{parent} {}

  • Moderators

    MainWindow and Ui::MainWindow are two different classes. Ui::MainWindow is the generated ui class. It does not derive from QObject and so it can't be used as a parent for QObjects.

    If you want MainWindow to be the parent then say so:

    OtherClass(MainWindow* parent) : QObject{parent} {}

  • Thank you for the reply.
    I used Ui::MainWindow, because it didn't recognize MainWindow. Turns out i had a circular dependency issue with the #include files.

Log in to reply