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. Custom QObject derived class as a parent in constructor definition
Forum Update on Monday, May 27th 2025

Custom QObject derived class as a parent in constructor definition

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.5k Views
  • 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.
  • enemyofthedawnE Offline
    enemyofthedawnE Offline
    enemyofthedawn
    wrote on last edited by enemyofthedawn
    #1

    I have a class constructor like this(expecting MainWindow as a parent and deriving from QObject)

    class ActionManager : public QObject
    {
        Q_OBJECT
    public:
        ActionManager(MainWindow *mainWindow, Settings *settings);
    
    ActionManager::ActionManager(MainWindow *mainWindow, Settings *settings) : QObject(mainWindow)
    

    This however, shows an error:

    error: C2664: 'QObject::QObject(const QObject &)': cannot convert argument 1 from 'MainWindow *' to 'QObject *'

    Should I reinterpret_cast this or it's a wrong way of doing this?

    A 1 Reply Last reply
    0
    • enemyofthedawnE enemyofthedawn

      I have a class constructor like this(expecting MainWindow as a parent and deriving from QObject)

      class ActionManager : public QObject
      {
          Q_OBJECT
      public:
          ActionManager(MainWindow *mainWindow, Settings *settings);
      
      ActionManager::ActionManager(MainWindow *mainWindow, Settings *settings) : QObject(mainWindow)
      

      This however, shows an error:

      error: C2664: 'QObject::QObject(const QObject &)': cannot convert argument 1 from 'MainWindow *' to 'QObject *'

      Should I reinterpret_cast this or it's a wrong way of doing this?

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @enemyofthedawn It's the wrong way to do it. Typically your constructor would take either a QWidget * or a QObject * depending on your needs. Since QMainWindow is derived from both of those you can go either way (I would usually do QWidget for a widget since it's derived from QObject).

      So here is how it would normally be:

      // .h
      ActionManager(Settings *settings, QWidget *parent=nullptr);
      
      // .cpp
      ActionManager::ActionManager(Settings *settings, QWidget *parent) : QWidget(parent)
      {
      }
      

      Optionally if you need MainWindow* somewhere else in your ActionManager class (i.e. you are going to store it) then you can go ahead and use a qobject_cast<QWidget*> to pass it to QWidget's constructor.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      enemyofthedawnE 1 Reply Last reply
      4
      • A ambershark

        @enemyofthedawn It's the wrong way to do it. Typically your constructor would take either a QWidget * or a QObject * depending on your needs. Since QMainWindow is derived from both of those you can go either way (I would usually do QWidget for a widget since it's derived from QObject).

        So here is how it would normally be:

        // .h
        ActionManager(Settings *settings, QWidget *parent=nullptr);
        
        // .cpp
        ActionManager::ActionManager(Settings *settings, QWidget *parent) : QWidget(parent)
        {
        }
        

        Optionally if you need MainWindow* somewhere else in your ActionManager class (i.e. you are going to store it) then you can go ahead and use a qobject_cast<QWidget*> to pass it to QWidget's constructor.

        enemyofthedawnE Offline
        enemyofthedawnE Offline
        enemyofthedawn
        wrote on last edited by
        #3

        @ambershark Thanks!

        1 Reply Last reply
        1

        • Login

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