我来简单介绍一下如何编写一些简单的代码来展示Qt常见对话框,即颜色对话框,文件对话框,输入对话框,错误提示对话框,字体对话框,消息对话,进度条对话框。
1.新建Qt项目,项目名称为随意,这里我把类名取为Dialog,基类为QDialog。
2.调用颜色对话框:项目创建完后,首先在头文件dialog.h添加调用颜色对华框的槽函数,dialog.h代码如下:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public slots:
int ShowColorDlg();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
然后在dialog.cpp中添加颜色对话框按钮(记得添加头文件QPushButton
和QColorDialog)。完成后添加按钮的槽函数ShowColorDlg链接,dialog.cpp代码如下:
#include "dialog.h"
#include "ui_dialog.h"
#include "QPushButton" //1
#include "QColorDialog" //1
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//设置颜色对话框
QPushButton * colorbutton= new QPushButton("显示颜色对话框",this);
//设置光标移动到该按钮位置,并按回车键或空格时,触发点击事件
colorbutton->setDefault(true);
//设置按钮位置大小
colorbutton->setGeometry(12,0,150,56);
//将颜色按钮与槽函数ShowColorDlg()连接
connect(colorbutton,SIGNAL(clicked(bool)),this,SLOT(ShowColorDlg()));
}
Dialog::ShowColorDlg()
{
//颜色对话框
QColorDialog * colorDlg = new QColorDialog;
//colorDlg->setModal(true);
colorDlg->show();
return 1;
}
Dialog::~Dialog()
{
delete ui;
}
同时在main.cpp修改显示窗体的大小,代码如下:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
w.resize(1000,500);
return a.exec();
}
编译执行后,现象如下:
3.调用文件按对话框:仿照之前的调用,在dialog.h添加如下代码:
public slots:
int ShowFileDlg();
在dialog.cpp添加如下代码(记得添加头文件QFileDialog和QMessageBox):
//设置文件按对话框
QPushButton * filebutton= new QPushButton("显示文件对话框",this);
filebutton->setDefault(true);
filebutton->setGeometry(12,60,150,56);
connect(filebutton,SIGNAL(clicked(bool)),this,SLOT(ShowFileDlg()));
.......
//2
Dialog::ShowFileDlg()
{
QFileDialog * fileDlg = new QFileDialog(this,"filedlg");
if(fileDlg->exec()==QFileDialog::Accepted)
{
QStringList name = fileDlg->selectedFiles();
QMessageBox::information(this, tr("提示"), name[0]);
}
return 1;
}
实现效果(不要在意其他的按钮,这是我在又添加了几个对话框后测试的截图):
4.调用输入对话框,在dialog.h添加如下代码:
public slots:
int ShowInputDlg();
在dialog.cpp添加如下代码(记得添加头文件QInputDialog):
//设置输入对话框
QPushButton * inputbutton= new QPushButton("显示输入对话框",this);
inputbutton->setDefault(true);
inputbutton->setGeometry(12,120,150,56);
connect(inputbutton,SIGNAL(clicked(bool)),this,SLOT(ShowInputDlg()));
...............
//3
Dialog::ShowInputDlg()
{
bool isok;
//设置输入框标签和默认内容
QString name=QInputDialog::getText(this,tr("user name"),
tr("PLEASE input new name"),QLineEdit::Normal,tr("正文"),&isok);
if(isok&&!name.isEmpty())
{
QString str= "OK";
QMessageBox::information(this,tr("提示"),name);
}
return 1;
}
实现效果:
5.调用错误对话框,dialog.h添加的代码如下:
public slots:
int ShowErrorDlg();
在dialog.cpp添加如下代码(记得添加头文件QErrorMessage):
//设置错误对话框
QPushButton * errorbutton= new QPushButton("显示错误对话框",this);
errorbutton->setDefault(true);
errorbutton->setGeometry(12,180,150,56);
connect(errorbutton,SIGNAL(clicked(bool)),this,SLOT(ShowErrorDlg()));
........
//4
Dialog::ShowErrorDlg()
{
QErrorMessage * errorDlg=new QErrorMessage;
errorDlg->setWindowTitle(QString("错误提示"));
errorDlg->showMessage(QString("内存泄漏"));
errorDlg->show();
return 1;
}
实现效果:
6.调用字体对话框,dialog.h添加的代码如下:
public slots:
int ShowFontDlg();
在dialog.cpp添加如下代码(记得添加头文件QFontDialog):
//设置字体对话框
QPushButton * fontbutton= new QPushButton("显示字体对话框",this);
fontbutton->setDefault(true);
fontbutton->setGeometry(12,240,150,56);
connect(fontbutton,SIGNAL(clicked(bool)),this,SLOT(ShowFontDlg()));
.......
//5
Dialog::ShowFontDlg()
{
bool ok;
QFontDialog::getFont(&ok, QFont( "Helvetica [Cronyx]", 10 ), this );
return 1;
}
实现效果:
7.调用消息对话框dialog.h添加的代码如下:
public slots:
int ShowMsgDlg();
在dialog.cpp添加如下代码(这次不需要添加头文件):
//设置消息对话框
QPushButton * msgbutton= new QPushButton("显示消息对话框",this);
msgbutton->setDefault(true);
msgbutton->setGeometry(12,300,150,56);
connect(msgbutton,SIGNAL(clicked(bool)),this,SLOT(ShowMsgDlg()));
........
//6
Dialog::ShowMsgDlg()
{
int ret = QMessageBox::information(this,QString("问题对话框"),QString("这是Qt!"),QMessageBox::Ok);
if(ret == QMessageBox::Ok)
{
QString str = "OK";
QMessageBox::information(this, tr("提示"), str);
}
return 1;
}
实现效果如下:
8.调用进度条对话框,dialog.h添加的代码如下:
public slots:
int ShowProcessDlg();
在dialog.cpp添加如下代码(这次需要添加头文件QProgressDialog):
//设置进度条对话框
QPushButton * processbutton= new QPushButton("显示进度对话框",this);
processbutton->setDefault(true);
processbutton->setGeometry(12,360,150,56);
connect(processbutton,SIGNAL(clicked(bool)),this,SLOT(ShowProcessDlg()));
.....
//7
Dialog::ShowProcessDlg()
{
QProgressDialog proDlg(QString("文件复制进度"),QString("取消"),0,50000,this);
proDlg.setWindowTitle(QString("进度对话框"));
proDlg.setWindowModality(Qt::WindowModal);//将对话框设置为模态
proDlg.show();
for(int i=0; i<50000; i++)
{ //演示复制进度
proDlg.setValue(i); //进度条的值
QCoreApplication::processEvents(); //避免界面冻结
if (proDlg.wasCanceled()) break; //按下取消按钮就中断
}
proDlg.setValue(50000);
return 1;
}
效果如下:
最终各部分代码如下所示:
dialog.h代码:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public slots:
int ShowColorDlg();
public slots:
int ShowFileDlg();
public slots:
int ShowInputDlg();
public slots:
int ShowErrorDlg();
public slots:
int ShowFontDlg();
public slots:
int ShowMsgDlg();
public slots:
int ShowProcessDlg();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp代码:
#include "dialog.h"
#include "ui_dialog.h"
#include "QPushButton" //1
#include "QColorDialog" //1
#include "QFileDialog" //2
#include "QMessageBox" //2
#include "QInputDialog" //3
#include "QErrorMessage" //4
#include "QFontDialog" //5
#include "QProgressDialog" //7
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//设置颜色对话框
QPushButton * colorbutton= new QPushButton("显示颜色对话框",this);
//设置光标移动到该按钮位置,并按回车键或空格时,触发点击事件
colorbutton->setDefault(true);
//设置按钮位置大小
colorbutton->setGeometry(12,0,150,56);
//将颜色按钮与槽函数ShowColorDlg()连接
connect(colorbutton,SIGNAL(clicked(bool)),this,SLOT(ShowColorDlg()));
//设置文件按对话框
QPushButton * filebutton= new QPushButton("显示文件对话框",this);
filebutton->setDefault(true);
filebutton->setGeometry(12,60,150,56);
connect(filebutton,SIGNAL(clicked(bool)),this,SLOT(ShowFileDlg()));
//设置输入对话框
QPushButton * inputbutton= new QPushButton("显示输入对话框",this);
inputbutton->setDefault(true);
inputbutton->setGeometry(12,120,150,56);
connect(inputbutton,SIGNAL(clicked(bool)),this,SLOT(ShowInputDlg()));
//设置错误对话框
QPushButton * errorbutton= new QPushButton("显示错误对话框",this);
errorbutton->setDefault(true);
errorbutton->setGeometry(12,180,150,56);
connect(errorbutton,SIGNAL(clicked(bool)),this,SLOT(ShowErrorDlg()));
//设置字体对话框
QPushButton * fontbutton= new QPushButton("显示字体对话框",this);
fontbutton->setDefault(true);
fontbutton->setGeometry(12,240,150,56);
connect(fontbutton,SIGNAL(clicked(bool)),this,SLOT(ShowFontDlg()));
//设置消息对话框
QPushButton * msgbutton= new QPushButton("显示消息对话框",this);
msgbutton->setDefault(true);
msgbutton->setGeometry(12,300,150,56);
connect(msgbutton,SIGNAL(clicked(bool)),this,SLOT(ShowMsgDlg()));
//设置进度条对话框
QPushButton * processbutton= new QPushButton("显示进度对话框",this);
processbutton->setDefault(true);
processbutton->setGeometry(12,360,150,56);
connect(processbutton,SIGNAL(clicked(bool)),this,SLOT(ShowProcessDlg()));
}
Dialog::ShowColorDlg()
{
//颜色对话框
QColorDialog * colorDlg = new QColorDialog;
//colorDlg->setModal(true);
colorDlg->show();
return 1;
}
//2
Dialog::ShowFileDlg()
{
QFileDialog * fileDlg = new QFileDialog(this,"filedlg");
if(fileDlg->exec()==QFileDialog::Accepted)
{
QStringList name = fileDlg->selectedFiles();
QMessageBox::information(this, tr("提示"), name[0]);
}
return 1;
}
//3
Dialog::ShowInputDlg()
{
bool isok;
QString name=QInputDialog::getText(this,tr("user name"),
tr("PLEASE input new name"),QLineEdit::Normal,tr("正文"),&isok);
if(isok&&!name.isEmpty())
{
QString str= "OK";
QMessageBox::information(this,tr("提示"),name);
}
return 1;
}
//4
Dialog::ShowErrorDlg()
{
QErrorMessage * errorDlg=new QErrorMessage;
errorDlg->setWindowTitle(QString("错误提示"));
errorDlg->showMessage(QString("内存泄漏"));
errorDlg->show();
return 1;
}
//5
Dialog::ShowFontDlg()
{
bool ok;
QFontDialog::getFont(&ok, QFont( "Helvetica [Cronyx]", 10 ), this );
return 1;
}
//6
Dialog::ShowMsgDlg()
{
int ret = QMessageBox::information(this,QString("问题对话框"),QString("这是Qt!"),QMessageBox::Ok);
if(ret == QMessageBox::Ok)
{
QString str = "OK";
QMessageBox::information(this, tr("提示"), str);
}
return 1;
}
//7
Dialog::ShowProcessDlg()
{
QProgressDialog proDlg(QString("文件复制进度"),QString("取消"),0,50000,this);
proDlg.setWindowTitle(QString("进度对话框"));
proDlg.setWindowModality(Qt::WindowModal);//将对话框设置为模态
proDlg.show();
for(int i=0; i<50000; i++)
{ //演示复制进度
proDlg.setValue(i); //进度条的值
QCoreApplication::processEvents(); //避免界面冻结
if (proDlg.wasCanceled()) break; //按下取消按钮就中断
}
proDlg.setValue(50000);
return 1;
}
Dialog::~Dialog()
{
delete ui;
}
main.cpp代码:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
w.resize(1000,500);
return a.exec();
}