当前位置:   article > 正文

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

qml radiobutton

先介绍一下 ExclusiveGroup。

ExclusiveGroup (互斥分组)本身是不可见元素,用于将若干个可选择元素组合在一起, 供用户选择其中的一个选项。你可以在 ExclusiveGroup 对象中定义 RadioButton、CheckBox、Action 等元素,此时不需要设置它们的 exclusiveGroup 属性;也可以定义一个只设置了 id 属性的 ExclusiveGroup 对象,在别处定义 RadioButton、CheckBox、Action 等元素时通过 id 初始化这些元素的 exclusiveGroup 属性。current 属性指向互斥分组中第一个选中的元素。

一、RadioButton

RadioButton用于多选一的场景,使用时需要通过 exclusiveGroup 属性为其指定一个分组。 它也可以和GroupBox结合使用。要使用RadioButton,需要导入Controls模块,这样: import QtQuick.Controls 1.2。

  • text 属性存储单选按钮的文本。

  • 单选按钮还有一个指示选中与否的小图标,一般显示在文本前面。给 style 属性设置自定义的 RadioButtonStyle 对象,可以定制 RadioButton 的外观。 checked 属性指示 RadioButton 是否被选中,也可以设置它来选中或取消选中。

  • hovered 是只读属性,指示鼠标是否悬停在 RadioButton 上。

  • pressed 属性在按钮被按下时为 true;当单选按钮被按下时,activeFocusOnPress 属性为 true,按钮获得焦点。

  • 如果你点击了一个单选按钮,则会触发clicked()信号。


1.1 RadioButtonStyle

RadioButtonStyle 用来定制一个 RadioButton,要使用它需要引入 QtQuick.Controls.Styles 1.x 模块。

  • background 属性定制背景,indicator 定制选中指示图标,label 定制单选按钮的文本,它们的类型都是 Component。

  • spacing 指定图标和文本之间的间隔。

  • control 指向使用 style 的 RadioButton 对象,组件内的对象可以通过 control 访问 RadioButton 的各种属性,如 focus、activeFocus、hovered 等。

下面的实例中我们会使用 RadioButtonStyle 来定制 RadioButton。


1.2 实例:选择你喜欢的手机操作系统

