当前位置:网站首页>Rosen's QT journey 100 QML four standard dialog boxes (color, font, file, promotion)
Rosen's QT journey 100 QML four standard dialog boxes (color, font, file, promotion)
2022-07-25 16:38:00 【Allen Roson】
A complete application usually needs to use dialog boxes to complete some interactions . Some dialog boxes provide some general functions , For example, select a file 、 Choose colors, etc . Because this part of the dialog box is the same for every program ,QML Provides a series of predefined dialog boxes , It is called a standard dialog . stay QML in , Using the standard dialog box requires importing Dialogs modular :
import QtQuick. Dialogs 1.1The module provides 4 A standard dialog :ColorDialog、FileDialog、FontDialog and MessageDialog. Let's introduce one by one .
Color dialog
ColorDialog Corresponding Widgets Medium QColorDialog, Dialog box for color selection . In the initial state , The dialog is invisible , You need to set its properties first , And then take it. visible Property is set to true, Or call open() function .
ColorDialog Dialog box that automatically acts as its parent window , Therefore, it is usually displayed in the center of the parent window . As long as the platform supports ,ColorDialog Will use the platform's own color dialog ; otherwise , ColorDialog Will initialize a QColorDialog Display . If even QColorDialog Cannot initialize ,ColorDialog Will display a QML The implementation of the DefaultColorDialog. qml. under these circumstances , By editing the DefaultCol-orDialog.qml You can customize ColorDialog The appearance of the .DefaultColorDialog.qml Contains a for storing the contents of the dialog Rectangle Components , This is to avoid that some embedded systems do not support multiple top-level windows . When the dialog box is visible , If possible , DefaultColorDialog.qml Will be embedded in a Win-dow in ; If the underlying platform only supports one top-level window , In this case , Because the main window is already a top-level window , So the color dialog can no longer become another top-level window , therefore ,QML Choose to re render it in the main window .
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Dialogs 1.2
Window {
id:window
visible: true
width: 200
height: 200
title: qsTr("Hello World")
Button{
text: qsTr("Open Color Dialog")
onClicked:colorDialog.open()
}
ColorDialog{
id:colorDialog
title:qsTr("select color")
onAccepted: window.color = color // Click on ok Execute after button
onRejected: console.log("cilcked cancel") // Click on cancle Execute after button
}
}

Font selection dialog box
FontDialog Corresponding Widgets Medium QFontDialog, Dialog box for font selection , Similar to the previous dialog types .FontDialog Types can be used in open() before , Use font Property to set the initial font , When the dialog box is closed , have access to font To get the selected font type .
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Dialogs 1.2
Window {
id:window
visible: true
width: 200
height: 200
title: qsTr("Hello World")
Button{
text: qsTr("Open Font Dialog")
onClicked:fontDialog.open()
}
FontDialog{
id:fontDialog
title:qsTr("select font")
font:Qt.font({family:"Arial",pointSize:24,weight:Font.Normal})
onAccepted: console.log("selected font is" + fontDialog.font) // Click on ok Execute after button
onRejected: console.log("cilcked cancel") // Click on cancle Execute after button
}
}

File selection dialog
FileDialog Corresponding Widgets Medium QFileDialog, Used to display a dialog box for selecting files . Same as ColorDialog similar , In the initial state , FileDialog It's also invisible . Set some related properties before using , And then take it. visible Property is set to true, Or call open function .FileDialog Other features are similar ColorDialog, No more details here .
Selected file or file list ( When multiple files are selected ) The path can be through fileUrl or fileUrls To get , If you want to select multiple files , Need to set up selectMultiple The attribute is true. If you want to choose a directory instead of a file , You can set selectFolder by true, And then through folder Property to get the selected directory path . If you're calling open() Previously set folder attribute , Then the specified directory will be opened . You can also use nameFilters To set the file name filter , Filters can be used ? and * wildcard , Each string is separated by a space .

