How to set text to TextInput from C++



  • 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


  • Moderators

    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.


  • Lifetime Qt Champion

    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



  • 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)


  • Moderators

    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.


  • Lifetime Qt Champion

    Yes there is:

    "Extending QML - Signal Support Example"

    in Qt's documentation


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.