3

I'm trying to get user input from qml TextField to c++, but it only works if text property value is hardcoded (const value).

c++

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

QObject *rootObject = engine.rootObjects().first();
QObject *serverField1 = rootObject->findChild<QObject*>("serverField1");

qDebug() << serverField1->property("text"); //Here I expect to get user input

Qml

ApplicationWindow {
    id: applicationWindow
    visible: true
    width: 300
    height: 550

    TextField {
        id: serverField1
        objectName: "serverField1"
        width: 200
        height: 110
//    text: "hardcoded value" //If text is const value, qDebug will get data from this property
    }
}
1
  • @eyllanesc, I need to get text that person write in this TextField, but when I try to get text properety I not get person input, I get text properety that I code in qml firstly Commented May 30, 2018 at 16:41

1 Answer 1

5

You are asking for the text of the TextField when the window is displayed so what you will get is the text you have initially set, what you should do is get it every time it changes. I think that you have some class that will handle some processing with that data, that class will be called Backend, it must inherit from QObject so that it can have a qproperty and embed an object of that class to QML through setContextProperty, so every time it changes the text in QML will also change the text in the Backend class object.

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQmlProperty>

#include <QDebug>

class Backend: public QObject{
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
    QString text() const{
        return mText;
    }
   void setText(const QString &text){
        if(text == mText)
            return;
        mText = text;
        emit textChanged(mText);
    }
signals:
    void textChanged(const QString & text);
private:
    QString mText;
};

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    Backend backend;

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("backend", &backend);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    // test

    QObject::connect(&backend, &Backend::textChanged, [](const QString & text){
        qDebug() << text;
    });
    return app.exec();
}

#include "main.moc"

main.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    TextField {
        id: serverField1
        x: 15
        y: 46
        width: 120
        height: 45
        topPadding: 8
        font.pointSize: 14
        bottomPadding: 16
        placeholderText: "Server Ip"
        renderType: Text.QtRendering
        onTextChanged: backend.text = text
    }
}

You can find the complete code in the following link.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.