[英]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 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 is not defined
ReferenceError: foo 未定义
What am I missing?我错过了什么? note: Any better way to do this is very welcome.
注意:非常欢迎任何更好的方法来做到这一点。 I just started learning QML.
我刚开始学习 QML。
Here's the code:这是代码:
main.cpp主文件
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();
}
NameUserInput.h名称用户输入.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 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)
}
}
}
}
If you want to export a QObject then you should use setContextProperty()
, not setProperty()
.如果要导出 QObject,则应使用
setContextProperty()
,而不是setProperty()
。 Also it is not necessary to use QVariant.也没有必要使用 QVariant。
engine.rootContext()->
setContextProperty("foo", &nameUserInput);
On the other hand it is not necessary to use Q_DECLARE_METATYPE, and it is good practice to put Q_PROPERTY in the private section:另一方面,不必使用 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.