Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Get SpinBox value from qml into C++
Forum Updated to NodeBB v4.3 + New Features

Get SpinBox value from qml into C++

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 3 Posters 442 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.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by
    #1

    I want to read a value of SpinBox inside c++ code how would I do it ?

    main.cpp:

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;    
        return app.exec();
    }
    
    

    main.qml:

    SpinBox {
              id: new_slider
              editable: true
              value: 60
              from: 1
              to: 60
              textFromValue: function(value){ return value + " min"; }
    }
    

    what is a signature ?? Lol

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

      Here are some docs on this subject:

      • integrating C++ and QML
      • engine context properties

      (Z(:^

      1 Reply Last reply
      1
      • Pradeep P NP Offline
        Pradeep P NP Offline
        Pradeep P N
        wrote on last edited by Pradeep P N
        #3

        Hi @Ahti

        You can even use public slots: in QML, below is example.

        • main.cpp
            MySpinBoxValue cppClass;
            engine.rootContext()->setContextProperty("cppObject", &cppClass);
        
        • MySpinBoxValue.h
            public slots:
                void spinBoxQmlValue(int value);
        
        • MySpinBoxValue.cpp
            void MySpinBoxValue::spinBoxQmlValue(int value)
            {
               qDebug() << Q_FUNC_INFO << value << endl;
            }
        
        • QML
            SpinBox {
                id: new_slider
                editable: true
                value: 60
                from: 1
                to: 60
        
                // This is to update value when ever there is change
                onValueChanged: {
                    console.log("SP_BOX_VAL FROM QML :: ", value)
                    cppObject.spinBoxQmlValue(value);
                }
                
                // This is to send initial Value to QML on APP Launch.
                Component.onCompleted: {
                    cppObject.spinBoxQmlValue(value);            
                }
            }
        

        Output :

        QML debugging is enabled. Only use this in a safe environment.
        void MySpinBoxValue::spinBoxQmlValue(int) 60 
        
        qml: SP_BOX_VAL FROM QML ::  59
        void MySpinBoxValue::spinBoxQmlValue(int) 59 
        
        qml: SP_BOX_VAL FROM QML ::  58
        void MySpinBoxValue::spinBoxQmlValue(int) 58 
        
        qml: SP_BOX_VAL FROM QML ::  57
        void MySpinBoxValue::spinBoxQmlValue(int) 57 
        
        qml: SP_BOX_VAL FROM QML ::  58
        void MySpinBoxValue::spinBoxQmlValue(int) 58 
        
        qml: SP_BOX_VAL FROM QML ::  59
        void MySpinBoxValue::spinBoxQmlValue(int) 59 
        

        All the best.

        Pradeep Nimbalkar.
        Upvote the answer(s) that helped you to solve the issue...
        Keep code clean.

        1 Reply Last reply
        4

        • Login

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