一、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. 基于cephfs搭建高可用分布式存储并mount到本地

    原文:https://www.fullstackmemo.com/2018/10/11/cephfs-ha-mount-storage/ 服务器硬件配置及环境 项目 说明 CPU 1核 内存 1GB ...

  2. IP 跟踪

    #coding=utf-8import sysimport os import re import urllibimport subprocess def getlocation(ip): resul ...

  3. postman强大的团队协作功能

    今天无意在调项目的接口 ,使用的postman工具 ,自己写的接口信息,只能自己看 ,感觉有点不方便,如果一个公司多名测试,如果一个人写接口信息,大家都能用,就会很节约时间 所以团队协作的功能就诞生了 ...

  4. iconfont采坑

    1. iconfont采坑 1.1. 前言 使用iconfont过程中踩过坑特此记录 不知道iconfont的这里也简单介绍一笔,阿里开放的一个图标素材库,用来快速找图标下载使用图标 iconfont ...

  5. 倒计时3天!i春秋四周年盛典狂欢,钜惠不停

    六月注定是不平凡的 感恩父亲节 父爱如山亦如海 难忘毕业季 青春无悔不散场 嗨购618 优惠福利送不停 更值得期待的是 在这个不平凡的六月 迎来了i春秋四周年庆典 当周年庆遇到618 会擦出怎样的火花 ...

  6. SQLMAP 速查手册

    /pentest/database/sqlmap/txt/ common-columns.txt 字段字典 common-outputs.txt common-tables.txt 表字典 keywo ...

  7. Django 练习班级管理系统八 -- 上传文件

    Form表单上传文件 修改 views.py import os def upload(request): if request.method == 'GET': img_list = models. ...

  8. day05 作业

    猜年龄 ''' 输入姑娘的年龄后,进行以下判断: 1. 如果姑娘小于18岁,打印"不接受未成年" 2. 如果姑娘大于18岁小于25岁,打印"心动表白" 3. 如 ...

  9. JSON Web Token 使用详解

    JWT 是什么? JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案.它是有三部分组成,示例如下,具体的讲解如下(jwt 是不会有空行的,下面只是为了显示,便使用了换行看着比较 ...

  10. RabbitMQ的几个常见问题

    1. 如何保证消息尽量发送成功? 问题描述: 如果没有启动消费者,重启了RabbitMQ服务,队列和消息都会丢失. 解决方案: 针对这个问题,有以下几个机制可以解决: 生产者确认: 持久化: 手动AC ...