从c++中获取QML编辑框的值

Get the value of QML Editbox from C++

本文关键字:编辑 QML c++ 获取      更新时间:2023-10-16

我用QML创建了一个带有一些文本框的QtQuick应用程序。我想在我的c++代码中使用这些文本框的值。那么我如何从c++代码中获取这些值呢?

可以是:

QML文件:

Item{
id: root
signal textChanged(string msg)
TextInput
{
    id: inputText
    anchors.horizontalCenter: root.horizontalCenter
    anchors.verticalCenter: root.verticalCenter
    text : ""
    inputMethodHints: Qt.ImhNoPredictiveText
    selectByMouse: true
    onAccepted: { 
        inputText.focus = false; 
        Qt.inputMethod.hide(); 
        root.textChanged(inputText.text); 
    }
 }
}

ِYou可以将qml的信号连接到cpp中的某个插槽,如:

QObject::connect((QObject *)viewer.rootObject(), SIGNAL(textChanged(QString)), this, SLOT(someSlot(QString)));

我相信你有一些c++类被用作数据容器。您将QML用于UI目的。用户通过QML UI输入的任何数据都必须存储在c++容器中。

有两种方法可以实现它。

1)在c++中创建和管理对象,并使用setContextProperty()方法向QML公开对象

2)在c++中创建一个类,并将其注册为QML类型,使用qmlRegisterType函数在QML端使用,然后从QML端本身管理对象

这个例子包含一个Customer类,它有一个简单的Employee类和一个数据成员名。这个例子演示了上述两种方法。

employee.h头文件

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <QObject>
class Employee : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
    explicit Employee(QObject *parent = 0);
    QString name();
    void setName(const QString &name);
signals:
    void nameChanged();
public slots:
private:
    QString m_name;
};

employee.cpp文件
#include "employee.h"
#include <QDebug>
Employee::Employee(QObject *parent) :
    QObject(parent)
{
}
QString Employee::name()
{
    return m_name;
}
void Employee::setName(const QString &name)
{
    if(m_name!=name)
    {
        m_name=name;
        emit nameChanged();
        qDebug()<<"C++:Name changed->"<<m_name;
    }
}

main.cpp文件
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <employee.h>
#include <QQmlEngine>
#include <QQmlContext>
#include <QtQml>
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    Employee emp;
    viewer.rootContext()->setContextProperty("employee",&emp);
    qmlRegisterType<Employee>("CPP.Mycomponents",1,0,"Employee");
    viewer.setMainQmlFile(QStringLiteral("qml/SO_GetValueOfQMLEditboxFromCpp/main.qml"));
    viewer.showExpanded();
    return app.exec();
}

//主要。qml

import QtQuick 2.0
import CPP.Mycomponents 1.0
Rectangle {
    width: 360
    height: 360
    Rectangle{
        id:rect1
        width:360
        height:50
        color:"transparent"
        border.color: "black"
     TextInput{
         id:contextPropertyInput
         anchors.left: parent.left
         anchors.leftMargin: 5
         width:350
         height:50
         color:"black"
         font.pixelSize: 16
         onTextChanged: employee.name = text
     }
    }
    Rectangle{
        width:360
        height:50
        color:"transparent"
        border.color: "black"
        anchors.top: rect1.bottom
        anchors.topMargin: 10
         TextInput{
             id:customQMLTypeInput
             anchors.left: parent.left
             anchors.leftMargin: 5
             width:360
             height:50
             color:"black"
             font.pixelSize: 16
         }
    }
     Employee{
         id:qmlEmp;
         name:customQMLTypeInput.text
     }
}

查看如何使用Employee作为QML类型。您还可以创建Employee的c++对象,并使用setContextProperty将其设置为上下文属性,然后编辑该特定对象。