Prompt message box
MessageDialog Corresponding C++ Medium QMessageBox, Standard dialog box for displaying some prompt information , Like a warning 、 Errors, etc . And QMessageBox It provides many convenient static functions , Here you need to set one by one MessageDialog Properties of , In order to achieve the desired effect .
Use text Property can set the main displayed content , Use detailedText Property to set the details to be displayed .icon Property is used to set the icon of the dialog , Altogether 5 Kind of :StandardIcon.NoIcon( No icons )、StandardIcon.Question( problem )、StandardIcon.Information( Information )、StandardIcon.Warning( Warning )、StandardIcon. Critical( error ). Use standardButtons Property to set the buttons in the dialog . Each button contains a ButtonRole, Used to determine the type of signal emitted when the button is pressed , for example Ok The button corresponds to AcceptRole, that Ok When the button is pressed, it should be in onAccepted() Perform corresponding operations in the signal processor ; and Help The button corresponds to HelpRole, Then the corresponding is onHelp() Signal processor .
MessageDialog Standard button types in
| The name of the button | describe |
| StandardButton.Ok | Use AcceptRole Defined OK Button |
| StandardButton.Open | Use AcceptRole Defined Open Button |
| StandardButton.Save | Use AcceptRole Defined Save Button |
| StandardButton.Cancel | Use RejectRole Defined Cancel Button |
| StandardButton.Close | Use RejectRole Defined Close Button |
| StandardButton.Discard | Use DestructiveRole Defined Discard or Don't Save Button |
| StandardButton.Apply | Use ApplyRole Defined Apply Button |
| StandardButton.Reset | Use ResetRole Defined Reset Button |
| StandardButton.RestoreDefaults | Use ResetRole Defined Restore Defaults Button |
| StandardButton.Help | Use HelpRole Defined Help Button |
| StandardButton.SaveAll | Use AcceptRole Defined SaveAll Button |
| StandardButton.Yes | Use YesRole Defined Yes Button |
| StandardButton.YesToAll | Use YesRole Defined Yes to All Button |
| StandardButton.No | Use NoRole Defined No Button |
| StandardButton. NoToAll | Use NoRole Defined No to All Button |
| StandardButton.Abort | Use RejectRole Defined Abort Button |
| StandardButton.Retry | Use AcceptRole Defined Retry Button |
| StandardButton.Ignore | Use AcceptRole Defined Ignore Button |

边栏推荐
- easyui入门
- MyBaits
- fastadmin tp 安装使用百度富文本编辑器UEditor
- [cloud co creation] explore how gaussdb helps ICBC create core financial data
- Talk about how to use redis to realize distributed locks?
- 伦敦银K线图的各种有用形态
- 测试框架-unittest-命令行操作、断言方法
- [image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code
- Today, I went to oppo for an interview and was asked numbly
- 一文理解分布式开发中的服务治理
猜你喜欢

激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就

论文笔记:Highly accurate protein structure prediction with AlphaFold (AlphaFold 2 & appendix)

解决Win10磁盘占用100%

Rebudget: balance efficiency and fairness in market-based multi-core resource allocation by reallocating the budget at run time

Budget report ppt

柏睿数据加入阿里云PolarDB开源数据库社区

今天去 OPPO 面试,被问麻了

伦敦银K线图的各种有用形态

WPF 实现用户头像选择器

城市燃气安全再拉警钟,如何防患于未“燃”?
随机推荐
WPF 实现用户头像选择器
SAP Fiori 的附件处理(Attachment handling)
MySQL metadata lock (MDL)
C# 模拟抽奖
MySQL pessimistic lock
一文理解分布式开发中的服务治理
复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
ReBudget:通过运行时重新分配预算的方法,在基于市场的多核资源分配中权衡效率与公平性
月薪1万在中国是什么水平?答案揭露残酷的收入真相
Chain game development ready-made version chain game system development detailed principle chain game source code delivery
02. 将参数props限制在一个类型的列表中
百度富文本编辑器UEditor单张图片上传跨域
Is the win11 dynamic tile gone? Method of restoring dynamic tile in Win 11
MySQL之联表查询、常用函数、聚合函数
MyBaits
7. Dependency injection
EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心
0x80131500 solution for not opening Microsoft Store
What is the shortcut key for win11 Desktop Switching? Win11 fast desktop switching method
Understanding service governance in distributed development