自定义QMessageBox
参考Qt自带的 QMessageBox源码,通过继承QDialog纯代码实现。代码结构清晰,原理简单,不再进行太多理论讲解。
注意:代码中类名MessageBox 可能和 windows环境的定义重复,虽然直接复制可用,但最好自己修改其它类名,或者添加命名空间
效果图
直接上效果图:
代码实现
直接上代码
messagebox.h 头文件
#ifndef MESSAGEBOX_H
#define MESSAGEBOX_H
#include <QWidget>
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QDebug>
#include <QString>
enum MsgBoxType
{
MsgBoxType_Warn = 0,
MsgBoxType_Info = 1,
MsgBoxType_Error = 2
};
// 类名MessageBox 可能和 windows环境的定义重复 最好自己修改其它类名
class MessageBox : public QDialog
{
Q_OBJECT
public:
explicit MessageBox(QWidget *parent, MsgBoxType type, QString text);
void initState();
void initWarn(const QString &text);
void initError(const QString &text);
void initInfo(const QString &text);
signals:
public slots:
void dealbtnSureClicked();
void dealbtnCancelClicked();
private:
QLabel *labPic;
QLabel *labNote;
QPushButton *btnSure;
QPushButton *btnCancle;
};
#endif // MESSAGEBOX_H
messagebox.cpp 实现文件
#include "messagebox.h"
MessageBox::MessageBox(QWidget *parent, MsgBoxType type, QString text) : QDialog(parent)
{
initState();
if(type == MsgBoxType_Info)
{
initInfo(text);
}
else if(type == MsgBoxType_Warn)
{
initWarn(text);
}
else
{
initError(text);
}
}
void MessageBox::initState()
{
this->resize(240,160);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
this->setStyleSheet("background-color:rgb(46,47,48)");
labPic = new QLabel(this);
labNote = new QLabel(this);
btnSure = new QPushButton("确认",this);
btnCancle = new QPushButton("取消",this);
connect(btnSure,&QPushButton::clicked,this,&MessageBox::dealbtnSureClicked);
connect(btnCancle,&QPushButton::clicked,this,&MessageBox::dealbtnCancelClicked);
}
void MessageBox::initWarn(const QString &text)
{
int width = this->width();
labPic->setStyleSheet("image:url(:/image/msg_question.png)");
labPic->setGeometry(width*0.5-20,10,40,40);
labNote->setStyleSheet("color:white");
labNote->setAlignment(Qt::AlignCenter);
labNote->setGeometry(0,70,width,20);
labNote->setText(text);
btnSure->setGeometry(width*0.2-15,110,80,30);
btnSure->setStyleSheet("QPushButton{color:white; border-radius: 5px; background-color:rgb(43,34,45)}"
"QPushButton:hover{background-color:blue}"
"QPushButton:pressed{background-color:blue}");
btnCancle->setGeometry(width*0.6,110,80,30);
btnCancle->setStyleSheet("QPushButton{color:white; border-radius: 5px; background-color:rgb(43,34,45)}"
"QPushButton:hover{background-color:blue}"
"QPushButton:pressed{background-color:blue}");
}
void MessageBox::initError(const QString &text)
{
int width = this->width();
labPic->setStyleSheet("image:url(:/image/msg_error.png)");
labPic->setGeometry(width*0.5-20,10,40,40);
labNote->setStyleSheet("color:white");
labNote->setAlignment(Qt::AlignCenter);
labNote->setGeometry(0,70,width,20);
labNote->setText(text);
btnSure->setGeometry(width*0.5-40,110,80,30);
btnSure->setStyleSheet("QPushButton{color:white; border-radius: 5px; background-color:rgb(43,34,45)}"
"QPushButton:hover{background-color:blue}"
"QPushButton:pressed{background-color:blue}");
btnCancle->hide();
}
void MessageBox::initInfo(const QString &text)
{
int width = this->width();
labPic->setStyleSheet("image:url(:/image/msg_info.png)");
labPic->setGeometry(width*0.5-20,10,40,40);
labNote->setStyleSheet("color:white");
labNote->setAlignment(Qt::AlignCenter);
labNote->setGeometry(0,70,width,20);
labNote->setText(text);
btnSure->setGeometry(width*0.5-40,110,80,30);
btnSure->setStyleSheet("QPushButton{color:white; border-radius: 5px; background-color:rgb(43,34,45)}"
"QPushButton:hover{background-color:blue}"
"QPushButton:pressed{background-color:blue}");
btnCancle->hide();
}
void MessageBox::dealbtnSureClicked()
{
this->accept();
}
void MessageBox::dealbtnCancelClicked()
{
this->reject();
}
函数调用
MessageBox msgBox(this, MsgBoxType_Warn, "警告信息");
int res = msgBox.exec();
if(res == QDialog::Accepted)
{
qDebug() << "Accepted";
}
else if(res == QDialog::Rejected)
{
qDebug() << "Rejected";
}
else
{
qDebug() << "error";
}
总结
实现思路,其实就是创建一个继承QDialog的widget而已,QDialog使其有自带messageBox的属性,widget布局自己发挥。