1. Qt为开发者提供了一些可复用的对话框类型,如QMessageBox,QFileDialog,QPrintDialog, QColorDialog, QInputDialog, QProgressDialog等,他们全部继承与QDialog类。

2. Qt中的对话框遵循相同的使用方式:

    DialogType dlg(this);                  /* 1. 定义对话框对象 */

    dlg.setPropertyxxx(value);             /* 2. 设置对话框属性 */

    if(dlg.exec() == DialogType::Value)
{
Type v = dlg.getDialogValue(); /* 3. 获取对话框数据 */
... /* 4. 处理对话框数据 */
}

3. 简单实例

Widget.h:

#ifndef WIDGET_H
#define WIDGET_H #include <QtGui/QWidget>
#include <QPushButton> class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton SimpleMsgBtn;
QPushButton CustomMsgBtn;
QPushButton OpenFileBtn;
QPushButton SaveFileBtn;
QPushButton QColorBtn;
QPushButton InputBtn;
QPushButton QFontBtn;
QPushButton QProgressBtn;
QPushButton QPrintBtn;
private slots:
void SmipleMsgBtn_Clicked();
void CustomMsgBtn_Clicked();
void OpenFileBtn_Clicked();
void SaveFileBtn_Clicked();
void QColorBtn_Clicked();
void InputBtn_Clicked();
void QFontBtn_Clicked();
void QProgressBtn_Clicked();
void QPrintBtn_Clicked();
public:
Widget(QWidget *parent = );
~Widget();
}; #endif // WIDGET_H

Widget.cpp:

