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. Connect QML signal from delegate to C++ object

Connect QML signal from delegate to C++ object

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 775 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.
  • F Offline
    F Offline
    fede
    wrote on last edited by
    #1

    Hello,
    I have a QML ListView with a button into the delegate Item. I'd like to know if it's possible connect a delegate Item signal to a particular C++ object slot.

    ODБOïO 1 Reply Last reply
    0
    • F fede

      Hello,
      I have a QML ListView with a button into the delegate Item. I'd like to know if it's possible connect a delegate Item signal to a particular C++ object slot.

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @fede hi,
      of course you can do that.
      Create your c++ class (subclass QObject) and the slot you need

      class MyClass: public QObject  
      { 
          Q_OBJECT 
      public: 
          explicit MyClass(QObject *parent = 0); 
        
      signals: 
        
      public slots: 
          void theSlot(){ ... }
      }; 
        
      

      Create an instance of the class in main.cpp and use setContextProperty() methode to make your object reachable in QML
      http://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty

      #include "myclass.h"
        
      int main(int argc, char *argv[]) 
      { 
          QGuiApplication app(argc, argv); 
          MyClass c;  
          QQmlApplicationEngine engine; 
          engine.rootContext()->setContextProperty("myObj", &c);
          engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 
        
          return app.exec(); 
      }
      

      then you can call theSlot()

      ..{
      onClicked: myObj.theSlot()
      }
      
      1 Reply Last reply
      1

      • Login

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