1、获取文件路径
(QFileDialog::getOpenFileName)
setWindowTitle("标准文本对话框");
m_grodLo = new QGridLayout(this);
m_fileBtn = new QPushButton("文件路径");//编辑按钮文本
m_fileEdit = new QLineEdit();
m_grodLo->addWidget(m_fileBtn,0,0);//加入部件布局(按钮,X,Y)
m_grodLo->addWidget(m_fileEdit,0,1);
//使用槽函数
connect(m_fileBtn,&QPushButton::clicked,this,&Dialog::ShowFileDia);
void Dialog::ShowFileDia()
{
//getOpenFileName(指定父类,标题,索引(如有多个使用;;隔开))
QString sPath = QFileDialog::getOpenFileName(this,"标准文件对话框",".",
"C++ files(*.cpp);;"
"C files(*.C);;"
"header files(*.h)");
m_fileEdit->setText(sPath);
}
2、使用调色板(QColor\QColorDialog)
m_colorBtn = new QPushButton("获取颜色标准");
m_colorFrame = new QFrame;
m_colorFrame->setFrameStyle(QFrame::Box);//设置边框风格
m_colorFrame->setAutoFillBackground(true);//设置填充背景属性
void Dialog::ShowColorDia()
{
QColor color = QColorDialog::getColor(Qt::blue);
if(color.isValid()){//判断是否有效
m_colorFrame->setPalette(QPalette(color));//将选择颜色填充
}
3、文字
m_fontBtn = new QPushButton("获取所选文字对话框");
m_fontEdit = new QLineEdit("一天敲一个");
void Dialog::ShowFontDia()
{
bool ok;
QFont font = QFontDialog::getFont(&ok);
if(ok)m_fontEdit->setFont(font);
}
4、修改信息,text,下拉选项,数字加减----QInputDialog
#include "inputdia.h"
#include<QInputDialog>
inputDia::inputDia(QWidget *parent):QDialog(parent)
{
setWindowTitle("学生信息");
m_nameLabel = new QLabel("姓名:");
m_sexLable = new QLabel("性别:");
m_ageLabel = new QLabel("年龄:");
m_scoreLable = new QLabel("成绩");
m_nameEdit = new QLabel("图图");
m_sexEdit = new QLabel("男");
m_ageEdit = new QLabel("8");
m_scoreEdit = new QLabel("90");
m_nameBtn = new QPushButton("修改姓名");
m_sexBtn = new QPushButton("修改性别");
m_ageBtn = new QPushButton("修改年龄");
m_scoreBtn = new QPushButton("修改成绩");
m_layout = new QGridLayout(this);
m_layout->addWidget(m_nameLabel,0,0);
m_layout->addWidget(m_nameEdit,0,1);
m_layout->addWidget(m_nameBtn,0,2);
m_layout->addWidget(m_sexLable,1,0);
m_layout->addWidget(m_sexEdit,1,1);
m_layout->addWidget(m_sexBtn,1,2);
m_layout->addWidget(m_ageLabel,2,0);
m_layout->addWidget(m_ageEdit,2,1);
m_layout->addWidget(m_ageBtn,2,2);
m_layout->addWidget(m_scoreLable,3,0);
m_layout->addWidget(m_scoreEdit,3,1);
m_layout->addWidget(m_scoreBtn,3,2);
m_layout->setSpacing(10);
m_layout->setMargin(10);
connect(m_nameBtn,&QPushButton::clicked,this,&inputDia::ShowName);
connect(m_sexBtn,&QPushButton::clicked,this,&inputDia::ShowSex);
connect(m_ageBtn,&QPushButton::clicked,this,&inputDia::ShowAge);
connect(m_scoreBtn,&QPushButton::clicked,this,&inputDia::ShowScore);
}
void inputDia::ShowName()
{
bool ok;
QString name = QInputDialog::getText(this,"更新名字","输入名字",QLineEdit::Normal,
m_nameEdit->text(),&ok);
if(ok)m_nameEdit->setText(name);
}
void inputDia::ShowSex()
{
QStringList sexList;
sexList << "男" << "女" << "太监";
bool ok;
QString sex = QInputDialog::getItem(this,"修改性别","选择性别",sexList,0,false,&ok);
if(ok&&!sex.isEmpty()) m_sexEdit->setText(sex);
}
void inputDia::ShowAge()
{
bool ok;
int age = QInputDialog::getInt(this,"修改年龄框","修改年龄",m_ageEdit->text().toInt(),
0,100,1,&ok);
if(ok) m_ageEdit->setText(QString::number(age));
}
void inputDia::ShowScore()
{
bool ok;
double score = QInputDialog::getDouble(this,"修改成绩框","修改成绩",m_scoreEdit->text().toDouble(),
0,100,1,&ok);
if(ok) m_scoreEdit->setText(QString::number(score));
}
5、提示框
#include "msgboxdlg.h"
#include<QMessageBox>
msgBoxDlg::msgBoxDlg(QWidget *parent):QDialog(parent)
{
setWindowTitle("标准对话框集合");
m_tipLabel = new QLabel("请选择一个消息框");
m_questionBtn = new QPushButton("问题消息框");
m_informationBtn = new QPushButton("信息消息框");
m_warningBtn = new QPushButton("警告消息框");
m_critialBtn = new QPushButton("错误消息框");
m_aboutBtn = new QPushButton("关于消息框");
m_aboutQtBtn = new QPushButton("Qt消息框");
m_mainLayout = new QGridLayout(this);
m_mainLayout->addWidget(m_tipLabel,0,0,1,2);
m_mainLayout->addWidget(m_questionBtn,1,0);
m_mainLayout->addWidget(m_informationBtn,1,1);
m_mainLayout->addWidget(m_warningBtn,2,0);
m_mainLayout->addWidget(m_critialBtn,2,1);
m_mainLayout->addWidget(m_aboutBtn,3,0);
m_mainLayout->addWidget(m_aboutQtBtn,3,1);
connect(m_questionBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowQustion);
connect(m_informationBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowInformation);
connect(m_warningBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowWarning);
connect(m_critialBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowCritial);
connect(m_aboutBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowAbout);
connect(m_aboutQtBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowAboutQt);
}
void msgBoxDlg::ShowQustion()
{
m_tipLabel->setText("问题消息提示框");
int res = QMessageBox::question(this,"消息提示框","已打开提示框,关闭?",
QMessageBox::Ok|QMessageBox::Cancel,
QMessageBox::Ok);
switch (res) {
case QMessageBox::Ok:
m_tipLabel->setText("确定消息");
break;
case QMessageBox::Cancel:
m_tipLabel->setText("取消消息");
break;
default:
break;
}
}
void msgBoxDlg::ShowInformation()
{
m_tipLabel->setText("信息提示框");
QMessageBox::information(this,"信息提示框","你是猪");
}
void msgBoxDlg::ShowWarning()
{
m_tipLabel->setText("警告提示框");
int box = QMessageBox::warning(this,"警告提示框","是否保存文件",
QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,
QMessageBox::Save);
switch (box) {
case QMessageBox::Save:
m_tipLabel->setText("保存文件");
break;
case QMessageBox::Discard:
m_tipLabel->setText("不保存");
break;
case QMessageBox::Cancel:
m_tipLabel->setText("取消");
break;
default:
break;
}
}
void msgBoxDlg::ShowCritial()
{
m_tipLabel->setText("错误提示框");
QMessageBox::critical(this,"错误消息框","不可能绝对不可能");
}
void msgBoxDlg::ShowAbout()
{
m_tipLabel->setText("消息提示框");
QMessageBox::about(this,"消息提示框","no money");
}
void msgBoxDlg::ShowAboutQt()
{
m_tipLabel->setText("Qt消息提示");
QMessageBox::aboutQt(this,"Qt消息框");
}
6、所有代码
1、dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QPushButton>
#include<QLineEdit>
#include<QGridLayout>
#include"inputdia.h"
#include"msgboxdlg.h"
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void ShowFileDia();
void ShowColorDia();
void ShowFontDia();
void ShowInputDia();
void ShowMsgBox();
void ShowCust();
private:
QPushButton *m_fileBtn;//文件按钮
QLineEdit *m_fileEdit;//获取文件框
QGridLayout *m_grodLo;//布局
QPushButton *m_colorBtn;//颜色按钮
QFrame *m_colorFrame;//显示所选颜色效果
QPushButton *m_fontBtn;//打开文字对话框
QLineEdit *m_fontEdit;//显示文字的效果
QPushButton *m_inputBtn;//学生信息
inputDia *m_input;//信息类
QPushButton *m_magBoxBtn;
msgBoxDlg *m_msgBox;//提示类
QPushButton *m_custBomBtn;
QLabel *m_custLable;
};
#endif // DIALOG_H
2、dialog.cpp
#include "dialog.h"
#include<QFileDialog>
#include<QColorDialog>
#include<QFontDialog>
#include<QMessageBox>
Dialog::Dialog(QWidget *parent):QDialog(parent)
{
setWindowTitle("标准文本对话框");
m_grodLo = new QGridLayout(this);
m_fileBtn = new QPushButton("获取文件路径按钮");//编辑按钮文本
m_fileEdit = new QLineEdit();
m_colorBtn = new QPushButton("获取颜色标准");
m_colorFrame = new QFrame;
m_fontBtn = new QPushButton("获取所选文字对话框");
m_fontEdit = new QLineEdit("一天敲一个");
m_inputBtn = new QPushButton("学生信息");
m_magBoxBtn = new QPushButton("提示信息");
m_custBomBtn = new QPushButton("自定义按钮");
m_custLable = new QLabel;
m_custLable->setFrameStyle(QFrame::Panel|QFrame::Sunken);
m_grodLo->addWidget(m_fileBtn,0,0);//加入部件布局(按钮,X,Y)
m_grodLo->addWidget(m_fileEdit,0,1);
m_grodLo->addWidget(m_colorBtn,1,0);
m_grodLo->addWidget(m_colorFrame,1,1);
m_grodLo->addWidget(m_fontBtn,2,0);
m_grodLo->addWidget(m_fontEdit,2,1);
m_grodLo->addWidget(m_inputBtn,3,0);
m_grodLo->addWidget(m_magBoxBtn,3,1);
m_grodLo->addWidget(m_custBomBtn,4,0);
m_grodLo->addWidget(m_custLable,4,1);
m_colorFrame->setFrameStyle(QFrame::Box);//设置边框风格
m_colorFrame->setAutoFillBackground(true);//设置填充背景属性
//槽函数,获取文件路径
connect(m_fileBtn,&QPushButton::clicked,this,&Dialog::ShowFileDia);
//颜色对话框
connect(m_colorBtn,&QPushButton::clicked,this,&Dialog::ShowColorDia);
//文字
connect(m_fontBtn,&QPushButton::clicked,this,&Dialog::ShowFontDia);
//学生信息
connect(m_inputBtn,&QPushButton::clicked,this,&Dialog::ShowInputDia);
//消息提示
connect(m_magBoxBtn,&QPushButton::clicked,this,&Dialog::ShowMsgBox);
//自定义
connect(m_custBomBtn,&QPushButton::clicked,this,&Dialog::ShowCust);
}
Dialog::~Dialog()
{
}
void Dialog::ShowFileDia()
{
//getOpenFileName(指定父类,标题,索引(如有多个使用;;隔开))
QString sPath = QFileDialog::getOpenFileName(this,"标准文件对话框",".",
"C++ files(*.cpp);;"
"C files(*.C);;"
"header files(*.h)");
m_fileEdit->setText(sPath);
}
void Dialog::ShowColorDia()
{
QColor color = QColorDialog::getColor(Qt::blue);
if(color.isValid()){//判断是否有效
m_colorFrame->setPalette(QPalette(color));//将选择颜色填充
}
}
void Dialog::ShowFontDia()
{
bool ok;
QFont font = QFontDialog::getFont(&ok);
if(ok)m_fontEdit->setFont(font);
}
void Dialog::ShowInputDia()
{
m_input = new inputDia(this);
m_input->show();
}
void Dialog::ShowMsgBox()
{
m_msgBox = new msgBoxDlg(this);
m_msgBox->show();
}
void Dialog::ShowCust()
{
m_custLable->setText("自定义消息框");
QMessageBox custtom;
custtom.setWindowTitle("自定义消息框");
QPushButton *yes = custtom.addButton("好",QMessageBox::ActionRole);
QPushButton *no = custtom.addButton("不好",QMessageBox::ActionRole);
QPushButton *cancel = custtom.addButton(QMessageBox::Cancel);
custtom.setIconPixmap(QPixmap("调色板.png"));
custtom.exec();
if(custtom.clickedButton() == yes){
m_custLable->setText("好好好");
}else if(custtom.clickedButton() == no){
m_custLable->setText("不好不好不好");
}else if(custtom.clickedButton() == cancel){
m_custLable->setText("取消");
}
}
3、inputdia.h
#ifndef INPUTDIA_H
#define INPUTDIA_H
#include<QDialog>
#include<QLabel>
#include<QPushButton>
#include<QGridLayout>
class inputDia : public QDialog
{
Q_OBJECT
public:
inputDia(QWidget *parent);
private slots:
void ShowName();
void ShowSex();
void ShowAge();
void ShowScore();
private:
QLabel *m_nameLabel;//名字
QLabel *m_ageLabel;//年龄
QLabel *m_sexLable;//性别
QLabel *m_scoreLable;//成绩
QLabel *m_nameEdit;
QLabel *m_sexEdit;
QLabel *m_ageEdit;
QLabel *m_scoreEdit;
QPushButton *m_nameBtn;
QPushButton *m_sexBtn;
QPushButton *m_ageBtn;
QPushButton *m_scoreBtn;
QGridLayout *m_layout;
};
#endif // INPUTDIA_H
4、inputdia.cpp
#include "inputdia.h"
#include<QInputDialog>
inputDia::inputDia(QWidget *parent):QDialog(parent)
{
setWindowTitle("学生信息");
m_nameLabel = new QLabel("姓名:");
m_sexLable = new QLabel("性别:");
m_ageLabel = new QLabel("年龄:");
m_scoreLable = new QLabel("成绩");
m_nameEdit = new QLabel("图图");
m_sexEdit = new QLabel("男");
m_ageEdit = new QLabel("8");
m_scoreEdit = new QLabel("90");
m_nameBtn = new QPushButton("修改姓名");
m_sexBtn = new QPushButton("修改性别");
m_ageBtn = new QPushButton("修改年龄");
m_scoreBtn = new QPushButton("修改成绩");
m_layout = new QGridLayout(this);
m_layout->addWidget(m_nameLabel,0,0);
m_layout->addWidget(m_nameEdit,0,1);
m_layout->addWidget(m_nameBtn,0,2);
m_layout->addWidget(m_sexLable,1,0);
m_layout->addWidget(m_sexEdit,1,1);
m_layout->addWidget(m_sexBtn,1,2);
m_layout->addWidget(m_ageLabel,2,0);
m_layout->addWidget(m_ageEdit,2,1);
m_layout->addWidget(m_ageBtn,2,2);
m_layout->addWidget(m_scoreLable,3,0);
m_layout->addWidget(m_scoreEdit,3,1);
m_layout->addWidget(m_scoreBtn,3,2);
m_layout->setSpacing(10);
m_layout->setMargin(10);
connect(m_nameBtn,&QPushButton::clicked,this,&inputDia::ShowName);
connect(m_sexBtn,&QPushButton::clicked,this,&inputDia::ShowSex);
connect(m_ageBtn,&QPushButton::clicked,this,&inputDia::ShowAge);
connect(m_scoreBtn,&QPushButton::clicked,this,&inputDia::ShowScore);
}
void inputDia::ShowName()
{
bool ok;
QString name = QInputDialog::getText(this,"更新名字","输入名字",QLineEdit::Normal,
m_nameEdit->text(),&ok);
if(ok)m_nameEdit->setText(name);
}
void inputDia::ShowSex()
{
QStringList sexList;
sexList << "男" << "女" << "太监";
bool ok;
QString sex = QInputDialog::getItem(this,"修改性别","选择性别",sexList,0,false,&ok);
if(ok&&!sex.isEmpty()) m_sexEdit->setText(sex);
}
void inputDia::ShowAge()
{
bool ok;
int age = QInputDialog::getInt(this,"修改年龄框","修改年龄",m_ageEdit->text().toInt(),
0,100,1,&ok);
if(ok) m_ageEdit->setText(QString::number(age));
}
void inputDia::ShowScore()
{
bool ok;
double score = QInputDialog::getDouble(this,"修改成绩框","修改成绩",m_scoreEdit->text().toDouble(),
0,100,1,&ok);
if(ok) m_scoreEdit->setText(QString::number(score));
}
5、msgboxdlg.h
#ifndef MSGBOXDLG_H
#define MSGBOXDLG_H
#include<QDialog>
#include<QLabel>
#include<QPushButton>
#include<QGridLayout>
class msgBoxDlg : public QDialog
{
public:
msgBoxDlg(QWidget *parent);
private slots:
void ShowQustion();
void ShowInformation();
void ShowWarning();
void ShowCritial();
void ShowAbout();
void ShowAboutQt();
private:
QLabel *m_tipLabel;
QPushButton *m_questionBtn;
QPushButton *m_informationBtn;
QPushButton *m_warningBtn;
QPushButton *m_critialBtn;
QPushButton *m_aboutBtn;
QPushButton *m_aboutQtBtn;
QGridLayout *m_mainLayout;
};
#endif // MSGBOXDLG_H
6、msgboxdlg.cpp
#include "msgboxdlg.h"
#include<QMessageBox>
msgBoxDlg::msgBoxDlg(QWidget *parent):QDialog(parent)
{
setWindowTitle("标准对话框集合");
m_tipLabel = new QLabel("请选择一个消息框");
m_questionBtn = new QPushButton("问题消息框");
m_informationBtn = new QPushButton("信息消息框");
m_warningBtn = new QPushButton("警告消息框");
m_critialBtn = new QPushButton("错误消息框");
m_aboutBtn = new QPushButton("关于消息框");
m_aboutQtBtn = new QPushButton("Qt消息框");
m_mainLayout = new QGridLayout(this);
m_mainLayout->addWidget(m_tipLabel,0,0,1,2);
m_mainLayout->addWidget(m_questionBtn,1,0);
m_mainLayout->addWidget(m_informationBtn,1,1);
m_mainLayout->addWidget(m_warningBtn,2,0);
m_mainLayout->addWidget(m_critialBtn,2,1);
m_mainLayout->addWidget(m_aboutBtn,3,0);
m_mainLayout->addWidget(m_aboutQtBtn,3,1);
connect(m_questionBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowQustion);
connect(m_informationBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowInformation);
connect(m_warningBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowWarning);
connect(m_critialBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowCritial);
connect(m_aboutBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowAbout);
connect(m_aboutQtBtn,&QPushButton::clicked,this,&msgBoxDlg::ShowAboutQt);
}
void msgBoxDlg::ShowQustion()
{
m_tipLabel->setText("问题消息提示框");
int res = QMessageBox::question(this,"消息提示框","已打开提示框,关闭?",
QMessageBox::Ok|QMessageBox::Cancel,
QMessageBox::Ok);
switch (res) {
case QMessageBox::Ok:
m_tipLabel->setText("确定消息");
break;
case QMessageBox::Cancel:
m_tipLabel->setText("取消消息");
break;
default:
break;
}
}
void msgBoxDlg::ShowInformation()
{
m_tipLabel->setText("信息提示框");
QMessageBox::information(this,"信息提示框","你是猪");
}
void msgBoxDlg::ShowWarning()
{
m_tipLabel->setText("警告提示框");
int box = QMessageBox::warning(this,"警告提示框","是否保存文件",
QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,
QMessageBox::Save);
switch (box) {
case QMessageBox::Save:
m_tipLabel->setText("保存文件");
break;
case QMessageBox::Discard:
m_tipLabel->setText("不保存");
break;
case QMessageBox::Cancel:
m_tipLabel->setText("取消");
break;
default:
break;
}
}
void msgBoxDlg::ShowCritial()
{
m_tipLabel->setText("错误提示框");
QMessageBox::critical(this,"错误消息框","不可能绝对不可能");
}
void msgBoxDlg::ShowAbout()
{
m_tipLabel->setText("消息提示框");
QMessageBox::about(this,"消息提示框","no money");
}
void msgBoxDlg::ShowAboutQt()
{
m_tipLabel->setText("Qt消息提示");
QMessageBox::aboutQt(this,"Qt消息框");
}