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. How to set text to TextInput from C++

How to set text to TextInput from C++

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

    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
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0
        • S Offline
          S Offline
          ssudhindrarao
          wrote on last edited by
          #4

          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
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0

              • Login

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