一、ComboBox

ComboBox,即下拉列表框,由一个列表框和一个标签控件(或编辑控件)组成。ComboBox 的下拉列表是使用 Menu 实现的,列表内的每个条目对应一个 Menultem。

  • 弹出下拉列表框后,用户选择列表中的一个条目,此时 currentlndex、currentText 属性就会变化,同时 activated 信号也会发射。

  • ComboBox 的 find() 方法用于查找列表中是否存在指定的字符串,对于可编辑的 ComboBox,向列表中添加条目时可以使用此方法来滤重;textAt() 返回指定索引位置的字符串;selectAll() 可以选中可编辑 ComboBox 的编辑框内的所有文本。

  • count 属性返回ComboBox下拉列表内的条目个数。

  • 下拉列表的数据从 model 属性来。model 可以是一个简单的字符串列表,如 ["TV”,"CD Player”,"Set Top Box" , ’’Router"];也可以是 ListModel,当使用 ListModel 时,可以通过 textRole 属性指定列表条目对应的 model 内的 role,如果不设定, 则默认使用第一个 role。比如下面的 ComboBox 示例使用的 ListModel,内部数据有 text 和 color 两个角色,如果不设定 ComboBox 的 textRole,则会默认使用 model 的 text;如果设定 textRole: "color",则使用 model 的 color 角色。

    ComboBox {
    editable: true;
    model: ListModel {
    ListElement { text: "Banana"; color: "Yellow" }
    }
    textRole: "color";
    }
  • 你可以给 style 属性指定一个 ComboBoxStyle 对象来定制 ComboBox。其实在 ComboBox.qml 中,为 style 指定了 一个动态创建的 ComboBoxStyle 对象,看源码:style: Qt.createComponent(Settings.style + "/ComboBoxStyle.qml", comboBox)

如果你想了解怎样实现一个功能完备的 ComboBoxStyle,可以循着这个线索找到可参考的源码。为了展示 Qt Quick “一切风格皆可定制” 的特色,我们还是来看看 ComboBoxStyle 到底能干什么。

1.1 ComboBoxStyle

先说一 下ComboBox 顶部显示当前条目的那个控件。当 ComboBox 不可编辑时,它是一个 Button,显示 currentText,响应点击弹出下拉列表框。如果你还记得 ButtonStyle 这个对象,应该能够想到这个 Button 的 background 和 label 可以定制。没错,ComboBoxStyle 恰恰有 background 和 label 两个类型为 Component 的属性,就是用来定制背景和文本的。

当 ComboBox 可编辑时,一个 Textlnput 对象叠加到 Button 上面,Textlnput 左边与 Button 左边对齐,右边留一些位置给那个代表 “下拉” 含义的小图标—这个小图标的宽度由 ComboBoxStyle 的 dropDownButtonWidth 属性来决定。

好啦,现在说到了 ComboBoxStyle 的 background、label、dropDownButtonWidth 三个属性。control 指向 ComboBox 实例,而 renderType —般不用设置。

1.2 ComboBox 综合演示

combobox demo.qml 的内容如下:

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2 Rectangle {
width: 480;
height: 360;
color: "#a0b0a0"; Grid {
anchors.fill: parent;
rows: 2;
columns: 2;
rowSpacing: 4;
columnSpacing: 4;
GroupBox {
width: 200;
height: 120;
title: "只读电器";
ComboBox {
anchors.top: parent.top;
anchors.topMargin: 8;
width: parent.width;
model: ["TV" , "CD Player" , "Set Top Box" , "Router"];
}
} GroupBox {
width: 200;
height: 120;
title: "可编辑水果";
ComboBox {
anchors.top: parent.top;
anchors.topMargin: 8;
width: parent.width;
editable: true;
model: ListModel {
ListElement { text: "Banana"; color: "Yellow" }
}
//textRole: "color";
onAccepted: {
if(count < 4 && find(currentText) === -1){
model.append({text: editText});
currentIndex = find(currentText);
if(count === 4){
editable = false;
}
}
}
}
} GroupBox {
width: 200;
height: 120;
title: "定制风格";
ComboBox {
anchors.top: parent.top;
anchors.topMargin: 8;
width: parent.width;
//editable: true;
model: ["Google" , "IBM" , "Digia"];
style: ComboBoxStyle {
dropDownButtonWidth: 20;
background: Rectangle {
implicitHeight: 24;
border.width: control.editable ? 0 : 1;
border.color: (control.hovered || control.pressed)? "blue" : "gray";
color: (control.hovered || control.pressed)? "#e0e0e0" : "#c0c0c0";
Canvas {
width: 16;
height: 18;
anchors.right: parent.right;
anchors.rightMargin: 2;
anchors.top: parent.top;
anchors.topMargin: 1;
onPaint: {
var ctx = getContext("2d");
ctx.save();
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(1, 8);
ctx.lineTo(8, 16);
ctx.lineTo(15, 8);
ctx.stroke();
ctx.restore();
}
}
}
label: Text {
anchors.left: parent.left;
anchors.leftMargin: 2;
width: parent.width - 22;
height: parent.height;
verticalAlignment: Text.AlignVCenter;
horizontalAlignment: Text.AlignHCenter;
text: control.currentText;
color: (control.hovered || control.pressed) ? "blue" : "black";
}
}
}
} GroupBox {
width: 200;
height: 236;
title: "月份输入";
ComboBox {
anchors.top: parent.top;
anchors.topMargin: 8;
width: parent.width;
editable: true;
model: ListModel {
}
validator: IntValidator{ top: 12; bottom: 1; }
onAccepted: {
if(count < 12 && find(currentText) === -1){
model.append({text: editText});
currentIndex = find(currentText);
if(count === 12){
editable = false;
}
}
}
}
}
}
}

下图是使用 qmlscene 记载 combobox_demo.qml 后的效果图。

下图是做了一些操作后的效果图。

我设计了 4 个 ComboBox 对象,把它们分别放在 4 个 GroupBox 里,这样可以利用 GroupBox 的标题和边框,分界清晰。Grid 定位器管理 4 个 GroupBox。

  • 名为 “只读电器” 的 ComboBox,使用一个字符串列表作为 model,最为简单。

  • “可编辑水果” 下拉列表框,使用 ListModel,允许编辑,在 onAccepted 信号处理器内插入用户输入的文本,带滤重功能。当下拉列表内的条目数量达到 4 时禁止编辑。

  • 名为 “月份输入” 的 ComboBox,使用了 IntValidator,只允许输入 1 至 12 这几个月份。其他的与 “可编辑水果” 类似。

二、ProgressBar

ProgressBar 进度条,用途大家都了解,这里就不赘述了。

  • minimumValue 表示最小值;maximumValue 表示最大值;value 是当前值,更新它进度条就会变化。
  • orientation 代表方向,默认取值 Qt.Horizontal,水平方向;要想进度条变垂直,设置其为 Qt. Vertical。
  • style,又见 style,无处不在的 style,它指向一个 ProgressBarStyle 来定制进度条的外观。 indeterminate 属性比较有意思,当你不知道实际进度时,设置它为 true,ProgressBar 就会变身为Busylndicator 了,请稍等,就不告诉你等多久……它的默认值为 false。

2.1 ProgressBarStyle

ProgressBarStyle 的 control 属性指向 ProgressBar 对象。currentProgress 是一个 0〜1 的值,表示当前进度。background 用来绘制进度条的背景,progress 用来绘制进度。

2.2 进度条综合演示

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2 Rectangle {
width: 480;
height: 240;
color: "#a0b0a0"; Row {
anchors.fill: parent;
anchors.margins: 10;
spacing: 8; ProgressBar {
minimumValue: 0;
maximumValue: 100;
value: 40;
width: 150;
height: 20;
Timer {
interval: 1000;
repeat: true;
running: true;
onTriggered: {
if(parent.value < 99.1){
parent.value += 1;
}else{
stop();
}
}
}
} ProgressBar {
orientation: Qt.Vertical;
minimumValue: 0;
maximumValue: 1;
value: 0.2;
width: 20;
height: 200;
Timer {
interval: 1000;
repeat: true;
running: true;
onTriggered: {
parent.value = Math.random();
}
}
} ProgressBar {
minimumValue: 0;
maximumValue: 1;
value: 0.2;
width: 80;
height: 16;
indeterminate: true;
} Item {
width: 180;
height: parent.height; ProgressBar {
id: customProgress;
anchors.centerIn: parent;
minimumValue: 0;
maximumValue: 1;
value: 0.2;
width: 160;
height: 20;
style: ProgressBarStyle {
background: Rectangle {
implicitWidth: 200;
implicitHeight: 20;
border.width: 1;
border.color: control.hovered ? "green" : "gray";
color: "lightgray";
}
progress: Rectangle {
color: "#208020";
}
}
} ProgressBar {
id: percentProgress;
anchors.top: customProgress.bottom;
anchors.topMargin: 4;
anchors.horizontalCenter: parent.horizontalCenter;
minimumValue: 0;
maximumValue: 1;
value: 0.33;
width: 160;
height: 20;
style: ProgressBarStyle {
id: progressStyle;
background: Rectangle {
border.width: 1;
border.color: control.hovered ? "green" : "gray";
color: "lightgray";
}
progress: Rectangle {
color: "#208020";
}
panel: Item {
implicitWidth: 200;
implicitHeight: 20; Loader {
anchors.fill: parent;
sourceComponent: background;
}
Loader {
id: progressLoader;
anchors.top: parent.top;
anchors.left: parent.left;
anchors.bottom: parent.bottom;
anchors.margins: 3;
z: 1;
width: currentProgress * (parent.width - 6);
sourceComponent: progressStyle.progress;
}
Text {
color: "blue";
text: currentProgress * 100 + "%";
z: 2;
anchors.centerIn: parent;
}
}
}
}
}
}
}

设计了 5 个进度条,水平、垂直、忙等待这三个 ProgressBar 对象都很简单。id 为 customProgress 的 ProgressBar 对象只定制了 progress 和 label, 不必说了。

id 为 percentProgress 的 ProgressBar,进度条的填充部分与背景之间留了 3 个像素的空隙, 这是通过设置 progressLoader 的 anchors.margins 实现的。在定制 panel 时,我添加了一个 Text 对象,让它居中显示进度百分比。

下图是执行 “qmlscene progressbar_demo.qml” 命令的效果图。

参考:

《Qt Quick核心编程》第9章

Qt Quick 常用元素:ComboBox(下拉列表) 与 ProgressBar(进度条)的更多相关文章

  1. Qt Quick 常用元素:RadioButton(单选框),CheckBox(复选框) 与 GroupBox(分组框)

    先介绍一下 ExclusiveGroup. ExclusiveGroup (互斥分组)本身是不可见元素,用于将若干个可选择元素组合在一起, 供用户选择其中的一个选项.你可以在 ExclusiveGro ...

  2. Qt Quick 常用元素:Textinput 与 TextEdit 文本编辑框

    一.Textinput Textinput 用于编辑一行文本,类似于 QLineEdit. font 分组属性允许你设置 Textlnput 元素所用字体的各种属性,包括字体族(family).大 小 ...

  3. Qt Quick 常用元素:TabView(选项卡) 与 Slider(滑块)

    一.TabView TabView 可以实现类似 Windows 任务管理器的界面,有人叫 TabView 为标签控件,有人又称之为选项卡控件,我们知道它就是这么个东西就行了.现在来介绍 TabVie ...

  4. Android学习笔记- ProgressBar(进度条)

    本节引言: 本节给大家带来的是Android基本UI控件中的ProgressBar(进度条),ProgressBar的应用场景很多,比如 用户登录时,后台在发请求,以及等待服务器返回信息,这个时候会用 ...

  5. ProgressBar(进度条)、SeekBar(拖动条)与星级评分条(RatingBar)

    1.ProgressBar(进度条) (1)介绍 (2)常用属性 (3)xml代码 <ProgressBar android:id="@+id/progressBar2" s ...

  6. progressbar进度条组件

    Progressbar 进度条组件 通过$.fn.progressbar.fn.defaults重写默认的defaults进度条(progressbar)提供了一种显示长时间操作进度的反馈.进度可被更 ...

  7. 第一百九十八节,jQuery EasyUI,ProgressBar(进度条)组件

    jQuery EasyUI,ProgressBar(进度条)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 ProgressBar(进度条) ...

  8. EasyUI系列学习(八)-ProgressBar(进度条)

    一.创建组件 1.class加载 <div class="easyui-progressbar"></div> 2.js加载 <div id=&quo ...

  9. [转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

随机推荐

  1. C# based on PdfSharp to split pdf files and get MemoryStream C#基于PdfSharp拆分pdf,并生成MemoryStream

    install-package PdfSharp -v 1.51.5185-beta using System; using PdfSharp.Pdf; using System.IO; using ...

  2. Enum.GetValues(),返回System.Array的一个实例

    Array enumData = Enum.GetValues(e.GetType()); Console.WriteLine("This enum has {0} members.&quo ...

  3. git 使用 tortoisegit 解冲突

    git 解冲突需要注意的问题 弄清除冲突双向的修改意图,并在解决冲突时,同时处理两边的意图. 举例说明 A.txt 文件, 在 master 分支上,有一行文字(代码)是这样: 这是一段在 maste ...

  4. maven-dependencies插件的模拟实现

    maven-dependencies插件的作用就是从本地的maven仓库中提取jar包,放到某个文件夹下面.这个功能其实是很简单的. 我在一家银行工作时,公司电脑都无法连外网,所以无法通过maven下 ...

  5. 关于css中布局遇到的一些问题

    现在本人初学网页布局经常遇到一些布局问题比如图片错位. 遇到的问题以及解决方案如下 行内元素有缝隙 块级元素没有缝隙 行内块元素中间会有小缝隙    常见的解决办法就是浮动

  6. CoreData数据库搭建

    1.首先创建父类吧重用的代码写在里边 #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @inte ...

  7. Django models 单表查询

    从数据库中查询出来的结果一般是一个集合,这个集合叫做 QuerySet 1. 查看Django QuerySet执行的SQL .query.__str__()或 .query属性打印执行的sql语句 ...

  8. Django 简单的验证码

    创建一个 Django 项目:yanzhengma 和 应用 app01 修改 urls.py 文件 from django.contrib import admin from django.urls ...

  9. 使用redis实现程序或者服务的高可用

    使用redis实现程序或者服务的高可用,就是将某一程序或服务部署在不同服务器上,或者是跨机房部署,当运行服务的服务器挂了之后,其他服务器上的该服务能立马顶上,这里我简单的使用redis实现这一目的. ...

  10. Linux内核编程、调试技巧小集【转】

    转自:https://www.cnblogs.com/arnoldlu/p/7152488.html 1. 内核中通过lookup_symbol_name获取函数名称 内核中很多结构体成员是函数,有时 ...