Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use QObject in derived class

How to use QObject in derived class

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 298 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    imaqt 0
    wrote on last edited by imaqt 0
    #1

    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} {}
    }
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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} {}
      
      1 Reply Last reply
      3
      • I Offline
        I Offline
        imaqt 0
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved