【嵌入式——QT】标准对话框

时间:2024-03-09 19:56:03
#include "DialogDialog.h" #include "ui_DialogDialog.h" #include <QDir> #include <QFileDialog> #include <QColorDialog> #include <QFontDialog> #include <QLineEdit> #include <QInputDialog> #include <QMessageBox> DialogDialog::DialogDialog(QWidget* parent) : QDialog(parent) , ui(new Ui::DialogDialog) { ui->setupUi(this); } DialogDialog::~DialogDialog() { delete ui; } void DialogDialog::on_pushButtonOpen_clicked() { QString curPath = QDir::currentPath(); QString aFileName = QFileDialog::getOpenFileName(this, u8"选择一个文件", curPath); if(!aFileName.isEmpty()) { ui->plainTextEdit->appendPlainText(aFileName); } } void DialogDialog::on_pushButtonOpens_clicked() { QString curPath = QDir::currentPath(); QStringList aFileNames = QFileDialog::getOpenFileNames(this, u8"选择一个文件", curPath); for(int i=0; i<aFileNames.count(); i++) { ui->plainTextEdit->appendPlainText(aFileNames.at(i)); } } void DialogDialog::on_pushButtonSelect_clicked() { QString curPath = QCoreApplication::applicationDirPath(); QString selectedDir = QFileDialog::getExistingDirectory(this, u8"选择一个目录", curPath, QFileDialog::ShowDirsOnly); if(!selectedDir.isEmpty()) { ui->plainTextEdit->appendPlainText(selectedDir); } } void DialogDialog::on_pushButtonSave_clicked() { QString curPath = QCoreApplication::applicationDirPath(); QString fileName = QFileDialog::getSaveFileName(this, u8"保存文件", curPath); if(!fileName.isEmpty()) { ui->plainTextEdit->appendPlainText(fileName); } } void DialogDialog::on_pushButtonColor_clicked() { QPalette pal = ui->plainTextEdit->palette();//获取现有palette QColor initColor = pal.color(QPalette::Text);//现有文字颜色 QColor color = QColorDialog::getColor(initColor, this, u8"选择颜色"); if(color.isValid()) { pal.setColor(QPalette::Text, color); ui->plainTextEdit->setPalette(pal); } } void DialogDialog::on_pushButtonFont_clicked() { QFont initFont = ui->plainTextEdit->font(); bool ok = false; QFont font = QFontDialog::getFont(&ok, initFont); if(ok) { ui->plainTextEdit->setFont(font); } } void DialogDialog::on_pushButtonQuestion_clicked() { QMessageBox::StandardButton defaultBtn = QMessageBox::NoButton; QMessageBox::StandardButton result; result = QMessageBox::question(this, u8"question消息框", u8"文件已被修改,是否保存", QMessageBox::Yes|QMessageBox::No | QMessageBox::Cancel, defaultBtn); if(result == QMessageBox::Yes) { ui->plainTextEdit->appendPlainText(u8"quesstion消息框 Yes 被选择"); } else if(result == QMessageBox::No) { ui->plainTextEdit->appendPlainText(u8"quesstion消息框 No 被选择"); } else if(result == QMessageBox::Cancel) { ui->plainTextEdit->appendPlainText(u8"quesstion消息框 Cancel 被选择"); } else { ui->plainTextEdit->appendPlainText(u8"quesstion消息框无选择"); } } void DialogDialog::on_pushButtonInformation_clicked() { QMessageBox::information(this, u8"information消息框", u8"文件已经打开,字体大小已设置", QMessageBox::Ok, QMessageBox::NoButton); } void DialogDialog::on_pushButtonWarning_clicked() { QMessageBox::warning(this, u8"warning消息框", u8"文件内容已被修改"); } void DialogDialog::on_pushButtonCritical_clicked() { QMessageBox::critical(this, u8"critical消息框", u8"有不明程序访问网络"); } void DialogDialog::on_pushButtonAbout_clicked() { QMessageBox::about(this, u8"about消息框", u8"软件版本1.0.0"); } void DialogDialog::on_pushButtonAboutQt_clicked() { QMessageBox::aboutQt(this, u8"aboutQt消息框"); } void DialogDialog::on_pushButtonStr_clicked() { QLineEdit::EchoMode echoMode = QLineEdit::Normal; bool ok = false; QString text = QInputDialog::getText(this, u8"输入文字对话框", u8"输入文件名", echoMode, u8"新建文件.txt", &ok); if(ok && !text.isEmpty()) { ui->plainTextEdit->appendPlainText(text); } } void DialogDialog::on_pushButtonInt_clicked() { int defaultValue = ui->plainTextEdit->font().pointSize(); int minValue = 6, maxValue = 50, stepValue = 1; bool ok = false; int inputValue = QInputDialog::getInt(this, u8"输入整数对话框", u8"设置字体大小", defaultValue, minValue, maxValue, stepValue, &ok); if(ok) { QFont font = ui->plainTextEdit->font(); font.setPointSize(inputValue); ui->plainTextEdit->setFont(font); } } void DialogDialog::on_pushButtonFloat_clicked() { float defaultValue = 3.13; float minValue = 0, maxValue = 1000; int decimals = 2; bool ok = false; float inputValue = QInputDialog::getDouble(this, u8"输入浮点数对话框", u8"输入浮点数", defaultValue, minValue, maxValue, decimals, &ok); if(ok) { QString str = QString::asprintf(u8"输入一个浮点数:%.2f", inputValue); ui->plainTextEdit->appendPlainText(str); } } void DialogDialog::on_pushButtonItem_clicked() { QStringList items; items<<u8"优秀"<<u8"良好"<<u8"合格"<<u8"不合格"; int curIndex = 0; bool editable = true; bool ok = false; QString text = QInputDialog::getItem(this, u8"条目选择对话框", u8"选择级别", items, curIndex, editable, &ok); if(ok && !text.isEmpty()) { ui->plainTextEdit->appendPlainText(text); } } void DialogDialog::on_pushButtonClear_clicked() { ui->plainTextEdit->clear(); }