从Qt官方文档学习qml(一) 基于qml的基础布局

平台:qtcreator 5.12.6

取材于:https://doc.qt.io/qt-5.12/qtquick-layouts-example.html 官网列子

/* 第一个列子的说明:
     对象(类型)都是首字母大写(驼峰写法)
     变量(属性)均是首字母小写(驼峰)1
*/

import QtQuick 2.12
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.12

ApplicationWindow {
    visible: true
    title: "Basic layouts"
    property int margin: 11
    width: mainLayout.implicitWidth + 2 * margin
    height: mainLayout.implicitHeight + 2 * margin
//    minimumWidth: mainLayout.Layout.minimumWidth + 2 * margin
//    minimumHeight: mainLayout.Layout.minimumHeight + 2 * margin

/*官方第一个列子,这里不需要写Layout(和以前版本不一致)上为官方*/

    minimumHeight: mainLayout.minimumHeight+2*margin
    minimumWidth: mainLayout.minimumWidth+2*margin

    //主要布局
    ColumnLayout {
        id: mainLayout
        anchors.fill: parent
        anchors.margins: margin

        //列布局
        GroupBox {
            id: rowBox
            title: "Row layout"
            Layout.fillWidth: true              //尽可能的填充宽度(==perfer)

            RowLayout {
                id: rowLayout
                anchors.fill: parent
                TextField {                     //单行文本编辑器
                    placeholderText: "This wants to grow horizontally"
                    Layout.fillWidth: true
                }
                Button {
                    text: "Button"
                }
            }
        }

        GroupBox {
            id: gridBox
            title: "Grid layout"
            Layout.fillWidth: true

            GridLayout {
                id: gridLayout
                rows: 3
                flow: GridLayout.TopToBottom   //流控制(默认从左到右)
                anchors.fill: parent           //具有相同的几何图形

                Label { text: "Line 1" }
                Label { text: "Line 2" }
                Label { text: "Line 3" }

                TextField { }
                TextField { }
                TextField { }

                TextArea {
                    text: "This widget spans over three rows in the GridLayout.\n"
                        + "All items in the GridLayout are implicitly positioned from top to bottom."
                    Layout.rowSpan: 3             //行跨距
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                }
            }
        }
        TextArea {   //多行编辑框
            id: t3
            text: "This fills the whole cell"
            Layout.minimumHeight: 30
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        GroupBox {
            id: stackBox
            title: "Stack layout"
            implicitWidth: 200
            implicitHeight: 60
            Layout.fillWidth: true
            Layout.fillHeight: true
            StackLayout {
                id: stackLayout
                anchors.fill: parent

                //函数
                function advance() { currentIndex = (currentIndex + 1) % count }               //索引和堆的个数

                Repeater {
                    id: stackRepeater
                    model: 5
                    Rectangle {
                        color: Qt.hsla((0.5 + index)/stackRepeater.count, 0.3, 0.7, 1)          //? index 不知道,初步认定是模型中的索引

                        Button { anchors.centerIn: parent; text: "Page " + (index + 1); onClicked: { stackLayout.advance() } }
                    }
                }
            }
        }
    }

   /*自己添加的*/
    menuBar: MenuBar{    //为appwindow的 属性
        Menu{
            title:"文件";
            MenuItem{
                text: "& 打开"
                onTriggered: console.log("it is ");
            }
            MenuItem{
                text: "退出"
                onTriggered: Qt.quit();
            }
        }

    }



}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值