C++ 双向绑定C++;QML中的模型
我正在努力学习更多关于QtQuick和QML的知识。我现在的目标是了解如何将数据从C++模型绑定到我的视图。到目前为止,我已经能够在QML中设置模型,并从模型中获取数据,但我不知道如何更新数据 如何为我的C++模型设置双向绑定?下面是到目前为止我编写的代码 message.hC++ 双向绑定C++;QML中的模型,c++,qt,qml,qtquick2,C++,Qt,Qml,Qtquick2,我正在努力学习更多关于QtQuick和QML的知识。我现在的目标是了解如何将数据从C++模型绑定到我的视图。到目前为止,我已经能够在QML中设置模型,并从模型中获取数据,但我不知道如何更新数据 如何为我的C++模型设置双向绑定?下面是到目前为止我编写的代码 message.h class Message : public QObject { Q_OBJECT Q_PROPERTY(QString author READ getAuthor WRITE setAuthor NOT
class Message : public QObject
{
Q_OBJECT
Q_PROPERTY(QString author READ getAuthor WRITE setAuthor NOTIFY authorChanged)
Q_PROPERTY(QString message READ getMessage WRITE setMessage NOTIFY messageChanged)
Q_SIGNALS:
void authorChanged(QString author);
void messageChanged(QString message);
public:
Message(QObject *parent = 0);
QString getAuthor();
void setAuthor(QString author);
QString getMessage();
void setMessage(QString message);
private:
QString _author;
QString _message;
};
message.cpp
#include "message.h"
Message::Message(QObject *parent) : QObject(parent)
{
}
QString Message::getAuthor()
{
return _author;
}
void Message::setAuthor(QString author)
{
if(author != _author)
{
_author = author;
emit authorChanged(author);
}
}
QString Message::getMessage()
{
return _message;
}
void Message::setMessage(QString message)
{
if(message != _message)
{
_message = message;
emit messageChanged(message);
}
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "message.h"
int main(int argc, char *argv[])
{
qmlRegisterType<Message>("com.butts.messaging", 1, 0, "Message");
//Message msg = Message();
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import com.butts.messaging 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: "Test"
Message {
id: testMessage
author: "Batman"
message: "Hello World!"
}
Flow {
TextField {
text: testMessage.message
}
Label {
text: testMessage.message
}
}
}
main.cpp
#include "message.h"
Message::Message(QObject *parent) : QObject(parent)
{
}
QString Message::getAuthor()
{
return _author;
}
void Message::setAuthor(QString author)
{
if(author != _author)
{
_author = author;
emit authorChanged(author);
}
}
QString Message::getMessage()
{
return _message;
}
void Message::setMessage(QString message)
{
if(message != _message)
{
_message = message;
emit messageChanged(message);
}
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "message.h"
int main(int argc, char *argv[])
{
qmlRegisterType<Message>("com.butts.messaging", 1, 0, "Message");
//Message msg = Message();
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
#包括
#包括
#包括“message.h”
int main(int argc,char*argv[])
{
qmlRegisterType(“com.butts.messaging”,1,0,“Message”);
//Message msg=Message();
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
engine.load(QUrl(QLatin1String(“qrc:/main.qml”));
返回app.exec();
}
另外,我在这方面是个大傻瓜,所以请随意指出我代码中的任何其他问题(格式、标准等),我需要以某种方式学习lol
编辑1
在阅读了@derM的答案后,我修改了代码以实现我想要的
TextField {
id: editor
//Binding model -> view
text: testMessage.message
//Binding model <- view
Binding {
target: testMessage
property: "message"
value: editor.text
}
}
Label {
id: display
//Binding model -> view
text: testMessage.message
}
TextField{
id:编辑
//绑定模型->视图
文本:testMessage.message
//绑定模型视图
文本:testMessage.message
}
双向绑定在QML中是一个复杂的问题,因为它通常用作某种赋值
因此,如果您使用propertyname:valuetobeboundto
绑定属性,然后再次将某个内容分配给propertyname
,则此绑定将丢失
解决方法有两种:使用-对象或不使用绑定,但手动处理所有属性更改信号(理想情况下,模型会正确发出这些信号)
首先,您可以找到详细的说明
在这里,他们为每个方向使用一个绑定-对象。好的是,通过分配新的绑定
,这些绑定不会被覆盖
考虑:
Row {
spacing: 2
Rectangle {
id: r0
width: 50
height: 30
}
Rectangle {
id: r1
width: 50
height: 30
color: b2.pressed ? 'red' : 'blue'
}
Button {
id: b2
}
Button {
id: b3
onPressed: r1.color = 'black'
onReleased: r1.color = 'green'
}
Binding {
target: r0
property: 'color'
value: b2.pressed ? 'red' : 'blue'
}
Binding {
target: r0
property: 'color'
value: (b3.pressed ? 'black' : 'green')
}
}
在开始时,r1
的值被绑定到b2
的状态,但只要按下一次b3
,r1
将不再通过单击b2
进行更新。对于r0
而言,更新将由两个Binding
-对象完成,因此绑定不会丢失。但是,您可以看到绑定是如何工作的:当按钮的状态发生变化时,绑定将被更新。
因此,b2
的按下和释放将触发信号,这将由第一个Binding
处理,b3
的按下和释放也是如此
现在来看双向绑定。在这里,避免绑定循环很重要
Row {
Button {
id: count0
property int count: 0
onClicked: count += 1
text: count
}
Button {
id: count1
property int count: 0
onClicked: count += 1
text: count
}
Binding {
target: count0
property: 'count'
value: count1.count
}
Binding {
target: count1
property: 'count'
value: count0.count
}
}
虽然这个例子很好。更改count0.count
将触发更改count1.count
。现在它被选中了,如果count0.count
需要更新,但是该值已经是正确的,因此递归结束,并且没有发生绑定循环
将第二个绑定更改为
Binding {
target: count1
property: 'count'
value: count0.count + 1
}
彻底改变了这种情况:现在随着count0.count
的每次更改,count1.count
需要提高。第一个绑定
然后尝试将count0.count
设置为与count1.count
相同的值,但是在另一个绑定
完成后,这两个绑定
都无法满足,也不需要进行任何更改。它将导致一个绑定循环。幸运的是,它们在QML中被很好地检测到,因此避免了锁定
现在只有最后一件事需要注意:
考虑这个组件定义:
// TestObj.qml
Item {
width: 150
height: 40
property alias color: rect.color
Row {
spacing: 10
Rectangle {
id: rect
width: 40
height: 40
radius: 20
color: butt.pressed ? 'green' : 'red'
}
Button {
id: butt
text: 'toggle'
}
}
}
这里我们使用propertyname:valueToBeBoundTo
-语法对color
-属性进行内部绑定。这意味着,color
-属性的任何外部赋值都可能覆盖内部绑定。
将此绑定替换为绑定
-对象,您就可以了
反之亦然:color
外部绑定到某个值,然后在内部处理信号并为其赋值,如果外部绑定不是由binding
-对象创建的,则会丢失
这只是一个概述。还有很多细节可能会改变绑定的行为。但我想我已经展示了如何创建双向绑定,并提到了一些可能遇到的陷阱
这适用于我,使用Qt Quick Controls 2:
TextField {
id: txt
text: testMessage.message
onTextChanged: {
testMesage.message = txt.text
}
}
谢谢@derM这是一个很好的答案,它给了我一个开始的地方。非常感谢!2019年QtWS上有一个关于这个主题的演讲: