C/C++ Qt Dialog 对话框组件应用技巧
在qt中对话框分为两种形式,一种是标准对话框,另一种则是自定义对话框,在一般开发过程中标准对话框使用是最多的了,标准对话框一般包括 qmessagebox,qinputdialog,qfiledialog 这几种,这里我将总结本人在开发过程中常用到的标准对话框的使用技巧。
qt框架下,常用的标准对话框有下面这几种:
- qmessagebox 提示信息框
- qinputdialog 基本输入对话框(文本输入,整数输入,浮点数输入,单选框输入)
- qfiledialog 文件选择对话框(选择文件,多选文件,保存文件)
qmessagebox 消息弹窗: 消息对话框用于提示用户,常见的有四种分别是:提示,警告,错误,确认,代码归纳如下所示。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qmessagebox>
mainwindow::mainwindow(qwidget *parent) :qmainwindow(parent),ui(new ui::mainwindow)
{
ui->setupui(this);
}
mainwindow::~mainwindow()
{
delete ui;
}
// by : lyshark
// https://www.cnblogs.com/lyshark
// 弹出各种messagebox
void mainwindow::on_pushbutton_clicked()
{
qstring dlgtitle="消息框";
qstring strinfo="文件已被修改,是否保存修改 ?";
qmessagebox::standardbutton defaultbtn = qmessagebox::nobutton; // 缺省按钮
qmessagebox::standardbutton result; // 返回选择的按钮
// 弹窗分类 question information warning critical
result=qmessagebox::question(this, dlgtitle, strinfo,qmessagebox::yes|qmessagebox::no |qmessagebox::cancel,defaultbtn);
if (result==qmessagebox::yes)
ui->plaintextedit->appendplaintext("question消息框: yes 被选择");
else if(result==qmessagebox::no)
ui->plaintextedit->appendplaintext("question消息框: no 被选择");
else if(result==qmessagebox::cancel)
ui->plaintextedit->appendplaintext("question消息框: cancel 被选择");
else
ui->plaintextedit->appendplaintext("question消息框: 无选择");
}
// 弹出关于提示
void mainwindow::on_pushbutton_2_clicked()
{
qstring dlgtitle="about 消息框";
qstring strinfo="我开发的数据查看软件 v1.0 \n 保留所有版权";
qmessagebox::about(this, dlgtitle, strinfo);
}
qmessagebox 退出事件: 弹窗组件还可以配合qcloseevent实现事件通知机制,例如当窗体被关闭则提示用户是否关闭窗体。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qmessagebox>
#include <qcloseevent>
mainwindow::mainwindow(qwidget *parent) :qmainwindow(parent),ui(new ui::mainwindow)
{
ui->setupui(this);
}
// 窗口关闭时询问是否退出
void mainwindow::closeevent(qcloseevent *event)
{
qmessagebox::standardbutton result=qmessagebox::question(this, "确认", "确定要退出本程序吗?",
qmessagebox::yes|qmessagebox::no |qmessagebox::cancel,
qmessagebox::no);
if (result==qmessagebox::yes)
event->accept();
else
event->ignore();
}
// by : lyshark
// https://www.cnblogs.com/lyshark
mainwindow::~mainwindow()
{
delete ui;
}
qinputdialog 对话框: 该对话框长用于输入一段特殊的文本,浮点数,或者选择一个列表框中的选项,该功能用于简单的用户交互场景。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qlineedit>
#include <qinputdialog>
mainwindow::mainwindow(qwidget *parent) :qmainwindow(parent),ui(new ui::mainwindow)
{
ui->setupui(this);
}
mainwindow::~mainwindow()
{
delete ui;
}
// 文本输入对话框
void mainwindow::on_pushbutton_clicked()
{
qstring dlgtitle="输入文字对话框";
qstring txtlabel="请输入文件名";
qstring defaultinput="新建文件.txt";
qlineedit::echomode echomode=qlineedit::normal; // 正常文字输入
// qlineedit::echomode echomode=qlineedit::password; // 密码输入
bool flag = false;
qstring text = qinputdialog::gettext(this, dlgtitle,txtlabel, echomode,defaultinput, &flag);
if (flag && !text.isempty())
{
ui->plaintextedit->appendplaintext(text);
}
}
// 整数数值输入对话框
// by : lyshark
// https://www.cnblogs.com/lyshark
void mainwindow::on_pushbutton_2_clicked()
{
qstring dlgtitle="输入整数对话框";
qstring txtlabel="设置字体大小";
int defaultvalue=ui->plaintextedit->font().pointsize(); // 现有字体大小
int minvalue=6, maxvalue=50, stepvalue=1; // 范围(步长)
bool flag=false;
int inputvalue = qinputdialog::getint(this, dlgtitle,txtlabel,defaultvalue, minvalue,maxvalue,stepvalue,&flag);
if (flag)
{
qfont font=ui->plaintextedit->font();
font.setpointsize(inputvalue);
ui->plaintextedit->setfont(font);
}
}
// 浮点数输入对话框
void mainwindow::on_pushbutton_3_clicked()
{
qstring dlgtitle="输入浮点数对话框";
qstring txtlabel="输入一个浮点数";
float defaultvalue=3.13;
float minvalue=0, maxvalue=10000; // 范围
int decimals=2; // 小数点位数
bool flag=false;
float inputvalue = qinputdialog::getdouble(this, dlgtitle,txtlabel,defaultvalue, minvalue,maxvalue,decimals,&flag);
if (flag)
{
qstring str=qstring::asprintf("输入了一个浮点数:%.2f",inputvalue);
ui->plaintextedit->appendplaintext(str);
}
}
// 单选框条目选择对话框
void mainwindow::on_pushbutton_4_clicked()
{
qstringlist items; // 列表内容
items <<"优秀"<<"良好"<<"合格"<<"不合格"; // 放入列表
qstring dlgtitle="条目选择对话框";
qstring txtlabel="请选择级别";
int curindex=0; //初始选择项
bool editable=false; // 是否可编辑
bool flag=false;
qstring text = qinputdialog::getitem(this, dlgtitle,txtlabel,items,curindex,editable,&flag);
if (flag && !text.isempty())
{
ui->plaintextedit->appendplaintext(text);
}
}
qfiledialog 对话框: 该对话框用于对文本的操作,例如打开文件,保存文件,选择文件夹等,当点击选择后,对话框会自动提取出文件路径。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qfiledialog>
mainwindow::mainwindow(qwidget *parent) :qmainwindow(parent),ui(new ui::mainwindow)
{
ui->setupui(this);
}
mainwindow::~mainwindow()
{
delete ui;
}
// 选择单个文件对话框
void mainwindow::on_pushbutton_clicked()
{
qstring curpath=qdir::currentpath(); // 获取系统当前目录
// qstring curpath=qcoreapplication::applicationdirpath(); // 获取应用程序的路径
qstring dlgtitle="选择一个文件"; // 对话框标题
qstring filter="文本文件(*.txt);;图片文件(*.jpg *.gif *.png);;所有文件(*.*)"; // 文件过滤器
qstring afilename=qfiledialog::getopenfilename(this,dlgtitle,curpath,filter);
if (!afilename.isempty())
{
ui->plaintextedit->appendplaintext(afilename);
}
}
// 选择多个文件对话框
// by : lyshark
// https://www.cnblogs.com/lyshark
void mainwindow::on_pushbutton_2_clicked()
{
// qstring curpath=qcoreapplication::applicationdirpath(); // 获取应用程序的路径
qstring curpath=qdir::currentpath(); // 获取系统当前目录
qstring dlgtitle="选择多个文件"; // 对话框标题
qstring filter="文本文件(*.txt);;图片文件(*.jpg *.gif *.png);;所有文件(*.*)"; // 文件过滤器
qstringlist filelist=qfiledialog::getopenfilenames(this,dlgtitle,curpath,filter);
for (int i=0; i<filelist.count();i++)
{
// 循环将文件路径添加到列表中
ui->plaintextedit->appendplaintext(filelist.at(i));
}
}
// 选择文件夹
void mainwindow::on_pushbutton_3_clicked()
{
qstring curpath=qcoreapplication::applicationdirpath(); // 获取应用程序的路径
// qstring curpath=qdir::currentpath(); // 获取系统当前目录
// 调用打开文件对话框打开一个文件
qstring dlgtitle="选择一个目录"; // 对话框标题
qstring selecteddir=qfiledialog::getexistingdirectory(this,dlgtitle,curpath,qfiledialog::showdirsonly);
if (!selecteddir.isempty())
{
ui->plaintextedit->appendplaintext(selecteddir);
}
}
// 保存文件对话框
void mainwindow::on_pushbutton_4_clicked()
{
qstring curpath=qcoreapplication::applicationdirpath(); // 获取应用程序的路径
qstring dlgtitle="保存文件"; // 对话框标题
qstring filter="文本文件(*.txt);;h文件(*.h);;c++文件(.cpp);;所有文件(*.*)"; // 文件过滤器
qstring afilename=qfiledialog::getsavefilename(this,dlgtitle,curpath,filter);
if (!afilename.isempty())
{
ui->plaintextedit->appendplaintext(afilename);
}
}
文章出处:https://www.cnblogs.com/lyshark
版权声明: 本博客文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章 [均为原创] 作品,转载请 [添加出处] ,您添加出处是我创作的动力!
博主警告:如果您恶意转载本人文章,则您的整站文章,将会变为我的原创作品,请相互尊重!
到此这篇关于c/c++ qt dialog 对话框组件应用的文章就介绍到这了,更多相关c++ 对话框组件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!