Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using C++ signals in QML
Forum Updated to NodeBB v4.3 + New Features

Using C++ signals in QML

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 2.4k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    psedoykin
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      0
      • P Offline
        P Offline
        psedoykin
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @
          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.

          157

          1 Reply Last reply
          0
          • P Offline
            P Offline
            psedoykin
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

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

              157

              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