这个是书上的一个示例,有需要的可以去看看书,书上的案例挺全的。
效果展示:
1、选择文本框实例,可以打开文件目录,并将文件路径显示在文本框中;
2、点击颜色标准对话框实例,可打开一个Color插件,选择颜色,实现文本框的填充;
3、点击字体标准对话框按钮,打开字体插件,转换字体大小,可调整文本框中的“Hello World”;
4、点击标准输入对话框按钮,可查看信息科进行修改,如图2所示;
5、点击用户自定义消息对话框实例,程序会弹出对话框,如图3所示;
图1:
图2:
图3:
代码部分:
dialog.cpp
#include "dialog.h"
#include<QFileDialog>
#include<QColorDialog>
#include<QPalette>
#include<QFontDialog>
#include<QMessageBox>
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("各种标准对话框的示例"));
fileBtn = new QPushButton;
fileBtn->setText(tr("文件标准对话框实例"));
fileLineEdit = new QLineEdit;
//fileLineEdit->setText();
mainLayout = new QGridLayout(this);//布局设计
mainLayout->addWidget(fileBtn,0,0);
mainLayout->addWidget(fileLineEdit,0,1);
connect(fileBtn,SIGNAL(clicked()),this,SLOT(showFile())); //事件关联
colorBtn = new QPushButton;
colorBtn->setText(tr("颜色标准对话框实例"));
colorFrame = new QFrame;
colorFrame->setFrameShape(QFrame::Box);
colorFrame->setAutoFillBackground(true);
mainLayout->addWidget(colorBtn,1,0);
mainLayout->addWidget(colorFrame,1,1);
connect(colorBtn,SIGNAL(clicked()),this,SLOT(showColor()));
fontBtn = new QPushButton;
fontBtn->setText(tr("字体标准对话框实例"));
fontLineEdit = new QLineEdit;
fontLineEdit->setText(tr("Hello World"));
mainLayout->addWidget(fontBtn,2,0);
mainLayout->addWidget(fontLineEdit,2,1);
connect(fontBtn,SIGNAL(clicked()),this,SLOT(showFont()));
inputBtn = new QPushButton;
inputBtn->setText(tr("标准输入对话框"));
mainLayout->addWidget(inputBtn,3,0);
connect(inputBtn,SIGNAL(clicked()),this,SLOT(showInputDlg()));
MsgBtn = new QPushButton;
MsgBtn->setText(tr("标准消息对话框实例"));
mainLayout->addWidget(MsgBtn,3,1);
connect(MsgBtn,SIGNAL(clicked()),this,SLOT(showMsgDlg()));
CustomBtn = new QPushButton;
CustomBtn->setText(tr("用户自定义消息对话框实例"));
label = new QLabel;
label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
mainLayout->addWidget(CustomBtn,4,0);
mainLayout->addWidget(label,4,1);
connect(CustomBtn,SIGNAL(clicked()),this,SLOT(showCustomDlg()));
}
Dialog::~Dialog()
{
}
void Dialog::showFile()
{
QString s=QFileDialog::getOpenFileName(this,"open file dialog","/","C++ files(*.cpp)::C files(*.c)::Head files(*.h)");
fileLineEdit->setText(s);
}
void Dialog::showColor()
{
QColor c = QColorDialog::getColor(Qt::blue);
if(c.isValid())
{
colorFrame->setPalette(QPalette(c));
}
}
void Dialog::showFont()
{
bool ok;
QFont f=QFontDialog::getFont(&ok);
if(ok)
{
fontLineEdit->setFont(f);
}
}
void Dialog::showInputDlg()
{
inputDlg = new InputDlg();
inputDlg->show();
}
void Dialog::showMsgDlg()
{
msgDlg = new MsgBoxDlg();
msgDlg->show();
}
void Dialog::showCustomDlg()
{
label->setText(tr("Custom massage box"));
QMessageBox customMsgBox;
customMsgBox.setWindowTitle(tr("用户自定义消息框"));
QPushButton *yesBtn = customMsgBox.addButton("YES",QMessageBox::ActionRole);
QPushButton *noBtn = customMsgBox.addButton("NO",QMessageBox::ActionRole);
QPushButton *cancelBtn = customMsgBox.addButton(QMessageBox::Cancel);
customMsgBox.setText(tr("这是一个自定义消息对话框"));
customMsgBox.setIconPixmap(QPixmap("Qt.png"));//设置标题图标
customMsgBox.exec();
if(customMsgBox.clickedButton()==yesBtn)
label->setText("Custom Message Box/Yes");
if(customMsgBox.clickedButton()==noBtn)
label->setText("Custom Message Box/no");
if(customMsgBox.clickedButton()==cancelBtn)
label->setText("Custom Message Box/no");
return ;
}
inputdlg.cpp
#include "inputdlg.h"
#include<QInputDialog>
InputDlg::InputDlg(QWidget *parent) : QDialog(parent)
{
setWindowTitle(tr("标准输入对话框实例"));
nameLabel1 = new QLabel;
nameLabel1->setText(tr("姓名:"));
nameLabel2 = new QLabel;
nameLabel2->setText(tr("张三"));
nameLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
nameBtn = new QPushButton;
nameBtn ->setText(tr("修改姓名"));
connect(nameBtn,SIGNAL(clicked()),this,SLOT(ChangeName()));
sexLabel1 = new QLabel;
sexLabel1->setText(tr("性别:"));
sexLabel2 = new QLabel;
sexLabel2->setText(tr("男"));
sexLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
sexBtn = new QPushButton;
sexBtn->setText(tr("修改性别"));
connect(sexBtn,SIGNAL(clicked()),this,SLOT(ChangeSex()));
ageLabel1 = new QLabel;
ageLabel1->setText(tr(" 年龄"));
ageLabel2 = new QLabel;
ageLabel2->setText(tr("22"));
ageLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
ageBtn = new QPushButton;
ageBtn->setText(tr("修改年龄"));
connect(ageBtn,SIGNAL(clicked()),this,SLOT(ChangeAge()));
scoreLabel1 = new QLabel;
scoreLabel1->setText(tr("成绩:"));
scoreLabel2 = new QLabel;
scoreLabel2->setText(tr("55"));
scoreLabel2->setFrameStyle(QFrame::Panel | QFrame::Sunken);
scoreBtn = new QPushButton;
scoreBtn->setText(tr("修改成绩"));
connect(scoreBtn,SIGNAL(clicked()),this,SLOT(ChangeScore()));
mainLayout = new QGridLayout(this);
mainLayout->addWidget(nameLabel1,0,0);
mainLayout->addWidget(nameLabel2,0,1);
mainLayout->addWidget(nameBtn,0,2);
mainLayout->addWidget(sexLabel1,1,0);
mainLayout->addWidget(sexLabel2,1,1);
mainLayout->addWidget(sexBtn,1,2);
mainLayout->addWidget(ageLabel1,2,0);
mainLayout->addWidget(ageLabel2,2,1);
mainLayout->addWidget(ageBtn,2,2);
mainLayout->addWidget(scoreLabel1,3,0);
mainLayout->addWidget(scoreLabel2,3,1);
mainLayout->addWidget(scoreBtn,3,2);
mainLayout->setMargin(15);//边框距离
mainLayout->setSpacing(10);//空间间隔
}
void InputDlg::ChangeName()
{
bool ok;
//修改
QString name =QInputDialog::getText(this,tr("标准字符串输入对话框"),tr("请输入姓名"),QLineEdit::Normal,nameLabel2->text());
if(ok && !name.isEmpty())
{
nameLabel2->setText(name);
}
}
void InputDlg::ChangeSex()
{
QStringList SexItems;
SexItems<<tr("男")<<tr("女");//只能输入男或女
bool ok;
//修改
QString sex =QInputDialog::getItem(this,tr("标准条目选择对话框"),tr("请输入性别"),SexItems,0,false,&ok);
if(ok && !sex.isEmpty())
{
sexLabel2->setText(sex);
}
}
void InputDlg::ChangeAge()
{
bool ok;
//修改
int age =QInputDialog::getInt(this,tr("标准Int输入对话框"),tr("请输入年龄"),ageLabel2->text().toInt(&ok),0,100,1,&ok);
if(ok)
{
ageLabel2->setText(QString(tr("%1")).arg(age));
}
}
void InputDlg::ChangeScore()
{
bool ok;
//修改
double score =QInputDialog::getDouble(this,tr("标准double类型输入对话框"),tr("请输入分数"),scoreLabel2->text().toDouble(&ok),0,100,1,&ok);;
if(ok )
{
scoreLabel2->setText(QString(tr("%1")).arg(score));
}
}
msgboxdlg.cpp
#include "msgboxdlg.h"
#include<QMessageBox>
MsgBoxDlg::MsgBoxDlg(QWidget *parent) : QDialog(parent)
{
setWindowTitle(tr("标准消息对话框的实例"));
label = new QLabel;
label->setText(tr("请选择一种消息对话框"));
questionBtn = new QPushButton;
questionBtn->setText(tr("QuestionMsg"));
connect(questionBtn,SIGNAL(clicked()),this,SLOT(showQuestionMsg()));
informationBtn = new QPushButton;
informationBtn->setText(tr("InformationMsg"));
connect(informationBtn,SIGNAL(clicked()),this,SLOT(showInformationMsg()));
warningBtn = new QPushButton;
warningBtn->setText(tr("WarningMsg"));
connect(warningBtn,SIGNAL(clicked()),this,SLOT(showWarningMsg()));
criticalBtn = new QPushButton;
criticalBtn->setText(tr("CriticalMsg"));
connect(criticalBtn,SIGNAL(clicked()),this,SLOT(showCriticalMsg()));
aboutBtn = new QPushButton;
aboutBtn->setText(tr("AboutMsg"));
connect(aboutBtn,SIGNAL(clicked()),this,SLOT(showAboutMsg()));
aboutQtBtn = new QPushButton;
aboutQtBtn->setText(tr("AboutQtMsg"));
connect(aboutQtBtn,SIGNAL(clicked()),this,SLOT(showAboutQtMsg()));
//布局
mainLayout = new QGridLayout(this);
mainLayout->addWidget(label,0,0,1,2);
mainLayout->addWidget(questionBtn,1,0);
mainLayout->addWidget(informationBtn,1,1);
mainLayout->addWidget(warningBtn,2,0);
mainLayout->addWidget(criticalBtn,2,1);
mainLayout->addWidget(aboutBtn,3,0);
mainLayout->addWidget(aboutQtBtn,3,1);
}
void MsgBoxDlg::showQuestionMsg()
{
label->setText(tr("Question Message Box"));
switch(QMessageBox::question(this,tr("Question消息框"),
tr("您现在已经修改完成,是否要结束程序?"),
QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok))
{
case QMessageBox::Ok:
label->setText("Question button/Ok");
break;
case QMessageBox::Cancel:
label->setText("Question button/Cancel");
break;
default:
break;
}
return;
}
void MsgBoxDlg::showInformationMsg()
{
label->setText(tr("Information Message Box"));
QMessageBox::information(this,tr("Information消息框"),
tr("这是Information消息框测试,欢迎您!"));
return;
}
void MsgBoxDlg::showWarningMsg()
{
label->setText(tr("Warning Message Box"));
switch(QMessageBox::warning(this,tr("Warning消息框"),
tr("您修改的内容还未保存,是否要保存对文档的修改?"),
QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,
QMessageBox::Save))
{
case QMessageBox::Save:
label->setText(tr("Warning button/Save"));
break;
case QMessageBox::Discard:
label->setText(tr("Warning button/Discard"));
break;
case QMessageBox::Cancel:
label->setText(tr("Warning button/Cancel"));
break;
default:
break;
}
return;
}
void MsgBoxDlg::showCriticalMsg()
{
label->setText(tr("Critical Message Box"));
QMessageBox::critical(this,tr("Critical消息框"),tr("这是一个Critical消息框测试!"));
return;
}
void MsgBoxDlg::showAboutMsg()
{
label->setText(tr("About Message Box"));
QMessageBox::about(this,tr("About消息框"),tr("这是一个About消息框测试!"));
return;
}
void MsgBoxDlg::showAboutQtMsg()
{
label->setText(tr("About Qt Message Box"));
QMessageBox::aboutQt(this,tr("About Qt消息框"));
return;
}
源码链接:
https://download.youkuaiyun.com/download/zq9955/15116561