Important: Please read the Qt Code of Conduct -

QML Textinput

  • Hallo, I would like to create a textinput and get the values from the user, once user enters the value I wouldlike to use these values in my cpp code. I am stucj with this for the past 10 days. Any kind of help would be appreciated.



  • User enters the value in text input field say some : 100
    Now I neeed to store this value in variable x, and make coressponding change in cpp document. Please help me with the idea and the code

  • Hi Mohamed!

    did you already read this doc?

    its a good introduction about exchanging data between QML and C++!

    Rgds, Steven

  • Yes, I did but this did not help me. Do you have an idea on how to go about with this.. Thanks in advance

  • I have an idea on how to link the date but I need to get the user date from the textinput field which is a big barrier for me, am stuck with this for the past 10 days:(

  • I suggest to derive your C++ class from QObject, and add that class to the Declaratice view.

    MyClass myClass; // <-- Create your class

    QDeclarativeView view;
    view.rootContext()->setContextProperty("myClass", &myClass); // <-- Add class to view


    Within your C++ class, create a slot that accepts a QString as parameter,

    class MyClass : public QObject

    public slots:
    void cppSlot(const QString &value) {
    qDebug() << "Called the C++ slot with value:" << value;

    from within QML, link your textinput to this slot

    // MyItem.qml
    import QtQuick 1.0

    TextInput {
    id: item
    width: 100; height: 20

     onAccepted: myClass.cppSLot(item.text)


    This is a quick draft, but i hope this would make it more clear !?

    Rgrds, Steven

  • I just have to enter the value on the keyboard and this wil work??

  • i guess :) thats how i do it...

    Did you try it already this way?

  • Thanks you Mr. Steven I got an idea now..

  • Yes, I did give it a try before, but I did not include onAccepted attribute, is there any way we can make an UI where the user enters the data once he hits enter the Ui disappears, and takes it to the CPp window.

  • any idea on how to make the changes in qml from cpp--

  • [[blank-post-content-placeholder]]

Log in to reply