当前位置:网站首页>QT_QDialog dialog
QT_QDialog dialog
2022-08-01 17:38:00 【little engineer】
QT_对话框
建议参考
- 模态对话框,就是会阻塞同一应用程序中其它窗口的输入.
模态对话框很常见,比如“打开文件”功能.你可以尝试一下记事本的打开文件,当打开文件对话框出现时,我们是不能对除此对话框之外的窗口部分进行操作的.
- 非模态对话框,例如查找对话框,我们可以在显示着查找对话框的同时,继续对记事本的内容进行编辑.
对话框分类
Qt 的内置对话框大致分为以下几类:
- QColorDialog: 选择颜色;
- QFileDialog: 选择文件或者目录;
- QFontDialog: 选select font;
- QInputDialog: 允许用户输入一个值,并将其值返回;
- QMessageBox: 模态对话框,用于显示信息、询问问题等;
- QPageSetupDialog: 为打印机提供纸张相关的选项;
- QPrintDialog: 打印机配置;
- QPrintPreviewDialog:打印预览;
- QProgressDialog: 显示操作过程.
自定义对话框
Qt 支持模态对话框和非模态对话框.
模态与非模态的实现:
使用QDialog::exec()实现应用程序级别的模态对话框使用QDialog::open()实现窗口级别的模态对话框使用QDialog::show()实现非模态对话框.
Qt 中使用QDialog类实现对话框.就像主窗口一样,我们通常会设计一个类继承QDialog.
QDialog(及其子类,以及所有Qt::Dialog类型的类)的对于其 parent 指针都有额外的解释:
如果 parent 为 NULL,则该对话框会作为一个顶层窗口,否则则作为其Child dialog of parent component(此时,其默认出现的位置是 parent 的中心).
顶层窗口与非顶层窗口的区别在于:/*顶层窗口在任务栏会有自己的位置,而非顶层窗口则会共享其父组件的位置.*/
#include <QDialog>
#include <QMessageBox>
#include <QColorDialog>
#include <QFileDialog>
//点击新建菜单项,弹出对话框
connect(ui->actionnew,&QAction::triggered,this,[=](){
// qDebug()<<"弹出对话框"<<endl;
//对话框 有两种
//模态对话框: When the dialog box is open, other windows cannot be operated
//非模态对话框:When the dialog box is open, it is possible to operate other windows
//Created modally
// QDialog dlg(this); // Child dialog of parent component.
// dlg.resize(200,100);
// dlg.exec(); //阻塞功能 应用程序级别 dlgCreated on the stack,It was not released becauseexec()阻塞.
// qDebug()<<"弹出对话框"<<endl;
//Created non-modally ,Blocking is not used
// QDialog dlg2(this); //Created on the stack,Without the use of blocking words flashed by
// dlg2->show(); //Without the use of blocking words flashed by.showafter the functionLambdaThe expression ends and exits,则dlg2Local objects are destroyed.对话框消失.
// QDialog * dlg2 = new QDialog(this); //new出来的this对象在堆上,创建成widget的children表(this),这样只有在widgetReleased when the window is closed!!!只有thisObject destructor will be freed,这里this是整个widget窗口.如果没有这个this依赖(QDialog * dlg2 = new QDialog),Then the created object needs to be created manuallydelete才能删除(delete QDialog).
// dlg2->resize(200,100);
// dlg2->show();
// //设置dlg2的属性
// dlg2->setAttribute(Qt::WA_DeleteOnClose); //When the close button of the dialog is clicked,Free this memory
//Set standard dialog QMessageBox
// QMessageBox::critical(this,"错误"," critical");
// QMessageBox::information(this,"信息"," info");
//参数1 父亲, 2:标题 3:提示内容 4:按键类型 5:关联回车的按键
// QMessageBox::question(this,"询问对话框","question",QMessageBox::Save | QMessageBox::Cancel);
// if(QMessageBox::Save == QMessageBox::question(this,"询问对话框","question",QMessageBox::Save | QMessageBox::Cancel))
// {
// qDebug()<<"点击的是保存"<<endl;
// }
// else {
// qDebug()<<"点击的是取消"<<endl;
// }
//选择颜色对话框
QColor color_choose = QColorDialog::getColor(QColor(255,0,0));
qDebug()<<color_choose.red()<<color_choose.blue()<<color_choose.green();
//文件对话框,caption:标题 dir:路径 filter:What types of files are displayed(过滤功能)
// QFileDialog::getOpenFileName(this,"打开文件","D:\\00_MyDate\\QT","(*.txt *.png)"); //注意.Change it to double slashes
// QString path = QFileDialog::getOpenFileName(this,"打开文件","D:\\00_MyDate\\QT","(*.txt *.png)");
// qDebug()<<path;
QString name = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("请选择视频文件")); //fromLocal8Bit : The default code may be garbled in Chinese
if (name.isEmpty())return; //If no file is selected,则name为空!
string file = name.toLocal8Bit().data(); //中文转换, Non-Chinese files are fine. Chinese must passtoLocal8Bit转换为utf8格式.
1. 文件对话框的使用
#include <QFileDialog>
#include <string>
#include <QMessageBox>
//例程1
QString curPath=QDir::currentPath();//获取系统当前目录
//获取应用程序的路径
QString dlgTitle="选择一个文件"; //对话框标题
QString filter="文本文件(*.txt);;图片文件(*.jpg *.gif *.png);;所有文件(*.*)"; //文件过滤器
QString aFileName=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
if (!aFileName.isEmpty())
ui->plainTextEdit->appendPlainText(aFileName);
}
//例程2
void XVideoUI::Open()
{
QString name = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("请选择视频文件")); //fromLocal8Bit : The default code may be garbled in Chinese
if (name.isEmpty())return;
string file = name.toLocal8Bit().data(); //中文转换, Non-Chinese files are fine. Chinese must passtoLocal8Bit转换为utf8格式.
//QMessageBox::information(this, "", name);
if (!XVideoThread::Get()->Open(file))
{
QMessageBox::information(this, "error", name + " open failed!");
return;
}
Play(); //Turn on default playback
}
//Open the No. 1 video source file
static VideoCapture cap1;//Video source number one
bool XVideoThread::Open(const std::string file)
{
cout << "open :" << file << endl;
mutex.lock();
bool re = cap1.open(file);
mutex.unlock();
cout << re << endl;
if (!re)
return re;
fps = cap1.get(CAP_PROP_FPS); //获取fps
width = cap1.get(CAP_PROP_FRAME_WIDTH); //Get the width of the video、高
height = cap1.get(CAP_PROP_FRAME_HEIGHT);
if (fps <= 0) fps = 25; //In case the value is not obtained.take a custom value
src1file = file; //Record the filename when opening
double count = cap1.get(CAP_PROP_FRAME_COUNT);
totalMs = (count / (double)fps) * 1000; //总帧数/fps = 秒时长, 换算为毫秒
return true;
}
2. 颜色对话框
QColorDialog 对话框
QColorDialog 是选择颜色对话框,选择颜色使用静态函数QColorDialog::getColor().下面是
“选择颜色”按钮的代码,它为文本框的字体选择颜色.
void Dialog::on_btnColor_clicked()
{
QPalette pal=ui->plainTextEdit->palette(); //获取现有palette
QColor iniColor=pal.color(QPalette::Text); //现有的文字颜色
QColor color=QColorDialog::getColor(iniColor,this,"选择颜色");
if (color.isValid()) //选择有效
{
pal.setColor(QPalette::Text,color); //palette 设置选择的颜色
ui->plainTextEdit->setPalette(pal); //设置palette
}
}
3. 字体对话框
QFontDialog 是选select font对话框,选select font使用静态函数QFontDialog::getFont().下面是“选
select font”按钮的代码,它为文本框选select font,字体设置的内容包括字体名称、大小、粗体、
斜体等.
void Dialog::on_btnFont_clicked()
{
//选select font
QFont iniFont=ui->plainTextEdit->font(); //获取文本框的字体
bool ok=false;
QFont font=QFontDialog::getFont(&ok,iniFont); //选select font
if (ok) //选择有效
ui->plainTextEdit->setFont(font);
}
4. 标准输入对话框
QInputDialog 标准输入对话框
QInputDialog 有单行字符串输入、整数输入、浮点数输入、列表框选择输入和多行文本等多种输入方式,图3 是其中4 interface effects.
void Dialog::on_btnInputString_clicked()
{
//输入字符串
QString dlgTitle="输入文字对话框";
QString txtLabel="请输入文件名";
QString defaultInput="新建文件.txt";
QLineEdit::EchoMode echoMode=QLineEdit::Normal;//正常文字输入
//QLineEdit::EchoMode echoMode=QLineEdit::Password;//密码输入
bool ok=false;
QString text = QInputDialog::getText(this, dlgTitle,txtLabel, echoMode,defaultInput, &ok);
if (ok && !text.isEmpty())
ui->plainTextEdit->appendPlainText(text);
}
5. 消息对话框
简单信息提示
消息对话框QMessageBox 用于显示提示、警告、错误等信息,或进行确认选择,由几个静态函数实现这些功能.其中warning()、information()、critical() 和about() 这几个函数的输入参数和使用方法相同,只是信息提示的图标有区别.
void Dialog::on_btnMsgInformation_clicked()
{
QString dlgTitle="information 消息框";
QString strInfo="文件已经打开,字体大小已设置";
QMessageBox::information(this, dlgTitle, strInfo,
QMessageBox::Ok,QMessageBox::NoButton);
}
void Dialog::on_btnMsgWarning_clicked()
{
QString dlgTitle="warning 消息框";
QString strInfo="文件内容已经被修改";
QMessageBox::warning(this, dlgTitle, strInfo);
}
void Dialog::on_btnMsgCritical_clicked()
{
QString dlgTitle="critical 消息框";
QString strInfo="有不明程序访问网络";
QMessageBox::critical(this, dlgTitle, strInfo);
}
void Dialog::on_btnMsgAbout_clicked()
{
QString dlgTitle="about 消息框";
QString strInfo="我开发的数据查看软件V1.0 \n 保留所有版权";
QMessageBox::about(this, dlgTitle, strInfo);
}
6. 自定义对话框
继承自QDialog 对话框
class QWDialogSize : public QDialog
{
Q_OBJECT
public:
explicit QWDialogSize(QWidget *parent = 0);
~QWDialogSize();
int rowCount();//Gets the number of lines of dialog input
int columnCount();//Gets the number of columns for dialog input
void setRowColumn(int row, int column); //Two on the initial dialogSpinBox 的值
private slots:
private:
Ui::QWDialogSize *ui;
};
边栏推荐
- The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
- XAML WPF item groupBox control
- tooltip control
- 金仓数据库KingbaseES安全指南--6.3. Kerberos身份验证
- SQL函数 TO_CHAR(二)
- 关于Mysql服务无法启动的问题
- 金仓数据库 OCCI迁移指南(2. 概述)
- SQL函数 TO_CHAR(三)
- 基于ORB-SLAM2的改进代码
- 小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
猜你喜欢
随机推荐
机器学习快速入门
在码云拉取代码后,调整了seata版本1.5.2。出现如下异常。是因为数据库表缺少字段导致的吗?
中信证券是国内十大券商吗?怎么开户安全?
金仓数据库 MySQL 至 KingbaseES 迁移最佳实践(2. 概述)
关系运算符和if,else语句
创造建材数字转型新视界,中建材如何多边赋能集团业务快速发展
DBPack SQL Tracing 功能及数据加密功能详解
SQL的ROUND函数用法及其实例
The site is not found after the website is filed. You have not bound this domain name or IP to the corresponding site! The configuration file does not take effect!
My new book has sold 10,000 copies!
QLineEdit学习与使用
C语言理论--笔试面试基础稳固
When custom annotations implement log printing, specific fields are blocked from printing
深圳市商务局2022年度中央资金(跨境电子商务企业市场开拓扶持事项)申报指南
二分练习题
2022年深圳市促进大健康产业集群高质量发展的若干措施
06 redis cluster structures
matlab 基于奇偶校验的LSB隐藏水印 三种改进
快速抽取resnet_v2_152中间的特征层
04 flink cluster construction









