QML(21)——Layout中的width, height设置技巧

效果展示

保持组件界面原始大小

在这里插入图片描述

组件size固定,spacing自适应

![在这里插入图片描述](https://img-blog.csdnimg.cn/bb5d6c8be658484983a2ddbf36b92e38.png

组件size自适应,spacing固定

在这里插入图片描述

使用技巧总结

优先级

Layout.fillWidth > preferredWidth > width > implicitWidth

需要固定size的自定义组件(button)

内部申明 width/height
外部不要设置Layout.fillWidth:true

需要自适应size的组件

界面设置 anchor.fill: parent
组件设置Layout.fillWidth:true

界面结构

主界面 main.qml

最外围界面

import QtQuick 2.12
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import "./TestWidth"

Window {
    width: 1000
    height: 800
    visible: true
    title: qsTr("Hello World")

    LayoutSpacing{
        anchors.centerIn: parent
    }
}

功能界面 LayoutSpacing.qml

根节点是RowLayout ,子节点有

  • RowLayout : 2 Rectangle
  • Rectangle
  • ComboBox
  • BasicComponent :自定义组件
import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

RowLayout {
    id: root
    spacing: 10
    RowLayout {
        Layout.fillWidth: true
        spacing: 10
        Rectangle {
            color: 'teal'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }

        Rectangle {
            color: 'plum'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
    }

    Rectangle {
        color: 'teal'
        Layout.fillWidth: true // 优先级 > preferredWidth > width
        Layout.preferredWidth: 100
        Layout.preferredHeight: 150
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
    Rectangle {
        color: 'plum'
        Layout.fillWidth: true
        Layout.preferredWidth: 200
        Layout.preferredHeight: 100
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
    
    ComboBox{
        id: basicCombobox
        // 在Layou里面时,width不生效, must use "Layout."
        Layout.preferredWidth: 60
        currentIndex: 0
        model: ["config", " spectrometer"]
        popup.closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
                           | Popup.CloseOnReleaseOutside
        onCurrentIndexChanged: {
            // 初始化时不会触发
            console.log("onCurrentIndexChanged", currentIndex)
            initCycle()
        }
        Component.onCompleted: {
            console.log("basicCombobox.width", basicCombobox.width)
        }
    }

    BasicComponent {
        // 如果内部,外部都没有指定size, 默认会为0
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
}

自定义组件 BasicComponent .qml

import QtQuick 2.15
import QtQuick.Layouts 1.15

Rectangle {
    id: root
    property color recColor: "#e9eed9"
//    implicitWidth: 30
//    implicitHeight: 50

    color: recColor
    width: 100
    height: 100
}

固定组件的size, Layout的spacing

如果想要展示组件的原始大小,不让其随着界面拉伸,同时固定spacing,可以注意以下

  • 对于LayoutSpacing.qml的外部和外部,都不要使用anchors.fill: parent
  • LayoutSpacing的内部根节点,不设定size,默认展示全部组件原始大小
    在这里插入图片描述

main.qml

import QtQuick 2.12
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import "./TestWidth"

Window {
    width: 800
    height: 400
    visible: true
    title: qsTr("Hello World")

    LayoutSpacing{
        anchors.centerIn: parent
    }
}

LayoutSpacing.qml

import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

RowLayout {
    id: root
    // 若对root的size不做任何设定,默认展示全部组件原始大小
    spacing: 10
    RowLayout {
        Layout.fillWidth: true
        spacing: 10
        Rectangle {
            color: 'teal'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            //            Layout.minimumHeight: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
        。。。。。。

固定组件的size, spacing自动拉伸

如果外围界面比内部组件要大,但是希望固定住组件的原始大小,让spacing自动拉伸填充

  • LayoutSpacing设置anchors.fill: parent, 在外部内部都可
  • 界面内部组件不要设置 Layout.fillWidth: true
    在这里插入图片描述

LayoutSpacing.qml

import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

RowLayout {
    id: root
    anchors.fill: parent
    spacing: 10


    RowLayout {
        Layout.fillWidth: true
        spacing: 10
        Rectangle {
            color: 'teal'
//            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }

        Rectangle {
            color: 'plum'
//            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
    }

    Rectangle {
        color: 'teal'
//        Layout.fillWidth: true // 优先级 > preferredWidth > width
        Layout.preferredWidth: 100
        Layout.preferredHeight: 150
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
    Rectangle {
        color: 'plum'
//        Layout.fillWidth: true
        Layout.preferredWidth: 200
        Layout.preferredHeight: 100
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }

    ComboBox{
        id: basicCombobox
        Layout.preferredWidth: 60
        currentIndex: 0
        model: ["config", " spectrometer"]
        popup.closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
                           | Popup.CloseOnReleaseOutside
    }
    
    BasicComponent {
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
}

固定spacing, 组件的size自动拉伸

如果希望组件大小自适应,保证间距spacing固定

  • LayoutSpacing设置anchors.fill: parent, 在外部内部都可
  • 界面内部组件设置 Layout.fillWidth: true
    在这里插入图片描述
    在这里插入图片描述

LayoutSpacing.qml

import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

RowLayout {
    id: root
    anchors.fill: parent
    spacing: 10
    
    RowLayout {
        Layout.fillWidth: true
        spacing: 10
        Rectangle {
            color: 'teal'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }

        Rectangle {
            color: 'plum'
            Layout.fillWidth: true
            Layout.preferredWidth: 100
            Layout.preferredHeight: 100
            Text {
                anchors.centerIn: parent
                text: parent.width + 'x' + parent.height
            }
        }
    }

    Rectangle {
        color: 'teal'
        Layout.fillWidth: true // 优先级 > preferredWidth > width
        Layout.preferredWidth: 100
        Layout.preferredHeight: 150
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
    Rectangle {
        color: 'plum'
        Layout.fillWidth: true
        Layout.preferredWidth: 200
        Layout.preferredHeight: 100
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }

    ComboBox{
        id: basicCombobox
        Layout.preferredWidth: 60
        currentIndex: 0
        model: ["config", " spectrometer"]
        popup.closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside
                           | Popup.CloseOnReleaseOutside
    }
    
    BasicComponent {
        Text {
            anchors.centerIn: parent
            text: parent.width + 'x' + parent.height
        }
    }
}

设置内部单个组件的size

作为Layout成员的组件,设置它的size必须使用Layout的属性,比如

Layout.minimumWidth: 100
Layout.preferredWidth: 200
Layout.preferredHeight: 100
    RowLayout {
        Layout.fillWidth: true
        spacing: 10
	    ComboBox{
	        id: basicCombobox
	        width: 10
	        currentIndex: 0
	        model: ["A", " B"]
	        Component.onCompleted: {
	            console.log("basicCombobox.width", basicCombobox.width)
	        }
	    }
    }
qml: basicCombobox.width 140

使用 Layout.preferredWidth属性,才能生效

    RowLayout {
        Layout.fillWidth: true
        spacing: 10
	    ComboBox{
	        id: basicCombobox
	        Layout.preferredWidth: 60
	        currentIndex: 0
	        model: ["A", " B"]
	        Component.onCompleted: {
	            console.log("basicCombobox.width", basicCombobox.width)
	        }
	    }
    }
qml: basicCombobox.width 60
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用QQuickWidget将QWidget嵌入到QML。具体步骤如下: 1.在QML文件添加一个QQuickWidget控件,用于装载QWidget窗口。 2.在C++代码创建一个QWidget窗口,并将其设置为QQuickWidget的父对象。 3.将QWidget窗口设置为QQuickWidget的源。 下面是一个示例代码: 引用: ```qml import QtQuick 2.0 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 import QtQuick.Window 2.0 import QtQuick.Widgets 1.0 Window { visible: true width: 640 height: 480 title: qsTr("QWidget in QML") QQuickWidget { id: widget width: 200 height: 200 anchors.centerIn: parent } } ``` 引用: ```cpp #include <QApplication> #include <QWidget> #include <QVBoxLayout> #include <QLabel> #include <QQuickWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget *widget = new QWidget(); QVBoxLayout *layout = new QVBoxLayout(widget); QLabel *label = new QLabel("Hello, QWidget!"); layout->addWidget(label); widget->setLayout(layout); QQuickWidget *quickWidget = new QQuickWidget(); quickWidget->setSource(QUrl("qrc:/main.qml")); quickWidget->rootContext()->setContextProperty("widget", widget); quickWidget->show(); return app.exec(); } ``` 在这个示例,我们在QML文件添加了一个QQuickWidget控件,并将其设置为窗口的心。在C++代码,我们创建了一个QWidget窗口,并将其设置为QQuickWidget的父对象。然后,我们将QWidget窗口设置为QQuickWidget的源,并将QWidget窗口的指针传递给QML文件的上下文属性。这样,我们就可以在QML文件使用QWidget窗口了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值