提供一个简单的实例,preferred_mobile_os.qml,内容如下:

  1. import QtQuick 2.2
  2. import QtQuick.Controls 1.2
  3. import QtQuick.Controls.Styles 1.2
  4. Rectangle {
  5. width: 320;
  6. height: 300;
  7. color: "#d0d0d0";
  8. Rectangle {
  9. id: resultHolder;
  10. color: "#a0a0a0";
  11. width: 200;
  12. height: 60;
  13. anchors.centerIn: parent;
  14. visible: false;
  15. z: 2;
  16. opacity: 0.8;
  17. border.width: 2;
  18. border.color: "#808080";
  19. Text {
  20. id: result;
  21. anchors.centerIn: parent;
  22. font.pointSize: 20;
  23. color: "blue";
  24. font.bold: true;
  25. }
  26. }
  27. ExclusiveGroup {
  28. id: mos;
  29. }
  30. Component {
  31. id: radioStyle;
  32. RadioButtonStyle {
  33. indicator: Rectangle {
  34. implicitWidth: 16;
  35. implicitHeight: 12;
  36. radius: 6;
  37. border.color: control.hovered ? "darkblue" : "gray";
  38. border.width: 1;
  39. Rectangle {
  40. anchors.fill: parent;
  41. visible: control.checked;
  42. color: "#0000A0";
  43. radius: 5;
  44. anchors.margins: 3;
  45. }
  46. }
  47. label: Text {
  48. color: control.activeFocus ? "blue" : "black";
  49. text: control.text;
  50. }
  51. }
  52. }
  53. Text {
  54. id: notation;
  55. text: "Please select the best mobile os:"
  56. anchors.top: parent.top;
  57. anchors.topMargin: 16;
  58. anchors.left: parent.left;
  59. anchors.leftMargin: 8;
  60. }
  61. RadioButton {
  62. id: android;
  63. text: "Android";
  64. exclusiveGroup: mos;
  65. anchors.top: notation.bottom;
  66. anchors.topMargin: 4;
  67. anchors.left: notation.left;
  68. anchors.leftMargin: 20;
  69. checked: true;
  70. focus: true;
  71. activeFocusOnPress: true;
  72. style: radioStyle;
  73. onClicked: resultHolder.visible = false;
  74. }
  75. RadioButton {
  76. id: ios;
  77. text: "iOS";
  78. exclusiveGroup: mos;
  79. anchors.top: android.bottom;
  80. anchors.topMargin: 4;
  81. anchors.left: android.left;
  82. activeFocusOnPress: true;
  83. style: radioStyle;
  84. onClicked: resultHolder.visible = false;
  85. }
  86. RadioButton {
  87. id: wp;
  88. text: "Windows Phone";
  89. exclusiveGroup: mos;
  90. anchors.top: ios.bottom;
  91. anchors.topMargin: 4;
  92. anchors.left: android.left;
  93. activeFocusOnPress: true;
  94. style: radioStyle;
  95. onClicked: resultHolder.visible = false;
  96. }
  97. RadioButton {
  98. id: firefox;
  99. text: "Firefox OS";
  100. exclusiveGroup: mos;
  101. anchors.top: wp.bottom;
  102. anchors.topMargin: 4;
  103. anchors.left: android.left;
  104. activeFocusOnPress: true;
  105. style: radioStyle;
  106. onClicked: resultHolder.visible = false;
  107. }
  108. RadioButton {
  109. id: sailfish;
  110. text: "Sailfish OS";
  111. exclusiveGroup: mos;
  112. anchors.top: firefox.bottom;
  113. anchors.topMargin: 4;
  114. anchors.left: android.left;
  115. activeFocusOnPress: true;
  116. style: radioStyle;
  117. onClicked: resultHolder.visible = false;
  118. }
  119. Button {
  120. id: confirm;
  121. text: "Confirm";
  122. anchors.top: sailfish.bottom;
  123. anchors.topMargin: 8;
  124. anchors.left: notation.left;
  125. onClicked: {
  126. result.text = mos.current.text;
  127. resultHolder.visible = true;
  128. }
  129. }
  130. }

实例定义了 5 个 RadioButton,分别代表 5 个移动操作系统,这些单选按钮同属于 mos 这个 ExclusiveGroup。我使用锚布局来安排界面元素的位置。

用于显示结果的 Text 对象处于界面中央,一开始是隐藏的,当点击 “Confirm” 按钮时显示用户的选择结果。当用户点击某个 RadioButton 时触发 clicked 信号,我在 onClicked 信号处理器内隐藏显示结果的 Text 对象。

QML 文件内嵌入了一个 RadioButtonStyle 组件,将选中图标变成了椭圆形,将选中时的文字变成了蓝色。RadioButton 通过 radioStyle 这个id来引用组件。

执行 “qmlscene preferred_mobile_os.qml” 命令,效果如下图所示。

二、CheckBox

CheckBox,复选框,顾名思义,你可以在一组选项中选择一个或多个选项,这些选项之间互不影响。像 RadioButton —样,CheckBox 可以显示一个提示选中与否的小图标,以及一行简单的文本。

相比 RadioButton,CheckBox 多了两个属性:partiallyCheckedEnabled 属性指示是否允许部分选中状态,默认为 false;checkedState 记录选中状态,它的值可能是 Qt.UnChecked、 Qt.Checked 或 Qt.PartiallyChecked。


2.1 CheckBoxStyle

与 RadioButtonStyle 类似,CheckBoxStyle 用来定制 CheckBox。需要注意的是,如果你指定了 exdusiveGroup 属性,那么同属于一个互斥组的复选框, 也可以达到多选一的效果。CheckBoxStyle 的属性与 RadioButtonStyle 几乎完全一样,唯一不同的是 control 属性的类型是 CheckBox。

