查看完整版本: [--
QtQuick在布局中动态添加组件的问题
--]
QTCN开发网
->
Qt QML开发
->
QtQuick在布局中动态添加组件的问题
[打印本页]
登录
->
注册
->
回复主题
->
发表主题
solo_wang
2019-01-30 22:15
QtQuick在布局中动态添加组件的问题
现在有个布局,比如说是Column{},在里面添加createComponent()动态创建的组件,这时候布局失效了,每个新创建的组件都是在(0,0)位置处。代码简化可以大概是下面的样子,有没有大佬给看看是不是我什么地方错了?
比如说有一个qml文件 test.qml
Item
{
id:root
RadioButton{
text:"测试一行"
}
}
// main.qml
ApplicationWindow{
id:root
height:640
width:480
visible:true
title:"测试"
function add(){
var component = Qt.createComponent("test.qml");
if (component.status == Component.Ready) {
var button = component.createObject(column);
}else{
console.log("Component.Error: ",component.errorString());
}
}
Column{
id:column
spacing:10
}
Button{
id:button
anchors{bottom:parent.bottom;bottomMargin:10;}
height:30;width:45;
onClicked:
add()
}
}
maxlogo
2019-01-30 23:12
动态的Column需要使用Repeater,通过Repeater的model添加数据
solo_wang
2019-01-31 07:39
maxlogo
:
动态的Column需要使用Repeater,通过Repeater的model添加数据
(2019-01-30 23:12)
老铁有没有demo来一个
solo_wang
2019-01-31 10:02
// 用Repeator的model添加数据还是不行,Column的布局还是没有生效~~~
Item {
id:root
RadioButton{
text:"测试一行"
}
}
// main.qml
ApplicationWindow{
id:root
height:640
width:480
visible:true
title:"测试"
property variant arr: [] // 存每个动态增加Item,并且作为repeator的model
function add(){
var component = Qt.createComponent("test.qml");
if (component.status == Component.Ready) {
var button = component.createObject(column); //column作为父组件
arr.push(button) // 保存新实例化的item
}else{
console.log("Component.Error: ",component.errorString());
}
}
Column{
id:column
spacing:10
Repeator{
id:repeator
model:arr
}
}
Button{
id:button
anchors{bottom:parent.bottom;bottomMargin:10;}
height:30;width:45;
onClicked:
add()
}
}
ch781609892
2019-04-24 10:45
test.qml中的Item给一个width和height即可
查看完整版本: [--
QtQuick在布局中动态添加组件的问题
--] [--
top
--]
Powered by
phpwind
v8.7
Code ©2003-2011
phpwind
Gzip disabled