Using C++ signals in QML



  • Hi,

    I am trying to use C++ signals with custom object in QML.

    Please see example:

    1. I have class person:

    @ class person : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString myStr READ myStr WRITE setErrorMessage NOTIFY changed)
    public:
    explicit person(QObject *parent = 0);
    person(const person &copy);

    ~person();
    
    QString myStr() const{
        return m_myStr;
    }
    void setErrorMessage(const QString &str);
    

    signals:
    void changed();

    public slots:

    public:
    QString m_myStr;
    }; @

    1. Also I have manager class

    @ class manager : public QObject
    {
    Q_OBJECT
    public:
    explicit manager();
    ~manager();

    signals:
    void changeStr(const person &pr);

    public slots:
    void needChangeStr();
    };

    manager::manager()
    {

    }

    manager::~manager()
    {

    }

    void manager:: needChangeStr()
    {
    person per;
    per.setErrorMessage("test");
    emit changeStr(per);
    } @

    1. In main.cpp I have registerd this classes:

    @ int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    qRegisterMetaType<person>("person");
    
    
    qmlRegisterType<manager>("core.m.com", 1, 0, "Manager");
    qmlRegisterType<person>("core.m.com", 1, 0, "Person");
    
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
    return app.exec&#40;&#41;;
    

    } @

    1. And In qml file I use it:

    @ Manager
    {
    id: man
    onChangeStr:
    {
    console.info(pr.myStr)
    }
    } @

    But I can not get myStr. The following error logs in output:

    qt.winrtrunner.app: undefined.

    So could you help me with it ?
    How should I use C++ signals in qml with custom parameters?


  • Moderators

    Hi and Welcome to Qt Devnet,

    bq. But I can not get myStr. The following error logs in output:
    qt.winrtrunner.app: undefined.

    That is probably because the person object which you are emitting is not the person object which you have registered using qmlRegisterType.

    What are you trying to achieve ? A little bit more explanation would be useful.



  • Hi,

    Thanks for answer.

    I am trying use C++ signals as wrote in:
    http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html
    Please see example in Exposing Signals paragraph:

    @class MessageBoard : public QObject
    {
    Q_OBJECT
    public:
    // ...
    signals:
    void newMessagePosted(const QString &subject);
    };@

    @MessageBoard {
    onNewMessagePosted: console.log("New message received:", subject)
    }@

    But I want to use my custom object instead of QString. For example I use my object person:

    @class MessageBoard : public QObject
    {
    Q_OBJECT
    public:
    // ...
    signals:
    void newMessagePosted(const Person &pr);
    };@

    @MessageBoard {
    onNewMessagePosted: console.log("New message received:", pr.myStr)
    }@

    I registered person using qmlRegisterType. But it is not work and I cannot understand why it is not work.

    If I input log such as console.log(pr) then "QVariant(person)" is shown in console. If I input log such as console.log(pr.myStr) then "undefine" is shown in console. However I used Q_PROPERTY for myStr member of person class: Q_PROPERTY(QString myStr READ myStr WRITE setErrorMessage NOTIFY changed)


  • Moderators

    @
    void manager:: needChangeStr()
    {
    person per;
    per.setErrorMessage("test");
    emit changeStr(per);
    }
    @

    Try creating the person object on heap using new and then emit that object.



  • It is works.
    However why my method is not work?


  • Moderators

    The scope of person object would be limited to just needChangeStr() and would be destroyed when it goes out of scope.


Log in to reply
 

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