Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to set text to TextInput from C++

    QML and Qt Quick
    3
    6
    2713
    Loading More Posts
    • 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.
    • S
      ssudhindrarao last edited by

      I have multiple QML files (for navigating through the application) I have created one TextInput Qml as a quantity.qml

      For the quantity field i want to sent the data from the weighing scale. When i send a signal from RS232, i am able to catch the signal in the CPP, but i am not able to set the data to my QML TextInput.

      Could anyone help me to set the value for field from the CPP
      I have tried below code, but its not working.
      @
      QtQuick2ApplicationViewer viewer;
      viewer.setMainQmlFile(QStringLiteral("qml/quantity.qml"));

      QObject item = viewer.rootObject()->findChild<QObject>("txtQty");
      item->setProperty("text", serialPort->readData());
      @
      Thanks in advance

      1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        How is it not working? Crash? Or nothing happens? Have you set the objectName property of your TextInput item to txtQty?

        The approach you are using is valid and should work. There are many other ways in which this can be done in Qt. You can, for example, expose your RS232 reading class (or some interface to it) to QML, or add it as a singleton, or send a signal from C++ and receive it in QML. All depends on which solution you like more, or which suits your use case better.

        (Z(:^

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Using serialPort->readData() like that won't do what you think. Unless it's in a slot connected to serialPort readRead signal.

          A more simple and cleaner way would be to encapsulate your communication with your scale in a dedicated QObject subclass and emit the new value once you've received it. You can then use that "Scale" object from your QML code.

          Hope it helps

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 0
          • S
            ssudhindrarao last edited by

            Thanks

            Yes nothing happens

            I have defined the object name as txtQty

            my quantity qml contains text field as below
            @
            Rectangle{
            id: quantityTextField
            anchors{
            centerIn: parent
            }
            border.color: "black"
            TextInput {
            id: quantityField
            objectName: "txtQty"
            inputMethodHints: Qt.ImhNoPredictiveText
            width: 150
            height: 62
            font{
            bold: true
            pixelSize: 10
            }
            wrapMode: Text.Wrap
            autoScroll: true
            }

            function setQuantity(_sQty) {
                 quantityField.text =_sQty;
                 return ;
             }
            

            }
            @
            Is there any example of
            send a signal from C++ and receive it in QML.

            I am using Qt5.2

            I tried below code
            @
            QtQuick2ApplicationViewer viewer2;
            viewer2.setMainQmlFile(QStringLiteral("qml/quantity.qml"));
            QObject *contentView = qobject_cast<QObject * >(viewer2.rootObject());
            QObject::connect(&svc,SIGNAL(setQuantity(QString)),contentView, SLOT(setQuantity(QString)));
            @

            setQuantity Signal is triggering in the cpp but its not going to javascript function(SLOT)

            1 Reply Last reply Reply Quote 0
            • sierdzio
              sierdzio Moderators last edited by

              Please wrap your code between '@' tags on this forum, it makes it easier to read. I have already done this to both of your posts.

              (Z(:^

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Yes there is:

                "Extending QML - Signal Support Example"

                in Qt's documentation

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post