2.2 实例:那些你喜欢的爱情电影

一个简单的实例,preferred_movies.qml,内容如下:

  1. import QtQuick 2.2
  2. import QtQuick.Controls 1.2
  3. import QtQuick.Controls.Styles 1.2
  4. Rectangle {
  5. width: 320;
  6. height: 300;
  7. color: "#d0d0d0";
  8. Rectangle {
  9. id: resultHolder;
  10. color: "#a0a0a0";
  11. width: 220;
  12. height: 80;
  13. anchors.centerIn: parent;
  14. visible: false;
  15. z: 2;
  16. opacity: 0.8;
  17. border.width: 2;
  18. border.color: "#808080";
  19. radius: 8;
  20. Text {
  21. id: result;
  22. anchors.fill: parent;
  23. anchors.margins: 5;
  24. font.pointSize: 16;
  25. color: "blue";
  26. font.bold: true;
  27. wrapMode: Text.Wrap;
  28. }
  29. }
  30. Component {
  31. id: checkStyle;
  32. CheckBoxStyle {
  33. indicator: Rectangle {
  34. implicitWidth: 14;
  35. implicitHeight: 14;
  36. border.color: control.hovered ? "darkblue" : "gray";
  37. border.width: 1;
  38. Canvas {
  39. anchors.fill: parent;
  40. anchors.margins: 3;
  41. visible: control.checked;
  42. onPaint: {
  43. var ctx = getContext("2d");
  44. ctx.save();
  45. ctx.strokeStyle = "#C00020";
  46. ctx.lineWidth = 2;
  47. ctx.beginPath();
  48. ctx.moveTo(0, 0);
  49. ctx.lineTo(width, height);
  50. ctx.moveTo(0, height);
  51. ctx.lineTo(width, 0);
  52. ctx.stroke();
  53. ctx.restore();
  54. }
  55. }
  56. }
  57. label: Text {
  58. color: control.checked ? "blue" : "black";
  59. text: control.text;
  60. }
  61. }
  62. }
  63. Text {
  64. id: notation;
  65. text: "Please select the best love movies:"
  66. anchors.top: parent.top;
  67. anchors.topMargin: 16;
  68. anchors.left: parent.left;
  69. anchors.leftMargin: 8;
  70. }
  71. Column {
  72. id: movies;
  73. anchors.top: notation.bottom;
  74. anchors.topMargin: 8;
  75. anchors.left: notation.left;
  76. anchors.leftMargin: 20;
  77. spacing: 8;
  78. CheckBox {
  79. text: "廊桥遗梦";
  80. style: checkStyle;
  81. onClicked: resultHolder.visible = false;
  82. }
  83. CheckBox {
  84. text: "人鬼情未了";
  85. style: checkStyle;
  86. onClicked: resultHolder.visible = false;
  87. }
  88. CheckBox {
  89. text: "触不到的恋人";
  90. style: checkStyle;
  91. onClicked: resultHolder.visible = false;
  92. }
  93. CheckBox {
  94. text: "西雅图夜未眠";
  95. style: checkStyle;
  96. onClicked: resultHolder.visible = false;
  97. }
  98. }
  99. Button {
  100. id: confirm;
  101. text: "Confirm";
  102. anchors.top: movies.bottom;
  103. anchors.topMargin: 8;
  104. anchors.left: notation.left;
  105. onClicked: {
  106. var str = new Array();
  107. var index = 0;
  108. var count = movies.children.length;
  109. for(var i = 0; i < count; i++){
  110. if(movies.children[i].checked){
  111. str[index] = movies.children[i].text;
  112. index++;
  113. }
  114. }
  115. if(index > 0){
  116. result.text = str.join();
  117. resultHolder.visible = true;
  118. }
  119. }
  120. }
  121. }

