1、标准文件对话框
#include "dialog.h"
#include "ui_dialog.h"
#include <QPushButton>
#include <QFileDialog>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
FileButon =new QPushButton;
FileButon2 =new QPushButton;
FileText =new QLineEdit;
FileLayou =new QGridLayout(this);
FileButon->setText(QString::fromLocal8Bit("选择文件"));
FileLayou->addWidget(FileButon,0,0,0);
FileLayou->addWidget(FileText,0,1,0);
FileLayou->addWidget(FileButon2,0,2);
connect(FileButon,SIGNAL(clicked()),this,SLOT(ShoeFile()));
ui->setupUi(this); //按钮按键关联函数ShoeFile
}
void Dialog::ShoeFile(void) //显示选择文件的界面
{
QString s =QFileDialog::getOpenFileName(this,"open file dialog","/","C++ files(*.cpp);;C files(*.c);;Head files(*.h)");
FileText->setText(s);
}
Dialog::~Dialog()
{
delete ui;
}
1.1)标准文件对话框的类 QFileDialog
1.2)QFileDialog::getOpenFileName(QWidget *parent = Q_NULLPTR, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString *selectedFilter = Q_NULLPTR, Options options = Options())
作用:获取文件
参数
parent — 父指针
caption – 选择文件界面标题
dir — 查找的目录
filter – 选择显示什么格式的文件
selectedFilter —过滤器
Options 是对话框的一些参数设定,比如只显示文件夹等等,它的取值是enum QFileDialog::Option,每个选项可以使用 | 运算组合起来。
2、颜色标准对话框
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
colorBtn=new QPushButton; //创建各个控件的对象
colorBtn->setText( QString::fromLocal8Bit("颜色标准对话框实例"));
colorFrame=new QFrame;
colorFrame->setFrameShape(QFrame::Box);
colorFrame->setAutoFillBackground(true);
mainLayout =new QGridLayout(this);
mainLayout->addWidget(colorBtn,1,0); //布局设计
mainLayout->addWidget(colorFrame,1,1);
connect(colorBtn,SIGNAL(clicked()),this,SLOT(showColor())); //事件关联
ui->setupUi(this);
}
void Dialog::showColor()
{
QColor c = QColorDialog::getColor(Qt::blue);
if(c.isValid())
{
colorFrame->setPalette(QPalette(c));
}
}
2.1)QFrame控件是一个框架,继承自QWidget,是其他小控件的父类,如:Q3Frame, Q3ProgressBar, QAbstractScrollArea, QLabel, QLCDNumber, QSplitter, QStackedWidget, and QToolBox,因此通常当作框架进行子类化处理,以生成我们自己想要的控件类型。
2.2)QFrame::setFrameShape(Shape) 设置面板框的样式
设置框架阴影效果:setFrameShadow(),参数为:QFrame.Plain(边框平坦);QFrame.Raised(边框突起);QFrame.Sunken(边框凹陷)
设置框架边框宽度:setLineWidth(int)
设置框架外形效果:setFrameShape(),参数为:
2.3)QFrame::setAutoFillBackground(bool) 是否显示面板的颜色
1显示 0不显示
2.4)QColorDialog::getColor(const QColor &initial = Qt::white, QWidget *parent = Q_NULLPTR, const QString &title = QString(), ColorDialogOptions options = ColorDialogOptions())
initial —初始化颜色
parent — 窗口的父指针
title – 窗口的标题
options --定义的对话框
2.5)QFrame::setPalette(QPalette(QColor )) – 设置颜色
3、标准输入对话框
InputDlg::InputDlg(QWidget* parent):QDialog(parent)
{
setWindowTitle(QString::fromLocal8Bit("标准输入对话框实例"));
nameLabel1 =new QLabel;
nameLabel1->setText(QString::fromLocal8Bit("姓名:"));
nameLabel2 =new QLabel;
nameLabel2->setText(QString::fromLocal8Bit("周何骏")); //姓名的初始值
nameLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
nameBtn =new QPushButton;
nameBtn->setText( QString::fromLocal8Bit("修改姓名"));
sexLabel1 =new QLabel;
sexLabel1->setText( QString::fromLocal8Bit("性别:"));
sexLabel2 =new QLabel;
sexLabel2->setText( QString::fromLocal8Bit("男")); //性别的初始值
sexLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
sexBtn =new QPushButton;
sexBtn->setText( QString::fromLocal8Bit("修改性别"));
ageLabel1 =new QLabel;
ageLabel1->setText( QString::fromLocal8Bit("年龄:"));
ageLabel2 =new QLabel;
ageLabel2->setText( QString::fromLocal8Bit("21")); //年龄的初始值
ageLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
ageBtn =new QPushButton;
ageBtn->setText( QString::fromLocal8Bit("修改年龄"));
scoreLabel1 =new QLabel;
scoreLabel1->setText( QString::fromLocal8Bit("成绩:"));
scoreLabel2 =new QLabel;
scoreLabel2->setText( QString::fromLocal8Bit("80")); //成绩的初始值
scoreLabel2->setFrameStyle(QFrame::Panel|QFrame::Sunken);
scoreBtn =new QPushButton;
scoreBtn->setText( QString::fromLocal8Bit("修改成绩"));
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);
connect(nameBtn,SIGNAL(clicked()),this,SLOT(ChangeName()));
connect(sexBtn,SIGNAL(clicked()),this,SLOT(ChangeSex()));
connect(ageBtn,SIGNAL(clicked()),this,SLOT(ChangeAge()));
connect(scoreBtn,SIGNAL(clicked()),this,SLOT(ChangeScore()));
}
void InputDlg::ChangeName()
{
bool ok;
QString text=QInputDialog::getText(this, QString::fromLocal8Bit("标准字符串输入对话框"),
QString::fromLocal8Bit("请输入姓名:"), QLineEdit::Normal,nameLabel2->text(),&ok);
if (ok && !text.isEmpty())
nameLabel2->setText(text);
}
void InputDlg::ChangeSex()
{
QStringList SexItems;
SexItems << QString::fromLocal8Bit("男") << QString::fromLocal8Bit("女");
bool ok;
QString SexItem = QInputDialog::getItem(this, QString::fromLocal8Bit("标准条目选择对话框"),
QString::fromLocal8Bit("请选择性别:"), SexItems, 0, false, &ok);
if (ok && !SexItem.isEmpty())
sexLabel2->setText(SexItem);
}
void InputDlg::ChangeAge()
{
bool ok;
int age = QInputDialog::getInt(this, QString::fromLocal8Bit("标准int类型输入对话框"),
QString::fromLocal8Bit("请输入年龄:"), 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, QString::fromLocal8Bit("标准double类型输入对话框"),
QString::fromLocal8Bit("请输入成绩:"),scoreLabel2->text().toDouble(&ok), 0, 100, 1, &ok);
if (ok)
scoreLabel2->setText(QString(tr("%1")).arg(score));
}
3.1)QString QInputDialog::getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString &text = QString(), bool *ok = Q_NULLPTR, Qt::WindowFlags flags = Qt::WindowFlags(), Qt::InputMethodHints inputMethodHints = Qt::ImhNone)
创建一个可输入的消息框
参数
parent – 指定一个父指针
title – 创建窗口的标题栏
label – 显示提示信息
QLineEdit::Normal – 显示方式,Normal 也就是正常 显示,你也可以声明为password
const QString &text – 接收字符串的地址
ok – 返回值
Qt::WindowFlags flags
3.2、QString QInputDialog::getItem(QWidget *parent, const QString &title, const QString &label, const QStringList &items, int current = 0, bool editable = true, bool *ok = Q_NULLPTR, Qt::WindowFlags flags = Qt::WindowFlags(), Qt::InputMethodHints inputMethodHints = Qt::ImhNone)
parent – 指定一个父指针
title – 创建窗口的标题栏
label – 显示提示信息
items – 插入组合框的字符串列表
current — 初始化显示的字符串编号
ok – 返回值
Qt::WindowFlags flags – 输入法提示
3.3
int QInputDialog::getInt(QWidget *parent, const QString &title, const QString &label, int value = 0, int min = -2147483647, int max = 2147483647, int step = 1, bool *ok = Q_NULLPTR, Qt::WindowFlags flags = Qt::WindowFlags())
parent – 指定一个父指针
title – 创建窗口的标题栏
label – 显示提示信息
value – 0
min --最小值
max – 最大值
step – 步长值
ok – 返回值
获取浮点数的话那就是QInputDialog::getDouble原理跟上面是一样的。
4.1)信息对话框
Question消息框使用QMessageBox::question()函数完成,此函数形式如下:
StandardButton QMessageBox::question
(
QWidget* parent, //消息框的父窗口指针
const QString& title, //消息框的标题栏
const QString& text, //消息框的文字提示信息
StandardButtons buttons=Ok, //注(1)
StandardButton defaultButton=NoButton //注(2)
);
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;
}
4.2)Information消息框使用QMessageBox::information()函数完成,函数形式如下:
StandardButton QMessageBox::information
(
QWidget*parent, //消息框的父窗口指针
const QString& title, //消息框的标题栏
const QString& text, //消息框的文字提示信息
StandardButtons buttons=Ok, //同Question消息框的注释内容
StandardButton defaultButton=NoButton //同Question消息框的注释内容
);
void MsgBoxDlg::showInformationMsg()
{
label->setText(tr("Information Message Box"));
QMessageBox::information(this,tr("Information消息框"),
tr("这是Information消息框测试,欢迎您!"));
return;
}
4.3)Warning消息框使用QMessageBox::warning()函数完成,函数形式如下:
StandardButton QMessageBox::warning
(
QWidget* parent, //消息框的父窗口指针
const QString& title, //消息框的标题栏
const QString& text, //消息框的文字提示信息
StandardButtons buttons=Ok, //同Question消息框的注释内容
StandardButton defaultButton=NoButton //同Question消息框的注释内容
);
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( QString::fromLocal8Bit("Warning button/Save"));
break;
case QMessageBox::Discard:
label->setText( QString::fromLocal8Bit("Warning button/Discard"));
break;
case QMessageBox::Cancel:
label->setText( QString::fromLocal8Bit("Warning button/Cancel"));
break;
default:
break;
}
return;
}
4.4)Critical消息框使用QMessageBox::critical()函数完成,函数形式如下:
StandardButton QMessageBox::critical
(
QWidget* parent, //消息框的父窗口指针
const QString& title, //消息框的标题栏
const QString& text, //消息框的文字提示信息
StandardButtons buttons=Ok, //同Question消息框的注释内容
StandardButton defaultButton=NoButton //同Question消息框的注释内容
);
void MsgBoxDlg::showCriticalMsg()
{
label->setText(tr("Critical Message Box"));
QMessageBox::critical(this,tr("Critical消息框"),tr("这是一个Critical消息框测试!"));
return;
}
4.5)
About Qt消息框使用QMessageBox:: aboutQt()函数完成,函数形式如下:
void QMessageBox::aboutQt
(
QWidget* parent, //消息框的父窗口指针
const QString& title=QString() //消息框的标题栏
);
完成文件“msgboxdlg.cpp”中的槽函数showAboutQtMsg(),具体代码如下:
void MsgBoxDlg::showAboutQtMsg()
{
label->setText(tr("About Qt Message Box"));
QMessageBox::aboutQt(this,tr("About Qt消息框"));
return;
}
4.6)自定义信息框
void Dialog::showCustomDlg()
{
label->setText(tr("Custom Message Box"));
QMessageBox customMsgBox;
customMsgBox.setWindowTitle( QString::fromLocal8Bit("用户自定义消息框")); //设置消息框的标题
QPushButton *yesBtn=customMsgBox.addButton(tr("Yes"),QMessageBox:: ActionRole); //(a)
QPushButton *noBtn=customMsgBox.addButton(tr("No"),QMessageBox::ActionRole);
QPushButton *cancelBtn=customMsgBox.addButton(QMessageBox::Cancel);
//(b)
customMsgBox.setText( QString::fromLocal8Bit("这是一个用户自定义消息框!")); //(c)
customMsgBox.setIconPixmap(QPixmap("Qt.png")); //(d)
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/Cancel");
return;
}