Qt中的标准对话框
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中的标准对话框的更多相关文章
- Qt中的标准对话框之QMessageBox
1. Qt标准对话框 Qt为开发者提供了一些可复用的对话框类型 Qt提供的可复用对话框全部继承自QDialog类 Qt中的对话框的使用方式和QDialog完全一致 2. 标准对话框的使用步骤 ①定义对 ...
- QT中的各种对话框
大家可以参见QT中各种MessageBox的使用的这篇文章 界面效果图如下,大家可以用代码自己操作 diglog.h #ifndef DIALOG_H #define DIALOG_H #includ ...
- Qt 中的消息对话框
1. QMessagebox 类的几个静态成员函数,可以直接调用创建对话框 StandardButton critical(QWidget * parent, const QString & ...
- 《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用
和大多数操作系统一样,Windows及Linux都提供了一系列的标准对话框,如文件选择,字体选择,颜色选择等,这些标准对话框为应用程序提供了一致的观感.Qt对这些标准对话框都定义了相关的类.这些类让使 ...
- 跟我一起学QT_QT标准对话框_文件对话框
标准对话框 QT的标准对话框分为以下几种 颜色对话框 文件对话框 字体对话框 输入对话框 消息对话框 进度对话框 错误信息对话框 向导对话框 文件对话框 QT中的文件对话框QFileDialog类提供 ...
- QT笔记之模态对话框及非模态对话框
模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等.所谓模态对话框就是在其 ...
- qt 关于Qt中MVC的介绍与使用
Qt包含一组使用模型/视图结构的类,可以用来管理数据并呈现给用户.这种体系结构引入的分离使开发人员更灵活地定制项目,并且提供了一个标准模型的接口,以允许广泛范围的数据源被使用到到现有的视图中. 模型 ...
- Qt标准对话框之QColorDialog
Qt中提供了一些标准的对话框,用于实现一些常用的预定义功能,比如本节中将要介绍的颜色对话框——QColorDialog. 在不同的系统平台下,颜色对话框的显示效果可能会有所不同,主要因系统主题风格而异 ...
- 如何修改Qt标准对话框的文字(例如,英文改成中文)
此篇文章参考qtcn论坛整理而成,因为文字和图片是本人亲自组织,所以仍算原创. http://www.qtcn.org/bbs/read-htm-tid-30650.html http://blog. ...
随机推荐
- 在JAVA中,String,Stringbuffer,StringBuilder 的区别
首先是,String,StringBuffer的区别 两者的主要却别有两方面,第一是线程安全方面,第二是效率方面 线程安全方面: String 不是线程安全的,这意味着在不同线程共享一个String ...
- jquery datatable设置垂直滚动后,表头(th)错位问题
jquery datatable设置垂直滚动后,表头(th)错位问题 问题描述: 我在datatable里设置:"scrollY": '300px',垂直滚动属性后,表头的宽度就会 ...
- bzoj 2653 middle(主席树)
题面:https://vjudge.net/problem/HYSBZ-2653 博客:https://blog.csdn.net/litble/article/details/78984846 这个 ...
- 03 MD5加密、Base64处理
1 什么是MD5 信息摘要算法,可以将字符进行加密,每个加密对象在进行加密后都是等长的 应用场景:将用户密码经过MD5加密后再存储到数据库中,这样即使是超级管理员也没有能力知道用户的具体密码是多少:因 ...
- 提取a标签的链接文字
在seg上看到一个问题 <a href="http://www.abc.com/thread-4131866-1-1.html" class="s xst" ...
- 高性能MySQL笔记-第4章Optimizing Schema and Data Types
1.Good schema design is pretty universal, but of course MySQL has special implementation details to ...
- SDUT 3400 数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 150MS Memory Limit: 65536KB Submit Statistic Problem Description ...
- Ryouko's Memory Note
题目意思:一个书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么|x-y|页都需要翻到(联系生活实际就很容易理解的了).接着有m pieces 的 information ...
- Algorithms - Insertion sort
印象 图1 插入排序过程 思想 插入排序(Insertion Sort)的主要思想是不断地将待排序的元素插入到有序序列中,是有序序列不断地扩大,直至所有元素都被插入到有序序列中. 分析 时间复杂度: ...
- dubbo心跳机制 (2)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 来看一下HeaderExchangeServer.this.getChannels(): 1 p ...