From cf12d7798a01d4be826c3fc7e6621626d4da3523 Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Wed, 2 Oct 2019 05:15:40 +0800 Subject: [PATCH] feat: QFileDialog QInputDialog and QMessageDialog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QFileDialog  标准文件对话框 QInputDialog 标准输入对话框 QMessageBox 标准消息对话框 QColorDialog 标准颜色对话框 QFontDialog 标准字体对话框 --- QtQDialogEx/ExDialog.cpp | 189 ++++++++++++++++++++++++++++++++++++ QtQDialogEx/ExDialog.h | 42 ++++++++ QtQDialogEx/ExDialog.ui | 181 ++++++++++++++++++++++++++++++++++ QtQDialogEx/QtQDialogEx.pro | 42 ++++++++ QtQDialogEx/main.cpp | 11 +++ QtQDialogEx/qt.ico | Bin 0 -> 4286 bytes 6 files changed, 465 insertions(+) create mode 100644 QtQDialogEx/ExDialog.cpp create mode 100644 QtQDialogEx/ExDialog.h create mode 100644 QtQDialogEx/ExDialog.ui create mode 100644 QtQDialogEx/QtQDialogEx.pro create mode 100644 QtQDialogEx/main.cpp create mode 100644 QtQDialogEx/qt.ico diff --git a/QtQDialogEx/ExDialog.cpp b/QtQDialogEx/ExDialog.cpp new file mode 100644 index 0000000..93fd79b --- /dev/null +++ b/QtQDialogEx/ExDialog.cpp @@ -0,0 +1,189 @@ +#include "ExDialog.h" +#include "ui_ExDialog.h" +#include +#include +#include +#include +#include + +ExDialog::ExDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::ExDialog) +{ + ui->setupUi(this); + setWindowTitle(QObject::tr("文件、颜色、字体、保存、消息、输入等对话框使用")); +} + +ExDialog::~ExDialog() +{ + delete ui; +} + +//标准文件对话框QFileDialog+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +void ExDialog::on_btnOpenFile_clicked() +{ + QString path = QDir::currentPath(); //获取应用程序当前目录 + QString fileter = "文本文件(*.txt);;图片文件(*.jpg *.gif);;所有文件(*.*)"; + + QString fileNmae = QFileDialog::getOpenFileName(this, "选择一个文件", path, fileter); + if (! fileNmae.isEmpty()) { + ui->plainTextEdit->appendPlainText(fileNmae); + } +} + +void ExDialog::on_btnOpenFiles_clicked() +{ + QString path = QDir::currentPath(); //获取应用程序当前目录 + QString fileter = "文本文件(*.txt);;图片文件(*.jpg *.gif);;所有文件(*.*)"; + + QStringList fileNmaeList = QFileDialog::getOpenFileNames(this, "选择多个文件", path, fileter); + for (int i = 0; i < fileNmaeList.count(); i++) { + ui->plainTextEdit->appendPlainText(fileNmaeList.at(i)); + } +} + +void ExDialog::on_btnExistingDir_clicked() +{ + QString currPath = QCoreApplication::applicationDirPath(); //获取应用程序当前目录 + QString path = QFileDialog::getExistingDirectory(this, "选择一个目录【非文件】", currPath, QFileDialog::ShowDirsOnly); //最后一个参数,表示只显示路径 + + if (!path.isEmpty()) { + ui->plainTextEdit->appendPlainText(path); + } +} + +void ExDialog::on_btnGetColor_clicked() +{ + QPalette pal = ui->plainTextEdit->palette(); //获取条调色板 + QColor initColor = pal.color(QPalette::Text); + QColor color = QColorDialog::getColor(initColor, this, "选择颜色"); + + if (color.isValid()) { //因为没有.isEmpty(),故而使用.isValid()来判断 + pal.setColor(QPalette::Text, color); + ui->plainTextEdit->setPalette(pal); + } +} + +void ExDialog::on_btnGetFont_clicked() +{ + QFont initFont = ui->plainTextEdit->font(); + bool ok = false; + QFont font = QFontDialog::getFont(&ok, initFont); + + if (ok) + ui->plainTextEdit->setFont(font); +} + +void ExDialog::on_btnSaveFile_clicked() +{ + QString path = QDir::currentPath(); //获取应用程序当前目录 + QString fileter = "头文件(*.h);;源文件(*.cpp);;所有文件(*.*)"; + QString fileNmae = QFileDialog::getSaveFileName(this, "保存文件", path, fileter); + + if (!fileNmae.isEmpty()) + ui->plainTextEdit->appendPlainText(fileNmae); +} + +//标准消息对话框+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +void ExDialog::on_btnQuestion_clicked() +{ + QMessageBox::StandardButton ret = QMessageBox::question(this, "问题消息对话框", "question对话框的内容", QMessageBox::Yes | QMessageBox::No | QMessageBox::Close, QMessageBox::NoButton); + + switch (ret) { + case QMessageBox::Yes: { + ui->plainTextEdit->appendPlainText("QMessageBox::yes 按钮被选中"); + break; + } + case QMessageBox::No: { + ui->plainTextEdit->appendPlainText("QMessageBox::No 按钮被选中"); + break; + } + case QMessageBox::Close: { + ui->plainTextEdit->appendPlainText("QMessageBox::Close 按钮被选中"); + break; + } + default: { + ui->plainTextEdit->appendPlainText("这是 switch 的default 的选项"); + break; + } + } +} + +void ExDialog::on_btnInformation_clicked() +{ + QMessageBox::information(this, "信息消息对话框", "information对话框的内容", QMessageBox::Ok, QMessageBox::NoButton); +} + +void ExDialog::on_btnWarning_clicked() +{ + QMessageBox::warning(this, "警告消息对话框", "warning对话框的内容", QMessageBox::Ok, QMessageBox::NoButton); +} + +void ExDialog::on_btnCritical_clicked() +{ + QMessageBox::critical(this, "危机消息对话框", "critical对话框的内容", QMessageBox::Ok, QMessageBox::NoButton); +} + +void ExDialog::on_btnAbout_clicked() +{ + QMessageBox::about(this, "关于消息对话框", "abou 作者: 投我以木李,报之以琼玖"); +} + +void ExDialog::on_btnAboutQt_clicked() +{ + QMessageBox::aboutQt(this, "关于Qt消息对话框"); +} + +//标准输入对话框QInputDialog+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +void ExDialog::on_btnGetString_clicked() +{ + bool ok = false; + QString text = QInputDialog::getText(this, "输入文字对话框", "请输入一个字符串", QLineEdit::Normal, "默认输入的字符串", &ok); + + if (ok && !text.isEmpty()) + ui->plainTextEdit->appendPlainText(text); +} + +void ExDialog::on_btnGetItem_clicked() +{ + QStringList list; + list<<"2019-10-02"<<"04:28"<<"在武汉的卧室"<<"敲代码"<<"这会没有困意"; + + int index = 0; + bool editable = true; //ComboBox是否可编辑 + bool ok = false; + QString text = QInputDialog::getItem(this, "输入item对话框", "请选择一个item", list, index, editable, &ok); + + if (ok && !text.isEmpty()) + ui->plainTextEdit->appendPlainText(text); +} + +void ExDialog::on_btnInt_clicked() +{ + int min = 0; + int max = 100; + int stepVal = 3; + int size = ui->plainTextEdit->font().pointSize(); + bool ok = false; + int val = QInputDialog::getInt(this, "输入整数对话框", "请输入一个整数改变字体大小", size, min, max, stepVal, &ok); + + if (ok) { + QFont font = ui->plainTextEdit->font(); + font.setPointSize(val); + ui->plainTextEdit->setFont(font); + ui->plainTextEdit->appendPlainText("字体大小已经被设置为:" + QString::number(val)); + } +} + +void ExDialog::on_btnDouble_clicked() +{ + int min = 0; + int max = 100; + int d = 2; //小数点的位数 + double val = 3.1415; + bool ok = false; + double ret = QInputDialog::getDouble(this, "输入浮点数对话框", "请输入一个整数改变字体大小", d, min, max, val, &ok); + + if (ok) + ui->plainTextEdit->appendPlainText("浮点数大小为:" + QString::number(ret, 'f', 4)); +} diff --git a/QtQDialogEx/ExDialog.h b/QtQDialogEx/ExDialog.h new file mode 100644 index 0000000..097ee8c --- /dev/null +++ b/QtQDialogEx/ExDialog.h @@ -0,0 +1,42 @@ +#ifndef EXDIALOG_H +#define EXDIALOG_H + +#include + +namespace Ui { +class ExDialog; +} + +class ExDialog : public QDialog +{ + Q_OBJECT + +public: + explicit ExDialog(QWidget *parent = nullptr); + ~ExDialog(); + +private slots: + void on_btnOpenFile_clicked(); //打开一个文件 + void on_btnOpenFiles_clicked(); //打开多个文件 + void on_btnExistingDir_clicked(); //选择已有目录 + void on_btnGetColor_clicked(); //选择颜色 + void on_btnGetFont_clicked(); //选择字体 + void on_btnSaveFile_clicked(); //保存文件 + + void on_btnQuestion_clicked(); + void on_btnInformation_clicked(); + void on_btnWarning_clicked(); + void on_btnCritical_clicked(); + void on_btnAbout_clicked(); + void on_btnAboutQt_clicked(); + + void on_btnGetString_clicked(); //输入字符串 + void on_btnGetItem_clicked(); //item选择输入 + void on_btnInt_clicked(); //输入整数 + void on_btnDouble_clicked(); //输入浮点数 + +private: + Ui::ExDialog *ui; +}; + +#endif // EXDIALOG_H diff --git a/QtQDialogEx/ExDialog.ui b/QtQDialogEx/ExDialog.ui new file mode 100644 index 0000000..24c8559 --- /dev/null +++ b/QtQDialogEx/ExDialog.ui @@ -0,0 +1,181 @@ + + + ExDialog + + + + 0 + 0 + 663 + 341 + + + + ExDialog + + + + + + 标准对话框: + + + + + + 打开多个文件 + + + + + + + Qt::Horizontal + + + + + + + 输入字符串 + + + + + + + item选择输入 + + + + + + + 标准消息框: + + + + + + + 输入整数 + + + + + + + 输入浮点数 + + + + + + + 文件对话框: + + + + + + + 选择已有目录 + + + + + + + Qt::Vertical + + + + + + + 选择字体 + + + + + + + 选择颜色 + + + + + + + question + + + + + + + warning + + + + + + + critical + + + + + + + information + + + + + + + about + + + + + + + aboutQt + + + + + + + 保存文件 + + + + + + + 打开一个文件 + + + + + + + 标准输入对话框QInputDialog: + + + + + + + + + + + + + + + diff --git a/QtQDialogEx/QtQDialogEx.pro b/QtQDialogEx/QtQDialogEx.pro new file mode 100644 index 0000000..b3659ce --- /dev/null +++ b/QtQDialogEx/QtQDialogEx.pro @@ -0,0 +1,42 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-10-01T15:57:01 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtQDialogEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExDialog.cpp + +HEADERS += \ + ExDialog.h + +FORMS += \ + ExDialog.ui + +RC_ICONS += qt.ico + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtQDialogEx/main.cpp b/QtQDialogEx/main.cpp new file mode 100644 index 0000000..3ad5fd0 --- /dev/null +++ b/QtQDialogEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExDialog.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExDialog w; + w.show(); + + return a.exec(); +} diff --git a/QtQDialogEx/qt.ico b/QtQDialogEx/qt.ico new file mode 100644 index 0000000000000000000000000000000000000000..0ee0568945377c30d5d973d89afcc31770cd8818 GIT binary patch literal 4286 zcmeHKdr(y86+io87ZyYwA_hySrjx{)Njo~7w3D=Go0O&*)2xbtP|_lTj{v^F6l<|4 z!TRE16coi*F_>DPiHV~$YSL&Lgk=|&m4~p)V}S)!47%0a{(A22URjA+|FAR3OzzI_ zp6@&7JHKBO?Lu5#%-ZBiRg$@WRi6TzUI$u#k!Uz4@+6%sBmDt|-5nU_e| z7fHeg@p7SVjknOZZ=`Vjy7MivgQDE}G#O0Oe_VLG8&NBA&`P!TV~z&n1pb#e?G?}cIT z&=`F2_fRJW4~F8#u}5(8_!tb9kHklFz0kAS7lZplFi;kZfgNhJq$t6h5IOG!{VQ~X z2S8Ikg_Bh8jEmPp%hXZkq`TBPS!M-yb_T3X;-}6Hkc0Gxn(s1;a2I2DF zQMg(b%I!v@XIn5j-}YnqC>qnhiyATZjC(_j`NC+@{_Pi}tBIyBD_$a)m&h4GCPW}OX$I{VzbSW->umrt_;?Pqz6}FObux$xM$Ce*qcgVDYBr!b^G2IH$T7-M9pWr$KL5339F zo}z&LQ50A4GT};|0`{%`=qwIL&%P)$WlzGtbj%TVcYHow2J@TWhi&JxXnQ-1;|HOu z1eh#6pNW=5zkM_;sXkHPDN*+Jc>vGiH2i4rS`h|XI zD+obbzCXG)`NFt>P!)G?KZ(=yJ2?s?X>55E&9|)}bZ^!&#|T*eqGf*>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