嵌套布局的 QML 问题

问题描述 投票:0回答:1

我对嵌套布局有疑问。在下面的 MRE 中,Item1 中的最后一个 ComboBox 正好位于布局的其他两个元素之上。我尝试将“anchors.fill: parent”添加到所有项目的主要布局中,但没有帮助。我还尝试将 ComboBox 单独放在 ColumnLayout 中,或单独放在 Item 中,或放在 Item 内的 ColumnLayout 中,但都没有帮助。这是 MRE:

main.py

import sys
from pathlib import Path
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine


if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    qml_file = Path(__file__).resolve().parent / "main.qml"
    engine.load(qml_file)
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())

main.qml

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

ApplicationWindow {
    id: mainWindow
    width: 900
    height: 600
    visible: true

    StackLayout {
        id: mainStack
        anchors.fill: parent
        anchors.margins: 20

        Item1 {}
    }
}

Item1.qml

Item {

    ColumnLayout {

        GridLayout {
            columns: 2

            Label {
                text: "label"
            }

            Switch {
                id: mode
                text: "switch"
            }
        }

        StackLayout {
            currentIndex: mode.checked ? 1 : 0

            Item2 {}

            Item3 {}
        }

        ComboBox {}
    }
}

Item2.qml

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Item {
    GridLayout {
        columns: 2

        Label {
            text: "label - item 2"
        }

        ComboBox {}
    }
}

Item3.qml

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Item {
    GridLayout {
        columns: 2

        Label {
            text: "label - item 3"
        }

        ComboBox {}
    }
}

我正在使用以下内容:

Python版本:3.11.1

PySide 版本:6.5.0

操作系统:MacOS 13.2.1

qml pyside6
1个回答
0
投票

在 Item1 中的 StackLayout 上设置隐式高度:

implicitHeight: 100

或将其设置为 Item1 中的 StackLayout

Layout.fillHeight: true
© www.soinside.com 2019 - 2024. All rights reserved.