1.消息对话框是应用程序中最常见的界面元素
消息对话框主要用于:为用户提示重要信息,强制用户进行操作选择
2.文件对话框
Open Mode------应用程序中需要用户打开一个外部的文件
Save Mode------应用程序中需要将当前内容存储在用户指定的外部文件中
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton SimpleMsgBtn;
QPushButton CustomMsgBtn;
QPushButton OpenFileBtn;
QPushButton SaveFileBtn;
private slots:
void SimpleMsgBtn_Clicked();
void CustomMsgBtn_Clicked();
void OpenFileBtn_Clicked();
void SaveFileBtn_Clicked();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
Widget.cpp
#include "Widget.h"
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
Widget::Widget(QWidget *parent) : QWidget(parent), SimpleMsgBtn(this),
CustomMsgBtn(this), OpenFileBtn(this), SaveFileBtn(this)
{
SimpleMsgBtn.setText("Simple Message Dialog");
SimpleMsgBtn.move(20, 20);
SimpleMsgBtn.resize(160, 30);
CustomMsgBtn.setText("Custom Message Dialog");
CustomMsgBtn.move(20, 70);
CustomMsgBtn.resize(160, 30);
OpenFileBtn.setText("Open File Dialog");
OpenFileBtn.move(20, 120);
OpenFileBtn.resize(160, 30);
SaveFileBtn.setText("Save File Dialog");
SaveFileBtn.move(20, 170);
SaveFileBtn.resize(160, 30);
resize(200, 220); //把窗口固定成 宽 200 × 高 220,可以拉伸
setFixedSize(200, 220); //把窗口固定成 宽 200 × 高 220,不可拉伸
connect(&SimpleMsgBtn, SIGNAL(clicked()), this, SLOT(SimpleMsgBtn_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()));
}
void Widget::SimpleMsgBtn_Clicked()
{
QMessageBox msg(this);
msg.setText("message dialog");
msg.exec();
}
void Widget::CustomMsgBtn_Clicked()
{
QMessageBox msg(this);
msg.setWindowTitle("Window Title");
msg.setText("custom dialog");
msg.setIcon(QMessageBox::Information);
msg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::YesToAll);
if( msg.exec() == QMessageBox::Ok)
{
qDebug() << "Ok";
}
}
void Widget::OpenFileBtn_Clicked()
{
QFileDialog dlg(this);
dlg.setAcceptMode(QFileDialog::AcceptOpen);
dlg.setNameFilter("Text(*.txt)"); //只显示 .txt 文本文件,过滤掉其他所有类型文件
dlg.setFileMode(QFileDialog::ExistingFile);
if(dlg.exec() == QFileDialog::Accepted ) //只有点击 "打开" 按钮,条件才成立,点击取消按钮则不执行内部代码
{
QStringList fs = dlg.selectedFiles(); //获取你选中的文件路径列表
for(int i=0; i<fs.count(); i++)
{
qDebug() << fs[i];
}
}
}
void Widget::SaveFileBtn_Clicked()
{
QFileDialog dlg(this);
dlg.setAcceptMode(QFileDialog::AcceptSave);
dlg.setNameFilter("Text(*.txt)"); //只显示 .txt 文本文件,过滤掉其他所有类型文件
if(dlg.exec() == QFileDialog::Accepted ) //只有点击 "打开" 按钮,条件才成立,点击取消按钮则不执行内部代码
{
QStringList fs = dlg.selectedFiles(); //获取你选中的文件路径列表
for(int i=0; i<fs.count(); i++)
{
qDebug() << fs[i];
}
}
}
Widget::~Widget()
{
}
3.颜色对话框------QColoeDialog类
QColor类用于在程序中表示颜色的概念
QColor类同时支持多种颜色表示方式
- RGB以红、绿、蓝为基准的三色模型
- HSV以色调、饱和度、明度为基准的六角锥体模型
- CMYK以天蓝、品红、黄色、黑为基准的全彩印刷色彩模型
4.输入对话框------QInputDialog类
用于临时进行数据输入的场合
输入对话框的输入模式
- QInputDialog::TextInput------输入文本字符串
- QInputDialog::IntInput------输入整型数
- QInputDialog::DoubleInput------输入浮点数
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton ColorDialogBtn;
QPushButton InputDialogBtn;
private slots:
void ColorDialogBtn_Clicked();
void InputDialogBtn_Clicked();
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
Widget.cpp
#include "Widget.h"
#include <QDebug>
#include <QColorDialog>
#include <QInputDialog>
#include <QColor>
Widget::Widget(QWidget *parent) : QWidget(parent),
ColorDialogBtn(this), InputDialogBtn(this)
{
ColorDialogBtn.setText("Color Dialog");
ColorDialogBtn.move(20, 20);
ColorDialogBtn.resize(160, 30);
InputDialogBtn.setText("Input Dialog");
InputDialogBtn.move(20, 70);
InputDialogBtn.resize(160, 30);
resize(200, 220); //把窗口固定成 宽 200 × 高 220,可以拉伸
setFixedSize(200, 220); //把窗口固定成 宽 200 × 高 220,不可拉伸
connect(&ColorDialogBtn, SIGNAL(clicked()), this, SLOT(ColorDialogBtn_Clicked()));
connect(&InputDialogBtn, SIGNAL(clicked()), this, SLOT(InputDialogBtn_Clicked()));
}
void Widget::ColorDialogBtn_Clicked()
{
QColorDialog dlg(this);
dlg.setWindowTitle("Color Editor");
// dlg.setCurrentColor(Qt::blue); //初始颜色
dlg.setCurrentColor(QColor(100, 111, 222)); //直接指定颜色的RGB
if( dlg.exec() == QColorDialog::Accepted )
{
//ARGB = 透明度 + 颜色 RGB = 颜色
QColor color = dlg.selectedColor();
qDebug() << dlg.selectedColor();
qDebug() << color;
//打印RGB三原色
qDebug() << color.red();
qDebug() << color.green();
qDebug() << color.blue();
//打印色相、饱和度、明度
qDebug() << color.hue();
qDebug() << color.saturation();
qDebug() << color.value();
}
}
void Widget::InputDialogBtn_Clicked()
{
QInputDialog dlg(this);
dlg.setWindowTitle("Input Test");
dlg.setLabelText("please input an integer: ");
dlg.setInputMode(QInputDialog::IntInput);
//设置输入整型数的范围
dlg.setIntMaximum(10);
dlg.setIntMinimum(0);
if( dlg.exec() == QInputDialog::Accepted)
{
qDebug() << dlg.intValue();
}
}
Widget::~Widget()
{
}
5.字体对话框------QFontDialog类,用于提供选择字体的对话框部件
6.进度对话框------QProgressDialog类
用于显示进度信息,用于需要用户等待的场合
7.打印对话框------QPrintDialog类,用于设置打印相关的参数信息
-
Qt中的QPrint类是打印设备及其参数的封装
-
QPrinter类封装了系统中打印设备的驱动接口
-
QPrinter以相同方式使用系统中的打印设备
void Widget::FontDialogBtn_Clicked()
{
QFontDialog dlg(this);
dlg.setWindowTitle("Font Dialog Test");
dlg.setCurrentFont(QFont("Courier New", 10, QFont::Bold));if( dlg.exec() == QFontDialog::Accepted ) { qDebug() << dlg.selectedFont(); }}
void Widget::ProgressDialogBtn_Clicked()
{
QProgressDialog dlg(this);
dlg.setWindowTitle("Updating");
dlg.setLabelText("Downloading from server...");
dlg.setMinimum(0);
dlg.setMaximum(100);
dlg.setValue(35); //设置当前进度
dlg.exec();
}
void Widget::PrintDialogBtn_Clicked()
{
QPrintDialog dlg(this);
dlg.setWindowTitle("Print Dialog Test");
if( dlg.exec() == QPrintDialog::Accepted )
{
QPrinter* p = dlg.printer(); //从对话框里获取打印机对象指针 p
QTextDocument td; //创建一个文本文档对象td.setPlainText("Printer object test!"); p->setOutputFileName("D:\\test6.pdf"); //把打印机指向 "输出成 PDF 文件" td.print(p); //让文本文档 td 通过打印机 p 打印出去 }}
小结:
Qt中标准对话框的设计模式
- GUI界面部件产生数据对象
- 业务逻辑中的其他对象使用数据对象
- GUI界面与业务逻辑通过数据对象连接