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. Pass parameters from QML to C++

Pass parameters from QML to C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 5 Posters 2.0k 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.
  • C Offline
    C Offline
    celica
    wrote on 21 Jan 2020, 15:01 last edited by
    #1

    Hello, I currently have a tumbler built in qml which I can extract the current user set time. I have these 3 parameters (hour, minute, am/pm) printing to the console using this code.

    Button 
    {text: "Set"
    //onClicked: model.submit()
    onClicked: print(hoursTumbler.hourcode, minutesTumbler.mincode, amPmTumbler.ampmcode)							 
    }
    

    My question is how can I pass these 3 parameters back to my C++ code which houses my main application.
    The C++ code currently calls the QML file like this.

    QQuickView* quickView = new QQuickView(QUrl::fromLocalFile(QDir::currentPath() + "\\Resources\\videos\\aos.qml"));
    		// Make the QML view resize when the parent is resized
    		quickView->setResizeMode(QQuickView::SizeRootObjectToView);
    		quickWidgettum = new QWidget;
    		// Add the QML view to my widget layout
    		quickWidgettum = QWidget::createWindowContainer(quickView);
    		quickWidgettum->setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
    		laos->addWidget(quickWidgettum);
    		quickWidgettum->hide();
    
    B 1 Reply Last reply 21 Jan 2020, 15:30
    0
    • C celica
      21 Jan 2020, 15:01

      Hello, I currently have a tumbler built in qml which I can extract the current user set time. I have these 3 parameters (hour, minute, am/pm) printing to the console using this code.

      Button 
      {text: "Set"
      //onClicked: model.submit()
      onClicked: print(hoursTumbler.hourcode, minutesTumbler.mincode, amPmTumbler.ampmcode)							 
      }
      

      My question is how can I pass these 3 parameters back to my C++ code which houses my main application.
      The C++ code currently calls the QML file like this.

      QQuickView* quickView = new QQuickView(QUrl::fromLocalFile(QDir::currentPath() + "\\Resources\\videos\\aos.qml"));
      		// Make the QML view resize when the parent is resized
      		quickView->setResizeMode(QQuickView::SizeRootObjectToView);
      		quickWidgettum = new QWidget;
      		// Add the QML view to my widget layout
      		quickWidgettum = QWidget::createWindowContainer(quickView);
      		quickWidgettum->setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
      		laos->addWidget(quickWidgettum);
      		quickWidgettum->hide();
      
      B Offline
      B Offline
      Bob64
      wrote on 21 Jan 2020, 15:30 last edited by
      #2

      @celica I haven't used QQuickView but from the documentation I would imagine that you do something like this:

      MyApi* api = new MyApi(...);
      
      quickView->engine()->setContextProperty("myApi", api);
      

      where

      #include <QObject>
      
      class MyApi : public QObject
      {
          Q_OBJECT
      public:
          ...
          Q_INVOKABLE virtual void setValue(int i);
         ...
      };
      

      Then in your QML, the identifier myApi is available to you and you should be able to call e.g. myApi.setValue(10) from javascript code.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        celica
        wrote on 22 Jan 2020, 11:49 last edited by
        #3

        Thanks for the input.
        I do not have an engine in my code. So I simply use
        quickViewdate->rootContext()->setContextProperty("myApi", api);

        I get the error ReferenceError: myApi is not defined when I try and call the function to connect.

        K 1 Reply Last reply 22 Jan 2020, 12:03
        0
        • C celica
          22 Jan 2020, 11:49

          Thanks for the input.
          I do not have an engine in my code. So I simply use
          quickViewdate->rootContext()->setContextProperty("myApi", api);

          I get the error ReferenceError: myApi is not defined when I try and call the function to connect.

          K Offline
          K Offline
          KroMignon
          wrote on 22 Jan 2020, 12:03 last edited by
          #4

          @celica I think you need to do this is several steps:

          QQuickView* quickView = new QQuickView()
          quickView->rootContext()->setContextProperty("myApi", api);
          quickView->setSource(QUrl::fromLocalFile(QDir::currentPath() + "\\Resources\\videos\\aos.qml")));
          // More initialization, if required
          ..
          quickView->show();
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          C 1 Reply Last reply 23 Jan 2020, 13:48
          1
          • K KroMignon
            22 Jan 2020, 12:03

            @celica I think you need to do this is several steps:

            QQuickView* quickView = new QQuickView()
            quickView->rootContext()->setContextProperty("myApi", api);
            quickView->setSource(QUrl::fromLocalFile(QDir::currentPath() + "\\Resources\\videos\\aos.qml")));
            // More initialization, if required
            ..
            quickView->show();
            
            C Offline
            C Offline
            celica
            wrote on 23 Jan 2020, 13:48 last edited by
            #5

            @KroMignon Thanks all for your input
            I managed to get it working. This may help someone:
            What I did was within my qml file created these objectname tags for each tumbler:

            Tumbler 
            {
            id: minutesTumbler
            objectName: "min"
            model: 60
            delegate: delegateComponent
            readonly property int mincode: minutesTumbler.currentIndex
            }
            

            Then from within c++ ->

            QQuickItem* object = quickView->rootObject();
            QObject *v_hour = object->findChild<QObject*>("hour");
            double myhour = v_hour->property("hourcode").toDouble();
            qDebug() << "Tumbler Data: " << myhour;
            
            P O 2 Replies Last reply 23 Jan 2020, 15:07
            1
            • C celica
              23 Jan 2020, 13:48

              @KroMignon Thanks all for your input
              I managed to get it working. This may help someone:
              What I did was within my qml file created these objectname tags for each tumbler:

              Tumbler 
              {
              id: minutesTumbler
              objectName: "min"
              model: 60
              delegate: delegateComponent
              readonly property int mincode: minutesTumbler.currentIndex
              }
              

              Then from within c++ ->

              QQuickItem* object = quickView->rootObject();
              QObject *v_hour = object->findChild<QObject*>("hour");
              double myhour = v_hour->property("hourcode").toDouble();
              qDebug() << "Tumbler Data: " << myhour;
              
              P Offline
              P Offline
              Pablo J. Rogina
              wrote on 23 Jan 2020, 15:07 last edited by
              #6

              @celica said in Pass parameters from QML to C++:

              I managed to get it working

              Great. So please don't forget to mark your post as solved!

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • C celica
                23 Jan 2020, 13:48

                @KroMignon Thanks all for your input
                I managed to get it working. This may help someone:
                What I did was within my qml file created these objectname tags for each tumbler:

                Tumbler 
                {
                id: minutesTumbler
                objectName: "min"
                model: 60
                delegate: delegateComponent
                readonly property int mincode: minutesTumbler.currentIndex
                }
                

                Then from within c++ ->

                QQuickItem* object = quickView->rootObject();
                QObject *v_hour = object->findChild<QObject*>("hour");
                double myhour = v_hour->property("hourcode").toDouble();
                qDebug() << "Tumbler Data: " << myhour;
                
                O Offline
                O Offline
                ODБOï
                wrote on 23 Jan 2020, 16:41 last edited by ODБOï
                #7

                @celica said in Pass parameters from QML to C++:

                double myhour = v_hour->property("hourcode").toDouble();

                you can ensure conversion happened correctly if you pass a bool to toDouble
                https://doc.qt.io/qt-5/qstring.html#toDouble

                1 Reply Last reply
                1

                1/7

                21 Jan 2020, 15:01

                • Login

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