
正文
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()
{ }







