无法在 ColumnLayout 中水平居中项目

问题描述

尝试将项目放在 ColumnLayout 中(嵌套在 StackLayout 中),但似乎不起作用。

StackLayout 还包含一个 Canvas,我用它来验证 Layout.fillWidth: true 是否按预期工作。所以我也假设 ColumnLayout 正在填充宽度(即问题不是外部 StackLayout),尽管我不明白它有什么问题。

Animation showing the problem

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.3

Window {
    id: mainWindow
    width: 340
    height: 380
    visible: true
    title: qsTr("Hello")

    TabBar {
        id: tabBar
        width: parent.width
        TabButton {text: "Layout"}
        TabButton {text: "Canvas"}
    }
    
    StackLayout {
        anchors.fill: parent
        anchors.topMargin: tabBar.height
        currentIndex: tabBar.currentIndex
        
        ColumnLayout {
            Layout.fillWidth: true
            spacing: 10
            
            Label {
                Layout.alignment: Qt.AlignCenter
                text: "Regular buttons:"
            }
            
            RowLayout {
                Layout.alignment: Qt.AlignCenter
                spacing: 10
                Button {text: "normal"}
                Button {text: "Highlighted"; highlighted: true}
                DelayButton {text: "Delay"}
            }
            
            Label {
                Layout.alignment: Qt.AlignCenter
                text: "CheckBoxes:"
            }
            
            RowLayout {
                Layout.alignment: Qt.AlignCenter
                spacing: 10
                CheckBox {text: "CheckBox 1"}
                CheckBox {text: "CheckBox 2"}
            }
            
            Label {
                Layout.alignment: Qt.AlignCenter
                text: "Radiobuttons:"
            }
            
            RowLayout {
                Layout.alignment: Qt.AlignCenter
                spacing: 10
                RadioButton {text: "Radiobutton 1"}
                RadioButton {text: "Radiobutton 2"}
            }
            
            Label {
                Layout.alignment: Qt.AlignCenter
                text: "Miscellaneous:"
            }
            
            Switch {
                Layout.alignment: Qt.AlignCenter
                text: "Switch"
            }
        }
        
        Canvas {
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.maximumHeight: 100
            Layout.margins: 2
            onPaint: {
                var ctx = getContext('2d')
                ctx.reset()
                ctx.strokeStyle = 'black'
                ctx.linewidth = 2
                ctx.beginPath()
                ctx.moveto(0,0)
                ctx.lineto(width,height)
                ctx.stroke()
                ctx.beginPath()
                ctx.moveto(width,0)
                ctx.lineto(0,height)
                ctx.stroke()
            }
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)