QT QML的元素布局的實現(xiàn)
本文介紹QT QML跨平臺移動APP開發(fā)中的元素布局的相關(guān)問題,先看一張圖,我們來分析一下其中的問題:

這張圖片中,有如下問題:
整體的布局沒有居中顯示
班級名稱:
沒有和 請輸入班級名稱輸入框垂直對齊
和輸入框的距離太遠(yuǎn)
班主任的提示也一樣
最后的Button一行,需求要求右對齊,在QML的程序中沒有實現(xiàn)
代碼修改完以后的效果:

改變寬度試一下:

原代碼說明:
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("QML 元素布局")
InputPage{
// 充滿父類
anchors.fill: parent
// 設(shè)置margins
anchors.margins: 10
}
}
InputPage.qml
import QtQuick 2.0
import QtQuick.Controls 2.12
Page {
// 定義參數(shù),每行的高度
property int rowHeight: 40
// 定義參數(shù),每行中,每列的間距
property int rowSpacing: 8
// 定義一列
Column{
id: column
// 充滿父類Page類
anchors.fill: parent
// 定義Column中,每行Row的間距
spacing: 10
Row{
// 寬度去Page的0.8
width: parent.width * 0.8
height: rowHeight
spacing: rowSpacing
// Row水平居中顯示
anchors.horizontalCenter: parent.horizontalCenter
Label{
text: "班級名稱"
// 定義垂直居中顯示
verticalAlignment: className.verticalAlignment
// 顯示字符,水平靠右顯示
horizontalAlignment: Text.AlignRight
// 設(shè)置寬度,Row的寬度的0.3
width: parent.width * 0.3
height: parent.height
}
TextField{
id: className
placeholderText: "請輸入班級名稱"
// 設(shè)置寬度,Row的寬度的0.60
width: parent.width * 0.60
height: parent.height
}
}
// 同上一行代碼
Row{
width: parent.width * 0.8
height: rowHeight
spacing: rowSpacing
anchors.horizontalCenter: parent.horizontalCenter
Label{
text: "班主任"
verticalAlignment: teacherInChargeClass.verticalAlignment
horizontalAlignment: Text.AlignRight
width: parent.width * 0.3
height: parent.height
}
TextField{
id: teacherInChargeClass
placeholderText: "請輸入班主任姓名"
width: parent.width * 0.6
height: parent.height
}
}
Row{
width: parent.width * 0.8
height: rowHeight
spacing: rowSpacing
anchors.horizontalCenter: parent.horizontalCenter
// 設(shè)置Button一行的左側(cè)的充滿寬度
Label{
text: ""
// 寬度說明
// 上述兩行(班級名稱,班主任)的總寬度是id=column的寬度的0.9倍
// 三個Button的寬度 = b1.width*3
// 三個Button的寬度,其中間的間隔有兩個間隔寬度
// 所以本行的寬度和上兩行的寬度是一致的,這樣就保證了button右對齊的
width: parent.width * 0.9 - b1.width*3 - rowSpacing*2
height: parent.height
}
Button{
id: b1
text: "新增"
width: parent.width * 0.15
height: parent.height
}
Button{
id: b2
text: "保存"
width: parent.width * 0.15
height: parent.height
}
Button{
id: b3
text: "放棄"
width: parent.width * 0.15
height: parent.height
}
}
}
}
參考課程 《QT QML跨平臺移動APP編程》
相關(guān)文章
詳解C++類的成員函數(shù)做友元產(chǎn)生的循環(huán)依賴問題
這篇文章主要為大家詳細(xì)介紹了C++類的成員函數(shù)做友元產(chǎn)生的循環(huán)依賴問題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Qt利用QNetwork實現(xiàn)上傳數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細(xì)介紹了Qt如何利用QNetwork實現(xiàn)上傳數(shù)據(jù)的 功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
一文詳解C語言中的switch語句和while循環(huán)
這篇文章主要給大家詳細(xì)介紹了C語言中的switch語句和while循環(huán),文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12

