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. [Solved] Trigger custom QML handler from c++

[Solved] Trigger custom QML handler from c++

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 1.9k 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.
  • G Offline
    G Offline
    gloww
    wrote on last edited by
    #1

    Hey there,

    maybe it is a really dumb question but i am a bit confused about how signals and slots between c++ and qml work. I like to trigger a Custom event in my QML like following:

    @Text {
    text: applicationData.getCurrentDateTime22()

         Connections {
             target: applicationData
             onImageChanged: console.log("The application data changed!")
         }
     }@
    

    But i am not sure how to trigger "onImageChanged".

    here is some more code:

    @class ApplicationData : public QObject
    {
    Q_OBJECT
    public:
    Q_INVOKABLE QString getCurrentDateTime22(){
    QString out;
    out = "return value";
    return out;
    }
    public slots:
    //void imageChanged2();

    signals:
    void imageChanged();
    };@

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

    QQmlApplicationEngine engine;
    
    ApplicationData data;
    engine.rootContext()->setContextProperty("applicationData", &data);
    
    
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    
    return app.exec();
    

    }@

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

      Hi,

      You can't trigger onImageChanged, it's a handler. All you need to do is trigger the signal imageChanged from C++ as
      @
      emit imageChanged()
      @

      and the corresponding handler should respond.

      157

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gloww
        wrote on last edited by
        #3

        Hi,

        thanks for your answer.
        i tried that from inside the getCurrentDateTime22() function. But i didnt get any output in the console. I tried if the console debugging is active and it worked with an native handler. I dont get error message either. I think i miss something.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gloww
          wrote on last edited by
          #4

          I attached my project as a zip. I dont know if this is common practice here. I hope thats ok. Maybe someone could look inside. I think this is easier than posting snippets. I tried several things now. I am sure i am mising something really stupid, but i cannot find it...

          http://tempsend.com/085AB21465

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            Your connections are not made when the signal is emitted. While binding the text to string, signal is emitted and by that time no connection exist. Hence you had issue.

            I just tried with this.
            @
            MouseArea {
            anchors.fill: parent
            id : m
            onClicked: {
            lal.text = me.getCurrentDateTime22();
            }
            }@

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gloww
              wrote on last edited by
              #6

              Oh yes, that was the missing part.
              now its working and its awsome. Thanks for the help.

              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