我正在尝试制作与 wpf 堆栈面板等效的东西,我已经有了一个逻辑并实现了它,但是宽度有问题,我不知道如何在不进入宽度循环绑定的情况下创建新组件,这是我的堆栈面板:
堆栈面板.qml
import QtQuick 2.12
import QtQuick.Controls 2.12
import KiMa.Models 1.0
Item {
id:root
property var orientation : UOrientation.Horizontal
property int itemSpacing : 10
default property list<Item> pageData
Loader{
property var childs
anchors.fill: parent
id:loader
onChildsChanged: {
if(root.pageData != null){
for(var z = 0;z<root.pageData.length;++z){
root.pageData[z].parent = loader.childs
}
}
}
}
state: orientation == UOrientation.Horizontal ? "row": "col"
states: [
State {
name: "row"
PropertyChanges {
target: loader
sourceComponent : row
}
},
State{
name: "col"
PropertyChanges {
target: loader
sourceComponent : col
}
}
]
Component{
id:col
Column{
Component.onCompleted: {
childs = _col;
}
id:_col
width: parent.width
spacing: root.itemSpacing
}
}
Component{
id:row
Row{
Component.onCompleted: {
childs = _row
}
id:_row
width: parent.width
layoutDirection: Qt.RightToLeft
spacing: root.itemSpacing
}
}
}
我的方向枚举是这样的:
#ifndef UORIENTATION_H
#define UORIENTATION_H
#include<QObject>
class UOrientation
{
Q_GADGET
public:
explicit UOrientation();
enum Orientation{
Horizontal,
Vertical
};
Q_ENUM(Orientation)
};
#endif // UORIENTATION_H
和使用示例应该是这样的:
StackPanel{
x: 320
height: 50
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
anchors.bottomMargin: 25
Button{
}
Button{
}
}
您需要将其添加到main.cpp 中:
qmlRegisterUncreatableType<UOrientation>("KiMa.Models",1,0,"UOrientation","its not creatable type!");
此代码正在运行,如果您有任何建议我进行更改,或者您认为我犯了一个错误,请告诉我,我在这里看到的唯一问题是宽度绑定。
我已经尝试使用childrenRect
但它不起作用:
width: childrenRect.width
height: childrenRect.height
注意:stackpanel 允许您将项目一个接一个地堆叠在一起,您可以将方向设置为水平或垂直,以便在 qt 中将它的一列和一行放在一起,我已经做到了。
纵向一:横向一:
您可以Grid
通过设置columns
. 如果您想要一个单独的组件,您可以使用以下内容创建您的 StackPanel.qml:
import QtQuick 2.0
Grid {
property int orientation: Qt.Horizontal
columns: orientation === Qt.Horizontal ? -1 : 1
}
如果您想要一个可滚动的对象,您也可以将 aListView
与ObjectModel
模型一起使用。ListView
有一个orientation
属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句