Search code examples
c++qtqmlqtquick2qt-quick

Get the value of QML Editbox from C++


I have created a QtQuick application with some textboxes in QML. I want to use the value of those textboxes in my c++ code. So how can I fetch those values from C++ code?


Solution

  • It can be like:

    QML file:

    Item{
    id: root
    
    signal textChanged(string msg)
    
    TextInput
    {
        id: inputText
        anchors.horizontalCenter: root.horizontalCenter
        anchors.verticalCenter: root.verticalCenter
    
        text : ""
        inputMethodHints: Qt.ImhNoPredictiveText
        selectByMouse: true
    
        onAccepted: { 
            inputText.focus = false; 
            Qt.inputMethod.hide(); 
            root.textChanged(inputText.text); 
        }
    
     }
    }
    

    ِYou can connect the signal of your qml to some slot in cpp like:

    QObject::connect((QObject *)viewer.rootObject(), SIGNAL(textChanged(QString)), this, SLOT(someSlot(QString)));