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. QObject class with QObject class
QtWS25 Last Chance

QObject class with QObject class

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.1k 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.
  • J Offline
    J Offline
    justin1122
    wrote on last edited by
    #1

    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
    0
    • CKurduC Offline
      CKurduC Offline
      CKurdu
      wrote on last edited by
      #2

      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
      0
      • J Offline
        J Offline
        justin1122
        wrote on last edited by
        #3

        Cheers mate! Worked liked a charm!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

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