我正在尝试将文本字段的值传递给字符串,我相信源是正确的,但是当我将"值"插入文本字段并单击按钮时,它什么也没返回,但是如果我在文本字段{文本:"示例"},它返回:"示例",有什么想法吗?
首页.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()
}
}
}
}
登录.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 ""
我的回答将更深入地尝试解决背景问题,即如何正确从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()
}
}
}
}
完整的示例可以在以下链接中找到
属性名称应该是文本。试试这个:
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;
}
它在这里工作