• Qt官方示例:Qt Quick Controls - Gallery


    这个示例演示常用的Qt Quick控件。

    主窗口代码:

    1. import QtQuick
    2. import QtQuick.Layouts
    3. import QtQuick.Controls
    4. import Qt.labs.settings
    5. import "." as App
    6. ApplicationWindow
    7. {
    8. id: window
    9. width: 360
    10. height: 520
    11. visible: true
    12. title: "Qt Quick 控件"
    13. function help()
    14. {
    15. let displayingControl = listView.currentIndex !== -1
    16. let currentControlName = displayingControl
    17. ? listView.model.get(listView.currentIndex).title.toLowerCase() : ""
    18. let url = "https://doc.qt.io/qt-5/" + (displayingControl ? "qml-qtquick-controls2-" + currentControlName + ".html"
    19. : "qtquick-controls2-qmlmodule.html");
    20. Qt.openUrlExternally(url)
    21. }
    22. required property var builtInStyles
    23. Settings
    24. {
    25. id: settings
    26. property string style
    27. }
    28. Shortcut
    29. {
    30. sequences: ["Esc", "Back"]
    31. enabled: stackView.depth > 1
    32. onActivated: navigateBackAction.trigger()
    33. }
    34. Shortcut
    35. {
    36. sequence: StandardKey.HelpContents
    37. onActivated: help()
    38. }
    39. Action
    40. {
    41. id: navigateBackAction
    42. icon.name: stackView.depth > 1 ? "back" : "drawer"
    43. onTriggered:
    44. {
    45. if (stackView.depth > 1)
    46. {
    47. stackView.pop()
    48. listView.currentIndex = -1
    49. } else {
    50. drawer.open()
    51. }
    52. }
    53. }
    54. Shortcut
    55. {
    56. sequence: "Menu"
    57. onActivated: optionsMenuAction.trigger()
    58. }
    59. Action
    60. {
    61. id: optionsMenuAction
    62. icon.name: "menu"
    63. onTriggered: optionsMenu.open()
    64. }
    65. header: ToolBar
    66. {
    67. RowLayout
    68. {
    69. spacing: 20
    70. anchors.fill: parent
    71. ToolButton
    72. {
    73. action: navigateBackAction
    74. }
    75. Label
    76. {
    77. id: titleLabel
    78. text: listView.currentItem ? listView.currentItem.text : "Gallery"
    79. font.pixelSize: 20
    80. elide: Label.ElideRight
    81. horizontalAlignment: Qt.AlignHCenter
    82. verticalAlignment: Qt.AlignVCenter
    83. Layout.fillWidth: true
    84. }
    85. ToolButton
    86. {
    87. action: optionsMenuAction
    88. Menu
    89. {
    90. id: optionsMenu
    91. x: parent.width - width
    92. transformOrigin: Menu.TopRight
    93. Action
    94. {
    95. text: "设置"
    96. onTriggered: settingsDialog.open()
    97. }
    98. Action
    99. {
    100. text: "帮助"
    101. onTriggered: help()
    102. }
    103. Action
    104. {
    105. text: "关于"
    106. onTriggered: aboutDialog.open()
    107. }
    108. }
    109. }
    110. }
    111. }
    112. Drawer
    113. {
    114. id: drawer
    115. width: Math.min(window.width, window.height) / 3 * 2
    116. height: window.height
    117. interactive: stackView.depth === 1
    118. ListView
    119. {
    120. id: listView
    121. focus: true
    122. currentIndex: -1
    123. anchors.fill: parent
    124. delegate: ItemDelegate
    125. {
    126. width: listView.width
    127. text: model.title
    128. highlighted: ListView.isCurrentItem
    129. onClicked:
    130. {
    131. listView.currentIndex = index
    132. stackView.push(model.source)
    133. drawer.close()
    134. }
    135. }
    136. model: ListModel
    137. {
    138. ListElement { title: "BusyIndicator"; source: "qrc:/pages/BusyIndicatorPage.qml" }
    139. ListElement { title: "Button"; source: "qrc:/pages/ButtonPage.qml" }
    140. ListElement { title: "CheckBox"; source: "qrc:/pages/CheckBoxPage.qml" }
    141. ListElement { title: "ComboBox"; source: "qrc:/pages/ComboBoxPage.qml" }
    142. ListElement { title: "DelayButton"; source: "qrc:/pages/DelayButtonPage.qml" }
    143. ListElement { title: "Dial"; source: "qrc:/pages/DialPage.qml" }
    144. ListElement { title: "Dialog"; source: "qrc:/pages/DialogPage.qml" }
    145. ListElement { title: "Delegates"; source: "qrc:/pages/DelegatePage.qml" }
    146. ListElement { title: "Frame"; source: "qrc:/pages/FramePage.qml" }
    147. ListElement { title: "GroupBox"; source: "qrc:/pages/GroupBoxPage.qml" }
    148. ListElement { title: "PageIndicator"; source: "qrc:/pages/PageIndicatorPage.qml" }
    149. ListElement { title: "ProgressBar"; source: "qrc:/pages/ProgressBarPage.qml" }
    150. ListElement { title: "RadioButton"; source: "qrc:/pages/RadioButtonPage.qml" }
    151. ListElement { title: "RangeSlider"; source: "qrc:/pages/RangeSliderPage.qml" }
    152. ListElement { title: "ScrollBar"; source: "qrc:/pages/ScrollBarPage.qml" }
    153. ListElement { title: "ScrollIndicator"; source: "qrc:/pages/ScrollIndicatorPage.qml" }
    154. ListElement { title: "Slider"; source: "qrc:/pages/SliderPage.qml" }
    155. ListElement { title: "SpinBox"; source: "qrc:/pages/SpinBoxPage.qml" }
    156. ListElement { title: "StackView"; source: "qrc:/pages/StackViewPage.qml" }
    157. ListElement { title: "SwipeView"; source: "qrc:/pages/SwipeViewPage.qml" }
    158. ListElement { title: "Switch"; source: "qrc:/pages/SwitchPage.qml" }
    159. ListElement { title: "TabBar"; source: "qrc:/pages/TabBarPage.qml" }
    160. ListElement { title: "TextArea"; source: "qrc:/pages/TextAreaPage.qml" }
    161. ListElement { title: "TextField"; source: "qrc:/pages/TextFieldPage.qml" }
    162. ListElement { title: "ToolTip"; source: "qrc:/pages/ToolTipPage.qml" }
    163. ListElement { title: "Tumbler"; source: "qrc:/pages/TumblerPage.qml" }
    164. }
    165. ScrollIndicator.vertical: ScrollIndicator { }
    166. }
    167. }
    168. StackView
    169. {
    170. id: stackView
    171. anchors.fill: parent
    172. initialItem: Pane
    173. {
    174. id: pane
    175. Image
    176. {
    177. id: logo
    178. width: pane.availableWidth / 2
    179. height: pane.availableHeight / 2
    180. anchors.centerIn: parent
    181. anchors.verticalCenterOffset: -50
    182. fillMode: Image.PreserveAspectFit
    183. source: "images/qt-logo.png"
    184. }
    185. Label
    186. {
    187. text: "Qt Quick Controls 提供了一组控件,可用于在 Qt Quick 中构建完整的界面"
    188. anchors.margins: 20
    189. anchors.top: logo.bottom
    190. anchors.left: parent.left
    191. anchors.right: parent.right
    192. anchors.bottom: arrow.top
    193. horizontalAlignment: Label.AlignHCenter
    194. verticalAlignment: Label.AlignVCenter
    195. wrapMode: Label.Wrap
    196. }
    197. Image
    198. {
    199. id: arrow
    200. source: "images/arrow.png"
    201. anchors.left: parent.left
    202. anchors.bottom: parent.bottom
    203. }
    204. }
    205. }
    206. Dialog
    207. {
    208. id: settingsDialog
    209. x: Math.round((window.width - width) / 2)
    210. y: Math.round(window.height / 6)
    211. width: Math.round(Math.min(window.width, window.height) / 3 * 2)
    212. modal: true
    213. focus: true
    214. title: "Settings"
    215. standardButtons: Dialog.Ok | Dialog.Cancel
    216. onAccepted:
    217. {
    218. settings.style = styleBox.displayText
    219. settingsDialog.close()
    220. }
    221. onRejected:
    222. {
    223. styleBox.currentIndex = styleBox.styleIndex
    224. settingsDialog.close()
    225. }
    226. contentItem: ColumnLayout
    227. {
    228. id: settingsColumn
    229. spacing: 20
    230. RowLayout
    231. {
    232. spacing: 10
    233. Label
    234. {
    235. text: "Style:"
    236. }
    237. ComboBox
    238. {
    239. id: styleBox
    240. property int styleIndex: -1
    241. model: window.builtInStyles
    242. Component.onCompleted:
    243. {
    244. styleIndex = find(settings.style, Qt.MatchFixedString)
    245. if (styleIndex !== -1)
    246. currentIndex = styleIndex
    247. }
    248. Layout.fillWidth: true
    249. }
    250. }
    251. Label
    252. {
    253. text: "Restart required"
    254. color: "#e41e25"
    255. opacity: styleBox.currentIndex !== styleBox.styleIndex ? 1.0 : 0.0
    256. horizontalAlignment: Label.AlignHCenter
    257. verticalAlignment: Label.AlignVCenter
    258. Layout.fillWidth: true
    259. Layout.fillHeight: true
    260. }
    261. }
    262. }
    263. Dialog
    264. {
    265. id: aboutDialog
    266. modal: true
    267. focus: true
    268. title: "About"
    269. x: (window.width - width) / 2
    270. y: window.height / 6
    271. width: Math.min(window.width, window.height) / 3 * 2
    272. contentHeight: aboutColumn.height
    273. Column
    274. {
    275. id: aboutColumn
    276. spacing: 20
    277. Label
    278. {
    279. width: aboutDialog.availableWidth
    280. text: "Qt Quick Controls 模块提供了基于 Qt Quick 的下一代用户界面控件。"
    281. wrapMode: Label.Wrap
    282. font.pixelSize: 12
    283. }
    284. Label
    285. {
    286. width: aboutDialog.availableWidth
    287. text: "与 Qt Quick Controls 1 相比,Qt Quick Controls 更简单、更轻量级、更快"
    288. wrapMode: Label.Wrap
    289. font.pixelSize: 12
    290. }
    291. }
    292. }
    293. }

    界面结构:

    左边的侧面板

    1. Drawer
    2. {
    3. id: drawer
    4. width: Math.min(window.width, window.height) / 3 * 2
    5. height: window.height
    6. interactive: stackView.depth === 1
    7. ListView
    8. {
    9. id: listView
    10. focus: true
    11. currentIndex: -1
    12. anchors.fill: parent
    13. delegate: ItemDelegate
    14. {
    15. width: listView.width
    16. text: model.title
    17. highlighted: ListView.isCurrentItem
    18. onClicked:
    19. {
    20. listView.currentIndex = index
    21. stackView.push(model.source)
    22. drawer.close()
    23. }
    24. }
    25. model: ListModel
    26. {
    27. ListElement { title: "BusyIndicator"; source: "qrc:/pages/BusyIndicatorPage.qml" }
    28. ListElement { title: "Button"; source: "qrc:/pages/ButtonPage.qml" }
    29. ListElement { title: "CheckBox"; source: "qrc:/pages/CheckBoxPage.qml" }
    30. ListElement { title: "ComboBox"; source: "qrc:/pages/ComboBoxPage.qml" }
    31. ListElement { title: "DelayButton"; source: "qrc:/pages/DelayButtonPage.qml" }
    32. ListElement { title: "Dial"; source: "qrc:/pages/DialPage.qml" }
    33. ListElement { title: "Dialog"; source: "qrc:/pages/DialogPage.qml" }
    34. ListElement { title: "Delegates"; source: "qrc:/pages/DelegatePage.qml" }
    35. ListElement { title: "Frame"; source: "qrc:/pages/FramePage.qml" }
    36. ListElement { title: "GroupBox"; source: "qrc:/pages/GroupBoxPage.qml" }
    37. ListElement { title: "PageIndicator"; source: "qrc:/pages/PageIndicatorPage.qml" }
    38. ListElement { title: "ProgressBar"; source: "qrc:/pages/ProgressBarPage.qml" }
    39. ListElement { title: "RadioButton"; source: "qrc:/pages/RadioButtonPage.qml" }
    40. ListElement { title: "RangeSlider"; source: "qrc:/pages/RangeSliderPage.qml" }
    41. ListElement { title: "ScrollBar"; source: "qrc:/pages/ScrollBarPage.qml" }
    42. ListElement { title: "ScrollIndicator"; source: "qrc:/pages/ScrollIndicatorPage.qml" }
    43. ListElement { title: "Slider"; source: "qrc:/pages/SliderPage.qml" }
    44. ListElement { title: "SpinBox"; source: "qrc:/pages/SpinBoxPage.qml" }
    45. ListElement { title: "StackView"; source: "qrc:/pages/StackViewPage.qml" }
    46. ListElement { title: "SwipeView"; source: "qrc:/pages/SwipeViewPage.qml" }
    47. ListElement { title: "Switch"; source: "qrc:/pages/SwitchPage.qml" }
    48. ListElement { title: "TabBar"; source: "qrc:/pages/TabBarPage.qml" }
    49. ListElement { title: "TextArea"; source: "qrc:/pages/TextAreaPage.qml" }
    50. ListElement { title: "TextField"; source: "qrc:/pages/TextFieldPage.qml" }
    51. ListElement { title: "ToolTip"; source: "qrc:/pages/ToolTipPage.qml" }
    52. ListElement { title: "Tumbler"; source: "qrc:/pages/TumblerPage.qml" }
    53. }
    54. ScrollIndicator.vertical: ScrollIndicator { }
    55. }
    56. }

    中间放置一个列表视图,视图有一个垂直的指示器。

    当按下视图中的项目时,对应的页面进入栈视图。

    点击工具栏中左边的按钮时,当前页面出栈:

    1. Action
    2. {
    3. id: navigateBackAction
    4. icon.name: stackView.depth > 1 ? "back" : "drawer"
    5. onTriggered:
    6. {
    7. if (stackView.depth > 1)
    8. {
    9. stackView.pop()
    10. listView.currentIndex = -1
    11. } else {
    12. drawer.open()
    13. }
    14. }
    15. }

    切换主题功能

    在载入主界面的qml文件时赋予初始化属性。

    这个示例能值得一说的点就这些。

    涉及到的类型和知识点:

  • 相关阅读:
    spring复习:(61)自定义CustomAutowireConfigurer
    WRFDA资料同化实践技术应用
    人脸比对CNN设计
    【Python基础入门技能树笔记】数据类型-基本数据类型
    渗透实战靶机3wp
    【计算机网络】传输层协议——TCP(中)
    #循循渐进学51单片机#UART串口通信#not.10
    2023年软件测试之功能测试(完整版)
    学内核之八:Linux内核的smp_processor_id是如何实现的
    Promise的面试题考点
  • 原文地址:https://blog.csdn.net/kenfan1647/article/details/125494201