我正在構建一個 Python + QtQuick 應用程序,它需要一個從QQuickPaintedItem 降級的類。我在管理已繪制項目的布局和大小時遇到了一些麻煩。特別是,我有一個看起來像這樣的應用程序左側有一組按鈕,窗口右側是一個加載程序(這是一個很好的理由,這只是演示問題的最小示例)。主 qml 文件 ( main.qml) 如下所示:import QtQuick 2.7import QtQuick.Controls 2.1import QtQuick.Layouts 1.3ApplicationWindow { id: mainWindow visible: true width: 720 height: 480 title: qsTr("QML Sampler") onClosing: main.close_application() RowLayout { anchors.fill: parent ColumnLayout { id: sideBar Layout.fillHeight: true Layout.preferredWidth: 200 Layout.minimumWidth: 150 Layout.maximumWidth: 350 Button { id: buttonScreen1 text: "Button 1" Layout.fillWidth: true } Button { id: buttonScreen2 text: "Button 2" Layout.fillWidth: true } } Loader { id: contentAreaLoader Layout.fillHeight: true Layout.preferredWidth: 520 Layout.minimumWidth: 300 source: 'loaded_content.qml' } }}加載的內容文件是實際包含自定義類的文件,如下所示 ( loaded_content.qml):import QtQuick 2.7import QtQuick.Controls 2.1import QtQuick.Layouts 1.3import CustomPaintedItem 1.0ColumnLayout { anchors.fill: parent Label { text: "Here's a thing!" } CustomPaintedItem { Layout.fillHeight: true Layout.fillWidth: true }}
1 回答

嚕嚕噠
TA貢獻1784條經驗 獲得超7個贊
在不使用 Loader 的情況下,布局僅處理子項:ColumnLayout自 amaximumWidth建立以來,它占用可用空間,因此它將是無限的(可以等價于 的一切Layout.fillWidth: true)。另一方面,不是 Layout 的 Loader 沒有該大小策略,因此如果您希望它占用所有可用空間,則必須Layout.fillWidth: true明確使用。
Loader {
id: contentAreaLoader
Layout.fillHeight: true
Layout.preferredWidth: 520
Layout.minimumWidth: 300
Layout.fillWidth: true // <----
source: 'loaded_content.qml'
}
添加回答
舉報
0/150
提交
取消