从c++中获取QML编辑框的值
Get the value of QML Editbox from C++
我用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
将其设置为上下文属性,然后编辑该特定对象。
相关文章:
- 从C++实例化QML
- 使用CMake创建QML插件
- QT通过C++添加映射QML项目
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 如何在没有信号的情况下从C++执行QML插槽
- QML按钮点击功能执行顺序
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 建议在运行时将带有类实例的列表从c++导入qml
- Qt Quick-如何仅从c++代码与qml属性交互
- 编辑文件中的文本
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 如何将带有自定义对象的容器从C++传递到QML
- SetDlgItemInt 不会更改嵌入资源的编辑框
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- 如何在 QML 中编辑 QQmlListProperty
- 从qml编辑QObject属性
- 在具有多个视图的 QML 中查看、编辑和更新数据(来自C++),而数据保留在C++(订阅数据)中
- QML TableView + QAbstractTableModel - 如何从 QML 编辑模型数据
- QML/D文本编辑器和基本注意事项
- 从c++中获取QML编辑框的值