Qt中QMessageBox消息对话框的使用
加入头文件#include
函数名原型:static StandardButton QMessageBox::information ( QWidget * parent, const QString & title, constQString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton );
#有以下提示框类型:
Qmessagebox::question()
Qmessagebox::information()
Qmessagebox::warning()
#实例使用如下:
//获取返回的按钮内容
StandardButton::QMessageBox btn= QMessageBox::information(NULL, “Title”, “Content”, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(btn==QMQMessageBox::YES)
{
//执行代码内容
}
else
{
//执行代码内容
}
效果如下:
为什么需要设计自定义提示框呢?
一是:Qt自带的提示框样式单一;
二是:提示框的太小;
三是:跟项目的不搭配;
代码如下:
messageBox.h
class messageBox:public QDialog
{
Q_OBJECT
public:
messageBox(QString title = "提示",QString text = nullptr, int showMessageType = Warning, int showBtnType = ButtonYes, QDialog *parent = nullptr);//构造函数
~messageBox();
enum Icon {//图标
// keep this in sync with QMessageDialogOptions::Icon
Information = 1,
Warning = 2,
Critical = 3,
Question = 4
};
//按钮选择
enum Button{
ButtonYes = 0, //一个yes
ButtonNo, //一个no
ButtonYesAndNo, //有两个按钮,一个yes,一个no
ButtonNot, //没有按钮
};
private:
QString m_title; //标题栏
QString m_showText; //内容
int m_showMessageType; //显示类型
int m_showBtnType; //按钮显示类型
QLabel *titleLabel;
QLabel *showLabel;
QLabel *iconLabel;
QPushButton *okBtn;
QPushButton *quitBtn;
// QIcon *icon;
QHBoxLayout *titlelayout;
QHBoxLayout *layout;
QHBoxLayout *layout1;
QVBoxLayout *vLayout;
void btnStyle();
void messageStyle();
private slots:
void btnClick();
};
messageBox.cpp
messageBox::messageBox(QString title, QString text,int showMessageType,int showBtnType,QDialog *parent)
: QDialog(parent),
m_title(title),
m_showText(text),
m_showMessageType(showMessageType),
m_showBtnType(showBtnType)
{
btnStyle();
messageStyle();
vLayout = new QVBoxLayout;
vLayout->addLayout(titlelayout);
vLayout->addLayout(layout);
vLayout->addLayout(layout1);
vLayout->setSpacing(0);
vLayout->setMargin(0);
vLayout->setStretch(0,1);
vLayout->setStretch(1,2);
vLayout->setStretch(2,1);
this->resize(360,200);
this->setLayout(vLayout);
this->show();
connect(okBtn,SIGNAL(clicked()),this,SLOT(btnClick()));
connect(quitBtn,SIGNAL(clicked()),this,SLOT(btnClick()));
}
按钮触发槽函数
void messageBox::btnClick()
{
QPushButton * btn = (QPushButton*)sender();
if (btn == okBtn) {
this->accept(); //返回1
} else if (btn == quitBtn) {
this->reject(); //返回0
}
}
提示框的布局
void messageBox::btnStyle()
{
titlelayout = new QHBoxLayout;
titleLabel = new QLabel(m_title);
titleLabel->setAlignment(Qt::AlignCenter);
titleLabel->setStyleSheet("background: rgbda(85,174,255,100%);color:#ffffff;");
titlelayout->addWidget(titleLabel);
okBtn = new QPushButton;
quitBtn = new QPushButton;
okBtn->setText(tr("确定"));
okBtn->setFixedSize(120,50);
okBtn->setFocusPolicy(Qt::NoFocus);
okBtn->setStyleSheet("border-image:url(:/image/partdialog/未选中.png);color: #ffffff;");
quitBtn->setText(tr("取消"));
quitBtn->setFixedSize(120,50);
quitBtn->setFocusPolicy(Qt::NoFocus);
quitBtn->setStyleSheet("border-image:url(:/image/partdialog/未选中.png);color: #ffffff;");
layout1 = new QHBoxLayout;
if (m_showBtnType == ButtonYes) {
layout1->addWidget(okBtn);
layout1->setSpacing(30);
layout1->setMargin(15);
} else if (m_showBtnType == ButtonNo) {
layout1->addWidget(quitBtn);
layout1->setSpacing(30);
layout1->setMargin(15);
} else if (m_showBtnType == ButtonYesAndNo) {
layout1->addWidget(okBtn);
layout1->addWidget(quitBtn);
layout1->setSpacing(30);
layout1->setMargin(15);
} else {
}
}
结论:其实提示框的重点是套用Dialog,然后和按钮的触发机制,accept()和reject()的返回值,在主线程调用的时候if (msg.exec())判断
下一章节:Qt 制作圆弧型提示框QDialog