我正在做一个项目,基本上我想做的是在2个QML文件的2个框之间同步属性import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
maxLines: 2,
),
],
),
),
);
}
}
(2个框都拥有该属性),我将theProperty
绑定到一个C ++ Q_PROPERTY,因此通过将两个框绑定到相同的C ++ Q_PROPERTY,可以实现同步。
这是我在Box A和B中的代码。theProperty
可以由Box A和B独立更改
theProperty
在cpp中:
Box_A {
id: box_A
// sth
Binding { target:box_A; property: "theProperty"; value:model.CppModel.theProperty }
onThePropertyChanged: {
model.CppModel.theProperty = theProperty
}
}
Box_B {
id: box_B
// sth
Binding { target:box_B; property: "theProperty"; value:model.CppModel.theProperty }
onThePropertyChanged: {
model.CppModel.theProperty = theProperty
}
}
在Box_A和B中,有一个鼠标区域可以通过其更改 Class Item: QObject{
Q_OBJECT
Q_PROPERTY(bool theProperty READ theProperty WRITE theProperty NOTIFY theProperty Changed)
//sth
}
:
theProperty
问题是,一旦我在框A或B中更改了MouseArea{
onClicked: theProperty=!theProperty
}
,qt创建者就会抱怨检测到值theProperty
的循环绑定,另一方面,有一种解决方法这个问题?
答案 0 :(得分:0)
你为什么不这样做:
Box_A {
id: box_A
// sth
theProperty:model.CppModel.theProperty
}
对您不起作用吗?