对话框
界面
代码
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <QColorDialog>
#include <QFontDialog>
#include <QInputDialog>
#include <QMessageBox>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_btnClearText_clicked()
{
ui->plainTextEdit->clear();
}
void Dialog::on_btnOpen_clicked()
{
QString curPath = QDir::currentPath(); //对话框默认打开目录
QString dlgTitle = "选择对话框";
QString filter = "文本文件(*.txt);;图片文件(*.jpg *.gif);;所有格式(*.*)";
QString fileName = QFileDialog::getOpenFileName(this, dlgTitle, curPath,
filter);
if(fileName.isEmpty())
return ;
ui->plainTextEdit->appendPlainText(fileName);
}
void Dialog::on_btnOpenMulti_clicked()
{
QString curPath = QDir::currentPath();
QString dlgTitle = "选择对话框";
QString filter = "文本文件(*.txt);;图片文件(*.jpg *.gif);;所有格式(*.*)";
QStringList fileNames = QFileDialog::getOpenFileNames(this, dlgTitle, curPath,
filter);
if(fileNames.count() == 0)
return ;
foreach(auto str, fileNames){
ui->plainTextEdit->appendPlainText(str);
}
}
void Dialog::on_btnSelDir_clicked()
{
QString curPath = QDir::currentPath();
QString dlgTitle = "选择对话框";
//QString filter = "文本文件(*.txt);;图片文件(*.jpg *.gif);;所有格式(*.*)";
QString selectedDir = QFileDialog::getExistingDirectory(this, dlgTitle, curPath, QFileDialog::ShowDirsOnly); //只显示目录
if(selectedDir.isEmpty())
return ;
ui->plainTextEdit->appendPlainText(selectedDir);
}
void Dialog::on_btnSave_clicked()
{
QString curPath = QCoreApplication::applicationDirPath(); //对话框默认打开目录
QString dlgTitle = "选择对话框";
QString filter = "文本文件(*.txt);;图片文件(*.jpg *.gif);;所有格式(*.*)";
QString fileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath,
filter);
if(fileName.isEmpty())
return ;
ui->plainTextEdit->appendPlainText("保存文件:" + fileName);
}
void Dialog::on_btnColor_clicked()
{
QPalette pal = ui->plainTextEdit->palette();
QColor initColor = pal.color(QPalette::Text);
QColor color = QColorDialog::getColor(initColor, this, "选择一个颜色");
if(!color.isValid())
return;
pal.setColor(QPalette::Text, color);
ui->plainTextEdit->setPalette(pal);
}
void Dialog::on_btnFont_clicked()
{
bool ok = false;
QFont initFont = ui->plainTextEdit->font();
QFont font = QFontDialog::getFont(&ok, initFont, this, "选择字体");
if(!ok)
return;
ui->plainTextEdit->setFont(font);
}
void Dialog::on_btnInputString_clicked()
{
QString dlgTitle = "输入文字对话框";
QString txtLabel = "请输入文件名";
QString defaultInput = "xxxx.txt";
QLineEdit::EchoMode echoMode = QLineEdit::Normal;
bool ok = false;
QString str = QInputDialog::getText(this, dlgTitle, txtLabel,
echoMode, defaultInput, &ok);
if(!ok) return ;
ui->plainTextEdit->appendPlainText(str);
}
void Dialog::on_btnInputInt_clicked()
{
QString dlgTitle = "输入整数对话框";
QString txtLabel = "请输入整数";
int defaultVal = ui->plainTextEdit->font().pointSize();
int minValue = 0, maxValue = 100;
bool ok = false;
int val = QInputDialog::getInt(this, dlgTitle, txtLabel,
defaultVal, minValue, maxValue,
1, &ok); //步长为1
if(!ok) return ;
QFont font = ui->plainTextEdit->font();
font.setPointSize(val);
ui->plainTextEdit->setFont(font);
ui->plainTextEdit->appendPlainText(QString::asprintf("输入了一个整数:%d", val));
}
void Dialog::on_btnInputFloat_clicked()
{
QString dlgTitle = "输入浮点数对话框";
QString txtLabel = "请输入浮点数";
double defaultVal = 15.0;
double minValue = 0, maxValue = 100;
bool ok = false;
double val = QInputDialog::getDouble(this, dlgTitle, txtLabel,
defaultVal, minValue, maxValue,
2, &ok); //小数点后2位
if(!ok) return ;
ui->plainTextEdit->appendPlainText(QString::asprintf("输入了一个浮点数:%.2f", val));
}
void Dialog::on_btnInputItem_clicked()
{
QStringList items;
items << "优先" << "良好" << "及格" << "不及格";
QString dlgTitle = "输入条目对话框";
QString txtLabel = "请选择条目";
bool ok = false;
QString text = QInputDialog::getItem(this, dlgTitle, txtLabel, items, 0,
true, &ok); //true表示除了选择还可以输入
if(!ok || text.isEmpty()) return ;
ui->plainTextEdit->appendPlainText(text);
}
void Dialog::on_btnMsgQuestion_clicked()
{
QString dlgTitle = "Question消息框";
QString txtLabel = "文件已被修改,是否保存?";
QMessageBox::StandardButtons result;
result = QMessageBox::question(this, dlgTitle, txtLabel,
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
QMessageBox::NoButton);
QString str;
if(result == QMessageBox::Yes)
str = "Question消息框 yes被选中";
else if(result == QMessageBox::No)
str = "Question消息框 No被选中";
else if(result == QMessageBox::Cancel)
str = "Question消息框 Cancel被选中";
ui->plainTextEdit->appendPlainText(str);
}
void Dialog::on_btnMsgInformation_clicked()
{
QMessageBox::information(this, "Title", "TXT", QMessageBox::Ok);
}
void Dialog::on_btnMsgWarning_clicked()
{
QMessageBox::warning(this, "Title", "TXT", QMessageBox::Ok);
}
void Dialog::on_btnMsgCritical_clicked()
{
QMessageBox::critical(this, "Title", "TXT", QMessageBox::Ok);
}
void Dialog::on_btnMsgAbout_clicked()
{
QMessageBox::about(this, "Title", "TXT");
}
void Dialog::on_btnMsgAboutQt_clicked()
{
QMessageBox::aboutQt(this, "Title");
}
主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