列举了文件对话框(QFileDialog),颜色对话框(QColorDialog),字体对话框(QFontDialog),输入对话框(QInputDialog),消息对话框(QMessageDialog)和自定义对话框的使用方法。
头文件:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QPushButton>
#include<QFileDialog>//文本对话框
#include<QLineEdit>
#include<QGridLayout>
#include<QColorDialog>//颜色对话框
#include<QFrame>
#include<QFontDialog>//字体对话框
#include<QInputDialog>//输入对话框
#include<QMessageBox>//消息对话框
#include<QLabel>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
private slots:
void fileshow();
void colorshow();
void fontshow();
void inputshow();
void customshow();
private:
QPushButton *filebutton;//文件按钮
QLineEdit *filepath;//文件路径
QFileDialog *fileopen;//文件对话框
QGridLayout *filelayout;//文件布局
QPushButton *colorbutton;//颜色按钮
QColorDialog *colordialog;//颜色对话框
QFrame *colorframe;//选择
QPushButton *fontbutton;//字体按钮
QLineEdit *fontLine;//字体显示
QFontDialog *font;//字体对话框
QPushButton *inputbutton;//输入对话框
QLineEdit *inputline;//显示
QInputDialog *input;
QPushButton *questionbutton;//问题按钮
QPushButton *informationbutton;//消息按钮
QPushButton *warningbutton;//警告按钮
QPushButton *criticalbutton;//评论按钮
QPushButton *about;//关于按钮
QPushButton *aboutqt;//关于qt按钮
QPushButton *custombutton;
QLabel *customlabel;
};
#endif // DIALOG_H
源文件:
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("各种标准对话框");
//文件对话框
filebutton=new QPushButton(this);
filebutton->setText("标准文件对话框");
filepath=new QLineEdit(this);
filelayout=new QGridLayout(this);
filelayout->addWidget(filebutton,0,0);
filelayout->addWidget(filepath,0,1);
connect(filebutton,&QPushButton::clicked,this,&Dialog::fileshow);//关联按钮和文本编辑区
//颜色对话框
colorbutton=new QPushButton("颜色对话框");
colorframe=new QFrame;
colorframe->setFrameShape(QFrame::Box);
colorframe->setAutoFillBackground(true);//自动设置背景
filelayout->addWidget(colorbutton,1,0);
filelayout->addWidget(colorframe,1,1);
connect(colorbutton,&QPushButton::clicked,this,&Dialog::colorshow);
//字体对话框
fontbutton=new QPushButton("字体对话框");
fontLine=new QLineEdit("字体显示");
filelayout->addWidget(fontbutton,2,0);
filelayout->addWidget(fontLine,2,1);
connect(fontbutton,&QPushButton::clicked,this,&Dialog::fontshow);
//输入对话框
inputbutton=new QPushButton("输入对话框");
inputline=new QLineEdit;
filelayout->addWidget(inputbutton,3,0);
filelayout->addWidget(inputline,3,1);
connect(inputbutton,&QPushButton::clicked,this,&Dialog::inputshow);
//messagebox对话框
questionbutton=new QPushButton("问题");
informationbutton=new QPushButton("消息");
warningbutton=new QPushButton("警告");
criticalbutton=new QPushButton("评论");
about=new QPushButton("关于");
aboutqt=new QPushButton("关于qt");
filelayout->addWidget(questionbutton,4,0);
filelayout->addWidget(informationbutton,4,1);
filelayout->addWidget(warningbutton,5,0);
filelayout->addWidget(criticalbutton,5,1);
filelayout->addWidget(about,6,0);
filelayout->addWidget(aboutqt,6,1);
connect(questionbutton,&QPushButton::clicked,
[=]()
{
int ret= QMessageBox::question(this,"问题对话框","are you ok?");//生成问题对话框并获取选择
if(ret)
{
}
});
connect(informationbutton,&QPushButton::clicked,
[=]()
{
QMessageBox::information(this,"消息对话框","are you sb?");
});
connect(warningbutton,&QPushButton::clicked,
[=]()
{
QMessageBox::warning(this,"警告对话框","sophisticated");
});
connect(criticalbutton,&QPushButton::clicked,
[=]()
{
QMessageBox::critical(this,"评论对话框","gagaga");
});
connect(about,&QPushButton::clicked,
[=]()
{
QMessageBox::about(this,"关于对话框","current");
});
connect(aboutqt,&QPushButton::clicked,
[=]()
{
QMessageBox::aboutQt(this,"关于qt对话框");
});
//自定义消息对话框
custombutton=new QPushButton("自定义对话框");
customlabel=new QLabel;
customlabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
connect(custombutton,&QPushButton::clicked,this,&Dialog::customshow);
filelayout->addWidget(custombutton,7,0);
filelayout->addWidget(customlabel,7,1);
}
//文本显示
void Dialog::fileshow()
{
fileopen=new QFileDialog(this);
QString path=fileopen->getOpenFileName(this,"open file dialog","/","C++ files(*.cpp)::C file(*.c)::Head file(*.h)");//设置打开类型并返回路径
filepath->setText(path);
}
//颜色显示
void Dialog::colorshow()
{
QColor color=colordialog->getColor(Qt::white);
if(color.isValid())
{
colorframe->setPalette(QPalette(color));
}
}
void Dialog::fontshow()
{
bool *select=new bool;
QFont f=font->getFont(select);
if(select)
{
fontLine->setFont(f);
}
}
//输入显示
void Dialog::inputshow()
{
bool ok;
QString str=input->getText(this,"字符串输入框","请输入姓名",QLineEdit::Normal,inputline->text(),&ok);
if(ok&&!str.isEmpty())
{
inputline->setText(str);
}
}
//自定义消息对话框
void Dialog::customshow()
{
customlabel->setText("自定义消息框");
QMessageBox Custombox;
Custombox.setWindowTitle("自定义消息框");
QPushButton *yes=Custombox.addButton("Yes",QMessageBox::ActionRole);
QPushButton *no=Custombox.addButton("No",QMessageBox::ActionRole);
QPushButton *cancle=Custombox.addButton("Cancle",QMessageBox::ActionRole);
Custombox.setText("自定义消息框");
Custombox.exec();//自定义框不关闭
if(Custombox.clickedButton()==yes)
{
}
if(Custombox.clickedButton()==no)
{
}
if(Custombox.clickedButton()==cancle)
{
}
return;
}
Dialog::~Dialog()
{
}