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. Connecting QML and C++

Connecting QML and C++

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 4 Posters 4.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.
  • A Offline
    A Offline
    Ajon2733
    wrote on last edited by
    #1

    How do you connect qml and mainwindows.cpp ?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Google does not bite: "LINK":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        spode
        wrote on last edited by
        #3

        you can in .cpp file, that derives from QWidget:
        @
        QDeclarativeView *view = new QDeclarativeView(this);
        view->setSource("qml/NomeProgramma/qmlfile.qml");
        QObject *obj = view->rootObject();
        connect(obj, SIGNAL(signal_fromQML(QString, int)), this, SLOT(slot_fromCpp(QString, int)));
        obj->setProperty("propertyFromQML", QVariant::fromValue("ValueThatComesFromCppFile"));
        view->show();
        this->show();
        @

        if you want to know something about lists from cpp to qml, you should have a view on qml data model!! ;)

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hornsj2
          wrote on last edited by
          #4

          You can also set context properties and use the Q_INVOKABLE macro in your C++ Q_Object derived class.

          @

             class customObject : public QObject
             {
                  Q_OBJECT
                  public: 
                  ...constructor/destructor/assignmentOperator/copyConstructor...(standard functions you should implement)
          
                  Q_INVOKABLE void someFunction();
          
                  private:
                  ...data
             };
          

          //-----------------------------------------------------------
          QDeclarativeView* view = newQDeclarativeView(this);
          QDeclarativeContext* rootContext = view->rootContext();

             rootContext->setContextProperty("NameViewableFromQML", &customObjectInstance);
          
             view->setSource("Something.qml");
           
            @
          

          Now you can do things like this in QML:

          NameViewableFromQML.someFunction();

          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