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. ...
随机推荐
- MSSQL 当前数据库中已存在用户或角色,SQLServer2008,错误15023,
原因: sql server中“登录”与“用户”的区别,“登录”用于用户身份验证,而数据库“用户”帐户用于数据库访问和权限验证.登录通过安全识别符 (SID) 与用户关联.将数据库恢复到其他服务器时, ...
- 使用绘图API自定义组件
-----------------siwuxie095 工程名:CustomizeSwing 包名:com.siwuxie095.swi ...
- Selenium二次封装-Java版本
package com.yanfuchang.selenium.utils; import java.awt.AWTException; import java.awt.Robot; import j ...
- 36-图像有用区(dfs, bfs)
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=92 图像有用区域 时间限制:3000 ms | 内存限制:65535 KB 难度:4 ...
- 算法Sedgewick第四版-第1章基础-021一双向链表,在遍历时可修改、删除元素
package algorithms.ADT; /*************************************************************************** ...
- R语言输出pdf时,中文乱码处理
本文转载自:https://blog.csdn.net/hongweigg/article/details/47907555 1.使用基础包,使用函数pdf()输出 在使用pdf()函数时,要输出中文 ...
- loj10088 出纳员问题
传送门 分析 我们设pre[i]为到第i个时段的雇佣员工的总数量,sum[i]表示时段i的可雇佣员工的总数量,r[i]表示时段i所需工人的数量.由此我们不难求出: 0<=pre[i]-pre[i ...
- 【Arcgis for android】保存地图截图到sd卡
关键词:arcgis for android ,截图,bitmap,sd卡 参考文章:http://blog.csdn.net/wozaifeiyang0/article/details/767972 ...
- Django之后台管理一
所有的网站都有一个管理后台来对所有的网站数据进行管理.那么Django的后台管理是如何进行的.在网页中输入http://127.0.0.1:8001/admin.得到如下的登录界面 在这里可以看到管理 ...
- js中 关于DOM的事件操作
一.JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象 ...