#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <QColorDialog>
#include <QInputDialog>
#include <QFontDialog>
#include <QProgressDialog>
#include <QPrintDialog>
#include <QTextDocument>
#include "Widget.h" Widget::Widget(QWidget *parent)
: QWidget(parent), SimpleMsgBtn(this), CustomMsgBtn(this), OpenFileBtn(this), SaveFileBtn(this),
QColorBtn(this), InputBtn(this), QFontBtn(this), QProgressBtn(this), QPrintBtn(this)
{
SimpleMsgBtn.setText("Simple Message Dialog");
SimpleMsgBtn.move(, );
SimpleMsgBtn.resize(, ); CustomMsgBtn.setText("Custom Message Dialog");
CustomMsgBtn.move(, );
CustomMsgBtn.resize(, ); OpenFileBtn.setText("Open File Message Dialog");
OpenFileBtn.move(, );
OpenFileBtn.resize(, ); SaveFileBtn.setText("Save File Message Dialog");
SaveFileBtn.move(, );
SaveFileBtn.resize(, ); QColorBtn.setText("QColor Dialog");
QColorBtn.move(, );
QColorBtn.resize(, ); InputBtn.setText("Input Dialog");
InputBtn.move(, );
InputBtn.resize(, ); QFontBtn.setText("QFont Dialog");
QFontBtn.move(, );
QFontBtn.resize(, ); QProgressBtn.setText("QProgress Dialog");
QProgressBtn.move(, );
QProgressBtn.resize(, ); QPrintBtn.setText("QPrint Dialog");
QPrintBtn.move(, );
QPrintBtn.resize(, ); resize(, 4);
setFixedSize(, ); connect(&SimpleMsgBtn, SIGNAL(clicked()), this, SLOT(SmipleMsgBtn_Clicked()));
connect(&CustomMsgBtn, SIGNAL(clicked()), this, SLOT(CustomMsgBtn_Clicked()));
connect(&OpenFileBtn, SIGNAL(clicked()), this, SLOT(OpenFileBtn_Clicked()));
connect(&SaveFileBtn, SIGNAL(clicked()), this, SLOT(SaveFileBtn_Clicked())); connect(&QColorBtn, SIGNAL(clicked()), this, SLOT(QColorBtn_Clicked()));
connect(&InputBtn, SIGNAL(clicked()), this, SLOT(InputBtn_Clicked())); connect(&QFontBtn, SIGNAL(clicked()), this, SLOT(QFontBtn_Clicked()));
connect(&QProgressBtn, SIGNAL(clicked()), this, SLOT(QProgressBtn_Clicked()));
connect(&QPrintBtn, SIGNAL(clicked()), this, SLOT(QPrintBtn_Clicked()));
} void Widget::SmipleMsgBtn_Clicked()
{
qDebug() << "SmipleMsgBtn_Clicked start"; QMessageBox msg(this); msg.setText("This is a message dialog"); msg.exec(); qDebug() << "SmipleMsgBtn_Clicked end";
} void Widget::CustomMsgBtn_Clicked()
{
qDebug() << "CustomMsgBtn_Clicked start"; QMessageBox msg(this); msg.setWindowTitle("Window Tile");
msg.setText("This is a message dialog");
msg.setIcon(QMessageBox::Information);
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::YesToAll); qDebug() << msg.standardButtons();
int r = msg.exec(); if(r == QMessageBox::Ok)
{
qDebug() << "OK";
} qDebug() << "CustomMsgBtn_Clicked end";
} void Widget::OpenFileBtn_Clicked()
{
qDebug() << "OpenFileBtn_Clicked start"; QFileDialog dlg(this); dlg.setAcceptMode(QFileDialog::AcceptOpen);
dlg.setFilter("Text(*.txt)");
dlg.setFileMode(QFileDialog::ExistingFiles); if(dlg.exec() == QFileDialog::Accepted)
{
QStringList fs = dlg.selectedFiles();
qDebug() << fs;
} qDebug() << "OpenFileBtn_Clicked end";
} void Widget::SaveFileBtn_Clicked()
{
qDebug() << "SaveFileBtn_Clicked start"; QFileDialog dlg(this); dlg.setAcceptMode(QFileDialog::AcceptSave);
dlg.setFilter("Text(*.txt)"); if(dlg.exec() == QFileDialog::Accepted)
{
QStringList fs = dlg.selectedFiles();
qDebug() << fs;
} qDebug() << "SaveFileBtn_Clicked end";
} void Widget::QColorBtn_Clicked()
{
qDebug() << "QColorBtn_Clicked start"; QColorDialog dlg(this); dlg.setWindowTitle("Color Editer");
dlg.setCurrentColor(QColor(, , )); if(dlg.exec() == QColorDialog::Accepted)
{
QColor color = dlg.selectedColor(); qDebug() << color;
qDebug() << color.red();
qDebug() << color.green();
qDebug() << color.blue();
qDebug() << color.hsvHue();
qDebug() << color.saturation();
qDebug() << color.value();
} qDebug() << "QColorBtn_Clicked end";
} void Widget::InputBtn_Clicked()
{
qDebug() << "InputBtn_Clicked start";
QInputDialog dlg(this); dlg.setWindowTitle("Input...");
dlg.setLabelText("Please input a string");
dlg.setInputMode(QInputDialog::TextInput); if(dlg.exec() == QInputDialog::Accepted)
{
qDebug() << dlg.textValue();
} qDebug() << "InputBtn_Clicked end";
} void Widget::QFontBtn_Clicked()
{
qDebug() << "QFontBtn_Clicked start";
QFontDialog dlg(this); dlg.setWindowTitle("Font Editor");
dlg.setCurrentFont(QFont("Arial", , QFont::Normal)); if(dlg.exec() == QFontDialog::Accepted)
{
qDebug() << dlg.selectedFont();
} qDebug() << "QFontBtn_Clicked end";
} void Widget::QProgressBtn_Clicked()
{
qDebug() << "QProgressBtn_Clicked start"; QProgressDialog dlg(this); dlg.setWindowTitle("Updating...");
dlg.setLabelText("Donwloading from server...");
dlg.setMinimum();
dlg.setMaximum(); dlg.setValue(); //Create a new thread, download and updat value dlg.exec(); qDebug() << "QProgressBtn_Clicked end";
} void Widget::QPrintBtn_Clicked()
{
qDebug() << "QPrintBtn_Clicked start"; QPrintDialog dlg(this); if(dlg.exec() == QPrintDialog::Accepted)
{
QPrinter* p = dlg.printer(); QTextDocument td; td.setPlainText("Print Object Test"); td.print(p); qDebug() << "QPrintDialog::Accepted";
} qDebug() << "QPrintBtn_Clicked end";
} Widget::~Widget()
{ }

