QML中利用ColumnLayout 、Rectangle、Grid布局方法:
anchors.fill:parent:锚点布局:填充父窗口
Layout.fillHeight:true 填充高度
Layout.fillWidth:true 填充宽度
布局思路:先用ColumnLayout,然后再用Rectangle划分每个区域,在rectangle中再RowLayout或Grid
进行布局,如果一行的控件比较多建议使用Grid,这里注意一行中的对齐方式的设置,如果想所一行中的所有控件都竖向居中,首先在上一级布局控件中设置竖向居中,然后在本级控件中设置竖向居中,个人一点学习新得,如有不妥欢迎批正!共同学习!
Dialog {
id:dmdialog
width: 959
height: 675
title: "周视图"
visible: false
ColumnLayout{
anchors.fill: parent
Rectangle{
Layout.fillHeight: true
Layout.fillWidth: true
TableView {
id:tbv
anchors.fill: parent
model: dataModel
sortIndicatorVisible:true
TableViewColumn {role: "Id";title: "查询项";width: 100}
TableViewColumn { role: "cameraName"; title: "周一";width: 100}
TableViewColumn { role: "checkPointName"; title: "周二"; width: 100}
TableViewColumn { role: "channelId"; title: "周三";width: 100}
TableViewColumn { role: "containStraight"; title: "周四"; width: 100}
TableViewColumn { role: "containLeft"; title: "周五";width: 100}
TableViewColumn { role: "containRight"; title: "周六"; width: 100}
TableViewColumn { role: "countTime"; title: "周日";width: 150}
headerDelegate: Rectangle{
implicitWidth: 10
implicitHeight: 24
border.color: "white"
color: "gray"
border.width: 1
MouseArea
{
id: mous
anchors.fill: parent
//接收左键和右键
acceptedButtons:Qt.LeftButton | Qt.RightButton;
Text{
id:headertext
anchors.fill: parent
text: styleData.value
color: styleData.pressed ?"blue" : "white"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pointSize: 11
font.bold: true
}
onClicked: {
if (mouse.button === Qt.RightButton)
{
//发射自定义信号
var colId=0;
if(headertext.text=="编号")
{
colId=0;
}
else if(headertext.text=="摄像头名称")
{
colId=1;
}
else if(headertext.text=="检测点名称")
{
colId=2;
}
else if(headertext.text=="通道")
{
colId=3;
}
else if(headertext.text=="直")
{
colId=4;
}
else if(headertext.text=="左")
{
colId=5;
}
else if(headertext.text=="右")
{
colId=6;
}
else if(headertext.text=="时间")
{
colId=7;
}
else if(headertext.text=="计数")
{
colId=8;
}
else if(headertext.text=="速度")
{
colId=9;
}
else if(headertext.text=="车道占有率")
{
colId=10;
}
dataModel.sendSignal(colId);
tbv.model="";
tbv.model=dataModel;
}
}
}
}
rowDelegate: Rectangle{
height: 24
color: styleData.selected?"blue":(styleData.alternate?"#ffffff":"#ffffFF")
}
itemDelegate: Rectangle {
height: 50
color: "transparent"
width: parent.width
Text {
anchors.fill: parent
width: parent.width
color: styleData.textColor
text: styleData.value
horizontalAlignment: Text.AlignRight
verticalAlignment: Text.AlignVCenter
font.pointSize: 10
}
}
}
}
Rectangle{
height: 30
Layout.fillWidth: true
color:"lightblue"
Grid{
Layout.fillWidth: true
height: parent.height
verticalItemAlignment: Grid.AlignVCenter
columns: 7
spacing: 5
leftPadding: 10
rightPadding: 10
Label {
id: cameraNameLabel
width: 70
height: parent.height
verticalAlignment: "AlignVCenter"
text: qsTr("摄像头名称:")
}
ComboBox {
id: cameraNameComboBox
width: 100
height: 22
editable: true
onEditTextChanged: {
//发送信号
var c_result1=dataModel.getPointByCameraName(cameraNameComboBox.editText.trim());
var temp_qml_model1=new Array();
for(var i=0;i<c_result1.length;++i)
{
temp_qml_model1.push(c_result1[i]);
}
checkPointNameComboBox.model=temp_qml_model1;
}
}
Label {
id: checkPointNameLabel
height: parent.height
verticalAlignment: "AlignVCenter"
text: qsTr("检测点名称:")
}
ComboBox {
id: checkPointNameComboBox
width: 100
height: 25
editable: true
}
Label {
id: channelLabel
height: parent.height
verticalAlignment: "AlignVCenter"
text: qsTr("通道:")
}
ComboBox {
id: channelComboBox
width: 100
height: 25
editable: true
}
Button {
id: queryButton
width: 40
height: 25
text: qsTr("查询")
onClicked: {
var cameraName_str=cameraNameComboBox.editText.toString();
var checkPointName_str=checkPointNameComboBox.editText.toString();
var startDateTime_str=Date.fromLocaleString(Qt.locale(), startDateTimeTextField.text, "yyyy-MM-dd hh:mm:ss");
var endDateTime_str=Date.fromLocaleString(Qt.locale(), endDateTimeTextField.text, "yyyy-MM-dd hh:mm:ss");
var totalIntervalspinbox_str=totalIntervalspinbox.value.toString();
if(startDateTime_str=="Invalid Date"||endDateTime_str=="Invalid Date")
{
promptDlg.title="信息提示";
pDlgText.text="查询时间不能为空!请填写完整!";
promptDlg.visible=true;
}
else
{
if(dateDifference(startDateTime_str,endDateTime_str)>30)
{
promptDlg.title="信息提示";
pDlgText.text="查询范围超限!为了防止查询时间过长影响检测性能!仅能查询30天以内的数据!";
promptDlg.visible=true;
}
else
{
var start_str=startDateTimeTextField.text;
var end_str=endDateTimeTextField.text;
dataModel.sendQuerySignal(cameraName_str,checkPointName_str,start_str,end_str,totalIntervalspinbox_str);
tbv.model="";
tbv.model=dataModel;
}
}
}
//自定义函数
function dateDifference(start,end){
var stime =new Date(start).getTime();
var etime = new Date(end).getTime();
var usedTime = etime - stime; //两个时间戳相差的毫秒数
var days=Math.floor(usedTime/(24*3600*1000));
return days;
}
}
}
}
Rectangle{
height: 30
Layout.fillWidth: true
color:"lightblue"
RowLayout{
anchors.fill: parent
spacing: 15
Label {
id: timelabel
width: 20
leftPadding: 10
Layout.fillHeight: true
verticalAlignment: "AlignVCenter"
text: qsTr("时段")
}
Con25.RangeSlider{
id:timeRangeSlider
Layout.fillWidth: true
Layout.fillHeight: true
from: 1
to: 24
first.value: 1
second.value: 4
stepSize: 0.5
first.onMoved:
{
promptlable.text="当前时段:"+first.value.toFixed(0)+"-"+second.value.toFixed(0)+" 当前周:"+dateSlider.value.toFixed(0);
}
second.onMoved:
{
promptlable.text="当前时段:"+first.value.toFixed(0)+"-"+second.value.toFixed(0)+" 当前周:"+dateSlider.value.toFixed(0);
}
}
}
}
Rectangle{
height: 30
Layout.fillWidth: true
color:"lightblue"
RowLayout{
anchors.fill: parent
spacing: 15
Label {
id: datelabel
width: 20
leftPadding: 10
Layout.fillHeight: true
verticalAlignment: "AlignVCenter"
text: qsTr("日期")
}
Con25.Slider{
id:dateSlider
Layout.fillWidth: true
Layout.fillHeight: true
from: 1
value: 1
stepSize: 1
to: 52
onValueChanged: {
promptlable.text="当前时段:"+timeRangeSlider.first.value.toFixed(0)+"-"+timeRangeSlider.second.value.toFixed(0)+" 当前周:"+dateSlider.value.toFixed(0);
}
}
}
}
Rectangle{
height: 20
Layout.fillWidth: true
color:"red"
RowLayout{
anchors.fill: parent
spacing: 15
Label{
id:promptlable
text: ""
color: "white"
visible: true
leftPadding:10
}
}
}
}
}