我选择 4 部经典爱情片供用户选择,使用 Row 管理对应的 CheckBox。定义了一个 CheckBoxStyle 组件,将选中图标的选中状态变为方框内嵌红叉,将选中时的文字变成了蓝色。CheckBox 通过 checkStyle 这个 id 来引用组件。

执行 “qmlscene preferred_movies.qml” 命令,效果如下图所示。

三、GroupBox

GmupBox (分组框),用于将其他的窗口部件组合在一起显示,最常用的是将单选按钮或复选框放在分组框中显示,不过也可以将任何控件放在分组框内。使用分组框需要导入 QtQuick.Controls 1.x 模块。

  • 分组框一般在顶部有一个标题(title 属性),说明其用途。默认带有边框,不过可以设置 flat 属性为 true 来去掉左、右、底三条边的边框。

  • GroupBox 本身也支持选中,可以通过 checkable 属性来设置。当你设置 checkable 为 true 时,它的标题栏会出现一个复选框,如果你勾选了它,那么它的子控件就是可选中的,否则它的子控件就不可操作。当分组框可选时,checked 属性保存其选中状态。

  • 分组框的尺寸根据它的孩子们的尺寸计算而来。如果你想使用锚布局来管理分组框的孩子们,则需要显式指定分组框本身的尺寸。

  • contentltem 指向一个 Item 对象,代表分组框的内容区,在分组框内声明的孩子们,它们的父会被自动设置为 contentltem。而如果你动态创建分组框的孩子们,则需要显式地将 contentltem 指定为它们的父。

我们修改 4.2 节的实例,使用分组框将表示电影的 CheckBox 组合在一块。新的 QML 文档是preferred_movies_groupbox.qml,内容如下(注意,我略掉了与 4.2 节相同的部分):

  1. Rectangle {
  2. ...
  3. GroupBox {
  4. id: groupbox;
  5. title: "请选择你最喜欢的爱情电影:";
  6. anchors.top: parent.top;
  7. anchors.topMargin: 8;
  8. anchors.left: parent.left;
  9. anchors.leftMargin: 20;
  10. width: 280;
  11. height: 160;
  12. Column {
  13. id: movies;
  14. anchors.top: parent.top;
  15. anchors.topMargin: 8;
  16. spacing: 8;
  17. CheckBox {
  18. text: "廊桥遗梦";
  19. style: checkStyle;
  20. onClicked: resultHolder.visible = false;
  21. }
  22. CheckBox {
  23. text: "人鬼情未了";
  24. style: checkStyle;
  25. onClicked: resultHolder.visible = false;
  26. }
  27. CheckBox {
  28. text: "触不到的恋人";
  29. style: checkStyle;
  30. onClicked: resultHolder.visible = false;
  31. }
  32. CheckBox {
  33. text: "西雅图夜未眠";
  34. style: checkStyle;
  35. onClicked: resultHolder.visible = false;
  36. }
  37. }
  38. }
  39. Button {
  40. id: confirm;
  41. text: "确认";
  42. anchors.top: groupbox.bottom;
  43. anchors.topMargin: 8;
  44. anchors.left: parent.left;
  45. anchors.leftMargin: 20;
  46. onClicked: {
  47. var str = new Array();
  48. var index = 0;
  49. var count = movies.children.length;
  50. for(var i = 0; i < count; i++){
  51. if(movies.children[i].checked){
  52. str[index] = movies.children[i].text;
  53. index++;
  54. }
  55. }
  56. if(index > 0){
  57. result.text = str.join();
  58. resultHolder.visible = true;
  59. }
  60. }
  61. }
  62. }

JS 折叠 复制 全屏

使用 qmlscene 加载 preferred_movies_groupbox.qml,效果如下图所示。

请对照下图和 4.2 节的图,看看使用分组框的效果与不使用分组框时的效果有何不同。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/992320
推荐阅读
相关标签
  

闽ICP备14008679号