Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QObject class with QObject class

    General and Desktop
    3
    4
    976
    Loading More Posts
    • 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.
    • J
      justin1122 last edited by

      I have a QObject class (Main) that has one int member and one string member. The other private member is a class that is also derived from Q_Object (Second). I am having trouble compiling the Main class due to errors related to "synthesized method 'Second::Second(const Second&)' first required here' and such.

      The errors are due to that fact that I cannot copy Q_Object derived classes, but I don't know how to get around this. I thought that my code below is already using pointers to the "Second" QObject class.

      What I'm trying to do is to make a QML widget that has two regular int/string fields plus a QListModel

      Below is my code:

      Main.h

      @
      #ifndef MAIN_H
      #define MAIN_H

      #include <QObject>
      #include "second.h"

      class Main : public QObject
      {
      Q_OBJECT

      Q_PROPERTY(int num READ num WRITE setnum NOTIFY numChanged)
      Q_PROPERTY(QString name READ name WRITE setname NOTIFY nameChanged)
      Q_PROPERTY(Second second READ second WRITE second NOTIFY secondChanged)
      

      public:
      Main(QObject *parent=0);
      Main(const int &num, const QString &name, const Second &second, QObject *parent=0);

      int num() const;
      void setnum(const int &num);
      
      QString name() const;
      void setname(const QString &name);
      
      Second second() const;
      void setsecond(const Second &second);
      

      signals:
      void numChanged();
      void nameChanged();
      void secondChanged();

      private:
      int m_num;
      QString m_name;
      Second m_second;

      };

      #endif // MAIN_H
      @

      Main.cpp

      @
      Main::Main(QObject *parent) :
      QObject(parent)
      {
      }

      Main::Main(const int &num, const QString &name, const Second &second, QObject *parent)
      : QObject(parent), m_num(num), m_name(name), m_second(second)
      {
      }

      int Recipe::num() const
      {
      return m_num;
      }

      void Main::setnum(const int &num)
      {
      if (num != m_num) {
      m_num = num;
      emit numChanged();
      }
      }

      QString Main::name() const
      {
      return m_name;
      }

      void Main::setname(const QString &name)
      {
      if (name != m_name) {
      m_name = name;
      emit nameChanged();
      }
      }

      Second Main::second() const
      {
      return &m_second;
      }

      void Main::setsecond(const Second &second)
      {
      if (second != m_second) {
      m_second = second;
      emit secondChanged();
      / }
      }@

      1 Reply Last reply Reply Quote 0
      • CKurdu
        CKurdu last edited by

        You must use pointer for QObjects.
        in main.h
        for example
        @
        line 13 : Q_PROPERTY(Second* second READ second WRITE setsecond NOTIFY secondChanged)

        line 36 : Second * m_second;

        //main.cpp
        Second* Main::second() const
        {
        return m_second;
        }

        void Main::setsecond(Second *second)
        {
        if (second != m_second) {
        m_second = second;
        emit secondChanged();
        / }
        }
        @

        You reap what you sow it

        1 Reply Last reply Reply Quote 0
        • J
          justin1122 last edited by

          Cheers mate! Worked liked a charm!

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            To add to CKurdu, QObject and QObject derived class are not copyable. That's why it didn't work. On a side note, don't forget to handle the lifetime of m_second properly.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • First post
              Last post