C++ 从C++;
我在QML中创建了一个带有一些文本框的QtQuick应用程序。我想使用C++代码中的文本框的值。那么,如何从C++代码中获取这些值? ,可以如下: QML文件:C++ 从C++;,c++,qt,qml,qtquick2,qt-quick,C++,Qt,Qml,Qtquick2,Qt Quick,我在QML中创建了一个带有一些文本框的QtQuick应用程序。我想使用C++代码中的文本框的值。那么,如何从C++代码中获取这些值? ,可以如下: QML文件: Item{ id: root signal textChanged(string msg) TextInput { id: inputText anchors.horizontalCenter: root.horizontalCenter anchors.verticalCenter: root.vertic
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);
}
}
}
ِ您可以将qml的信号连接到cpp中的某个插槽,如:
QObject::connect((QObject *)viewer.rootObject(), SIGNAL(textChanged(QString)), this, SLOT(someSlot(QString)));
我相信你有一些C++类,它被用作一个数据容器。您正在将QML用于UI目的。任何用户通过QML UI输入的数据都必须存储在C++容器中。 有两种方法可以实现它
1)创建和管理C++中的对象,并使用<代码> SETCONTXEXPRESS()/<代码>方法< /P>将对象暴露给QML < p > 2)在C++中创建类,并将该类登记为QML类型,以使用Que>QMLReavestType < /Cuff>函数,然后从QML边ItSelf
管理对象 此示例包含一个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;
};
查看如何将员工
用作QML类型。您还可以创建一个C++代码对象:代码>雇员< /Cord>,并使用<代码> SETCONTeXeField< /Cord>将其设置为上下文属性,并编辑该对象。选择权在你
#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;
}
}
#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();
}
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
}
}