Qt中的标准对话框的更多相关文章

  1. Qt中的标准对话框之QMessageBox

    1. Qt标准对话框 Qt为开发者提供了一些可复用的对话框类型 Qt提供的可复用对话框全部继承自QDialog类 Qt中的对话框的使用方式和QDialog完全一致 2. 标准对话框的使用步骤 ①定义对 ...

  2. QT中的各种对话框

    大家可以参见QT中各种MessageBox的使用的这篇文章 界面效果图如下,大家可以用代码自己操作 diglog.h #ifndef DIALOG_H #define DIALOG_H #includ ...

  3. Qt 中的消息对话框

    1. QMessagebox 类的几个静态成员函数,可以直接调用创建对话框 StandardButton critical(QWidget * parent, const QString &  ...

  4. 《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用

    和大多数操作系统一样,Windows及Linux都提供了一系列的标准对话框,如文件选择,字体选择,颜色选择等,这些标准对话框为应用程序提供了一致的观感.Qt对这些标准对话框都定义了相关的类.这些类让使 ...

  5. 跟我一起学QT_QT标准对话框_文件对话框

    标准对话框 QT的标准对话框分为以下几种 颜色对话框 文件对话框 字体对话框 输入对话框 消息对话框 进度对话框 错误信息对话框 向导对话框 文件对话框 QT中的文件对话框QFileDialog类提供 ...

  6. QT笔记之模态对话框及非模态对话框

    模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等.所谓模态对话框就是在其 ...

  7. qt 关于Qt中MVC的介绍与使用

    Qt包含一组使用模型/视图结构的类,可以用来管理数据并呈现给用户.这种体系结构引入的分离使开发人员更灵活地定制项目,并且提供了一个标准模型的接口,以允许广泛范围的数据源被使用到到现有的视图中. 模型 ...

  8. Qt标准对话框之QColorDialog

    Qt中提供了一些标准的对话框,用于实现一些常用的预定义功能,比如本节中将要介绍的颜色对话框——QColorDialog. 在不同的系统平台下,颜色对话框的显示效果可能会有所不同,主要因系统主题风格而异 ...

  9. 如何修改Qt标准对话框的文字(例如,英文改成中文)

    此篇文章参考qtcn论坛整理而成,因为文字和图片是本人亲自组织,所以仍算原创. http://www.qtcn.org/bbs/read-htm-tid-30650.html http://blog. ...

随机推荐

  1. Flow Layout

    --------------siwuxie095                             将根面板 contentPane 的布局切换为 Flow Layout     Flow La ...

  2. 使用Java建立聊天客户端

    ---------------siwuxie095                             关于 聊天服务器,详见本人博客的分类:来一杯Java, 里面的 使用ServerSocket ...

  3. str_place()替换函数

    str_replace() 函数使用一个字符串替换字符串中的另一些字符. 注释:该函数对大小写敏感.请使用 str_ireplace() 执行对大小写不敏感的搜索. echo str_replace( ...

  4. Vbs 脚本编程简明教程之一

    —为什么要使用 Vbs ? 在 Windows 中,学习计算机操作也许很简单,但是很多计算机工作是重复性劳动,例如你每周也许需要对一些计算机文件进行复制.粘贴.改名.删除,也许你每天启动 计算机第一件 ...

  5. DB2--值为null则赋默认值

    数据库sql操作经常会做一些null值的处理.如果一个字段的值为null,我们希望查询出的结果默认设为0或空,则使用函数 COALESCE(column,0)  ,0的位置可以替换为其他值,可以是'' ...

  6. oracle获取列的备注和数据类型

    select column_name, data_type, data_precision, data_scale, nvl((select t_s.comments from all_col_com ...

  7. windows脚本设置网络IP地址

    需求描述 不通的网络环境下,可能需要设置静态IP地址,或设置为动态获取,每次重复手动的配置费时费力,通过脚本可以实现一键设置 脚本实现 1.设置静态IP 1.1新建文本文档,复制粘贴如下内容 nets ...

  8. 完整读写txt 并提取{}里的内容

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  9. metasploit 读书笔记1

    The msfpayload component of Metasploit allows you to generate shellcode, executables, and much more ...

  10. netty下载源码并导入idea

    netty源码导入eclipse会有一些兼容性问题,网上有解决方案,官方推荐idea,故此用idea. 拷贝git地址:https://github.com/netty/netty.git 使用git ...