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 = 0);~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(20, 20);SimpleMsgBtn.resize(160, 20);CustomMsgBtn.setText("Custom Message Dialog");CustomMsgBtn.move(20, 60);CustomMsgBtn.resize(160, 20);OpenFileBtn.setText("Open File Message Dialog");OpenFileBtn.move(20, 100);OpenFileBtn.resize(160, 20);SaveFileBtn.setText("Save File Message Dialog");SaveFileBtn.move(20, 140);SaveFileBtn.resize(160, 20);QColorBtn.setText("QColor Dialog");QColorBtn.move(20, 180);QColorBtn.resize(160, 20);InputBtn.setText("Input Dialog");InputBtn.move(20, 220);InputBtn.resize(160, 20);QFontBtn.setText("QFont Dialog");QFontBtn.move(20, 260);QFontBtn.resize(160, 20);QProgressBtn.setText("QProgress Dialog");QProgressBtn.move(20, 300);QProgressBtn.resize(160, 20);QPrintBtn.setText("QPrint Dialog");QPrintBtn.move(20, 340);QPrintBtn.resize(160, 20);resize(200, 400);setFixedSize(200, 400);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(100, 111, 240));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", 10, 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(0);dlg.setMaximum(100);dlg.setValue(50);//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() {}
?
轉載于:https://www.cnblogs.com/wulei0630/p/6693409.html
總結
- 上一篇: 《上巳日恩赐曲江宴会即事》第六句是什么
- 下一篇: 位运算: