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. Setting up a slot for QmlApplicationViewer

Setting up a slot for QmlApplicationViewer

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

    Hi there,

    I've been trying to find a solution for this the past few hours and I'm kind of stuck here. I have a main.cpp file which loads my qml file like this:

    @QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/test/main.qml"));
    viewer.showExpanded();@

    So nothing special. Also, I have a QThread that's running and which is emitting signals. Now I try to set up a slot (or something else) that calls a function in my qml file. I tried to add a slot into qmlapplicationviewer.h and implemented it in qmlapplicationviewer.cpp like that:

    @void QmlApplicationViewer::zoom(int scale){
    QVariant sc = scale;
    QObject* mainItem = this->findChild<QObject *>("mainItem");
    QMetaObject::invokeMethod(mainItem, "zoomIn", Q_ARG(QVariant, sc));
    }@

    In main.cpp I connect the signal to the slot:

    @QObject::connect( canThread, SIGNAL( zoom(int) ), &viewer, SLOT( zoom(int) ));@

    And this is my main.qml:

    @import QtQuick 1.1

    Rectangle{
    id: mainItem
    objectName: "mainItem"
    ...

    function zoomIn(steps) {
        bg.scale *= 2;
    }
    

    }@

    The signal is emitted correctly by my thread and also the slot gets called. Nevertheless, the function in qml is never executed

    I'm no real c++ crack so maybe someone can figure out where I run into problems.

    Thanks a lot!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

      Hello,

      May i ask you if :
      Did you have only one object of objectName: "mainItem" ?
      Did you check if mainItem is not null?

      dmcr

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TilmanK
        wrote on last edited by
        #3

        There is only one "mainItem", yes. But, the pointer "mainItem" in my slot is indeed 0. But why?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bluestreak
          wrote on last edited by
          #4

          Try naming them differently, it will help the debugging process so you can examine them individually and have better clarity as to what you are looking at and what is causing the problem

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TilmanK
            wrote on last edited by
            #5

            I couldn't figure out how to solve this, so I made a little wordaround.

            I coded a parser class which takes the pointer from:

            @(QObject *)viewer.rootObject();@

            This works when putting all functions to your root object...

            1 Reply Last reply
            0
            • M Offline
              M Offline
              meolic
              wrote on last edited by
              #6

              It seems to me that your solution is
              @
              QMetaObject::invokeMethod(
              (QObject *)viewer->rootObject(), // for this object we will call method
              "test" // name of the method to call
              // without parameters
              );
              @
              @
              PageStackWindow {
              id: mainApp
              ...
              function test() {
              myModel.test()
              }

              ListModel {
                  id: myModel
                  ...
                  function test() {
                      console.log("TEST OK");
                  }
              }
              
              Page {
                  ...
              }
              

              }
              @

              However, I am interested in more elegant solution.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                meolic
                wrote on last edited by
                #7

                OK, the reason for the problem is, that findChild() does not check "id", it looks for "objectName" property. Thanks to http://www.lothlorien.com/kf6gpe/?p=309 . Moreover, invokeMethod() should be called after setMainQmlFile() and must not be trigered from QML (i.e. qml calls C++ function which calls javascript function) before it finishes the initialization (not even from Component.onCompleted)

                @
                QMetaObject::invokeMethod(
                viewer->rootObject()->findChild<QObject *>("myModel"), // QML item
                "test" // method
                // parameters
                );
                @
                @
                PageStackWindow {
                id: mainApp
                ...

                ListModel {
                    id: myModel
                    objectName: myModel
                    ...
                    function test() {
                        console.log("TEST OK");
                    }
                }
                 
                Page {
                    ...
                }
                

                }
                @

                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