QObject class with QObject class



  • 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();
    / }
    }@



  • 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();
    / }
    }
    @



  • Cheers mate! Worked liked a charm!


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.