簡體   English   中英

如何從 C++ 代碼中檢索 QML 的 TextField 中的文本?

[英]How do I retrieve text from QML's TextField from C++ code?

I'm trying to retrieve the user input text value typed in the TextField but QML can't find my object, when I type something, the following error paper on Qt Creator's application output

ReferenceError: foo 未定義

我錯過了什么? 注意:非常歡迎任何更好的方法來做到這一點。 我剛開始學習 QML。

這是代碼:

主文件

int main(int argc, char *argv[])
{
    //qRegisterMetaType<NameUserInput>(NAMEOF(NameUserInput));
    //qRegisterMetaType<NameUserInput*>(NAMEOF(NameUserInput*));
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    NameUserInput nameUserInput;
    QQmlApplicationEngine engine;
    engine.rootContext()->setProperty("foo", //&nameUserInput);
                                     QVariant::fromValue(&nameUserInput));
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);
    return app.exec();
}

名稱用戶輸入.h

class NameUserInput : public QObject
{
    Q_OBJECT
public:
    explicit NameUserInput(QObject *parent = nullptr);
    //NameUserInput(const NameUserInput &other);
    NameUserInput(const QString &text);
    ~NameUserInput() override = default;
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
    QString text() const;
    void setText(const QString &text);
signals:
    void textChanged(const QString &text);
private:
    QString mText;
};

Q_DECLARE_METATYPE(NameUserInput*)

main.qml

    import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import QtQuick.Controls.Styles 1.4

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("This is my application title!")

    ColumnLayout
    {
        id: col1
        spacing: 2

        Rectangle
        {
            width: 100
            Layout.preferredWidth: 40
            Layout.preferredHeight: 40
            Layout.alignment: Qt.AlignLeft
            Text {
                font.pointSize: 20
                id: label1
                text: qsTr("Your name:")
            }
        }

        Rectangle
        {
            width: 320
            Layout.preferredWidth: 40
            Layout.preferredHeight: 40
            Layout.alignment: Qt.AlignLeft
            TextField {
                placeholderText: "this is the default text"
                font.pointSize: 20
                //text: "this is my text"
                id: textEdit1
                onTextChanged: foo.text = text
                background: Rectangle {
                    border.color: "blue"
                    border.width: 3
                    radius: 12
                }
            }
        }

        Rectangle
        {
            width: 100
            Layout.preferredWidth: 40
            Layout.preferredHeight: 40
            Layout.alignment: Qt.AlignLeft
            Button
            {
                text: "Hit me!"
                onClicked: console.log("user input:" + textEdit1.text)
            }
        }
    }
}

如果要導出 QObject,則應使用setContextProperty() ,而不是setProperty() 也沒有必要使用 QVariant。

engine.rootContext()->setContextProperty("foo", &nameUserInput);

另一方面,不必使用 Q_DECLARE_METATYPE,最好將 Q_PROPERTY 放在私有部分中:

#ifndef NAMEUSERINPUT_H
#define NAMEUSERINPUT_H

#include <QObject>

class NameUserInput : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
    explicit NameUserInput(QObject *parent = nullptr);
    NameUserInput(const QString &text);
    ~NameUserInput() override = default;
    QString text() const;
    void setText(const QString &text);
signals:
    void textChanged(const QString &text);
private:
    QString mText;
};


#endif // NAMEUSERINPUT_H

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM