我正在尝试将TextField的值传递给String,我相信Source是正确的,但是当我将“value”插入TextField并单击按钮时,它什么也没有返回,但如果我设置了这个值 TextField {Text:“Example”},它返回:“示例”,任何想法?
FirstPage.qml
Item {
Rectangle {
anchors.fill: parent
ColumnLayout {
id: layoutLogin
anchors.centerIn: parent
anchors.margins: 3
spacing: 3
TextField {
objectName: "login"
Layout.fillWidth: true
placeholderText: "Username"
}
TextField {
property string password: text
objectName: "passwordd"
Layout.fillWidth: true
placeholderText: "Password"
echoMode: TextInput.Password
}
Button {
id: proccessButton
text: "Login"
Layout.fillWidth: true
onClicked: Login.test()
}
}
}
}
login.cpp:
Login::Login() {
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:/FirstPage.qml")));
QObject *object = view.rootObject();
QObject *login = object->findChild<QObject*>("login");
QObject *password = object->findChild<QObject*>("password");
login_u = login->property("login").toString();
password_u = password->property("password").toString();}
void Login::test(){
qDebug() << "user:" << login_u;
qDebug() << "password" << password_u;
}
单击“按钮”时的输出:
user: ""
password ""
答案 0 :(得分:1)
我的答案会更深入地尝试解决背景问题,即如何正确地从C ++获取QML数据。
第一项任务是实现一个继承自QObject
的类,并处理用户名和密码属性,如下所示:
在这个类中,我们必须使用Q_PROPERTY
宏公开属性,如果我们想要从QML调用一个函数,那么它必须以Q_INVOKABLE
开头。
#ifndef LOGIN_H
#define LOGIN_H
#include <QObject>
#include <QDebug>
class Login : public QObject
{
Q_OBJECT
Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged)
Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
public:
explicit Login(QObject *parent = nullptr):QObject(parent){
}
Q_INVOKABLE void test(){
qDebug()<<mUsername<<mPassword;
}
QString username() const{
return mUsername;
}
void setUsername(const QString &username){
if(mUsername == username)
return;
mUsername = username;
emit usernameChanged(mUsername);
}
QString password() const{
return mPassword;
}
void setPassword(const QString &password)
{
if(mPassword == password)
return;
mPassword = password;
emit passwordChanged(mPassword);
}
signals:
void usernameChanged(QString username);
void passwordChanged(QString password);
private:
QString mUsername;
QString mPassword;
};
#endif // LOGIN_H
然后我们使用qmlRegisterType在QML旁边注册它,所以现在这是一个QML库
#include "login.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<Login>("com.examples.login", 1, 0, "Login");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
最后我们在qml端使用它们来分配相应的连接
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import com.examples.login 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Login")
Login{
id: login
username: usernameField.text
password: passwordField.text
}
Rectangle {
anchors.fill: parent
ColumnLayout {
id: layoutLogin
anchors.centerIn: parent
anchors.margins: 3
spacing: 3
TextField {
id: usernameField
textColor: "black"
Layout.fillWidth: true
placeholderText: "Username"
}
TextField {
id: passwordField
Layout.fillWidth: true
placeholderText: "Password"
echoMode: TextInput.Password
textColor: "black"
}
Button {
id: proccessButton
text: "Login"
Layout.fillWidth: true
onClicked: login.test()
}
}
}
}
完整示例可在以下link
中找到答案 1 :(得分:0)
我认为属性名称应该是文本。 试试这个:
login_u = login->property("text").toString();
password_u = password->property("text").toString();
我意识到你正在获取Login类的构造函数中的值。 它应该在调用test()时获得。
void Login::test()
{
QQuickItem *object = m_view->rootObject();
QObject *login = object->findChild<QObject*>("login");
QObject *password = object->findChild<QObject*>("password");
login_u = login->property("text").toString();
password_u = password->property("text").toString();
qDebug() << "user:" << login_u;
qDebug() << "password" << password_u;
}
它在这里工作