样例:设计的比较简单,图片资源都是在网上找的,就不上传了,可以自己优化完善
defMessageBox.h:
#ifndef DEFMESSAGEBOX_H
#define DEFMESSAGEBOX_H
#include
#include
#include
#include
namespace Ui {
class DefMessageBox;
}
enum BTNTYPE
{
OK = 0x1 << 2,//0x00000400
CANCEL = 0x1 << 3,//0x00002000
YES = 0x1 << 4,//0x00000800
NO = 0x1 << 5,//0x00400000
CLOSE = 0x1 << 6
};
class DefMessageBox : public QDialog
{
Q_OBJECT
public:
explicit DefMessageBox(QWidget *parent, const QString& title, const QString& text, int type = BTNTYPE::OK);
~DefMessageBox();
private:
Ui::DefMessageBox *ui;
//创建控件
void CreateControl(const QString& title, const QString& text, int type);
// 设置窗体图标
void setIcon(const QString &icon);
//按钮图片三种状态切图
void SetButtonStyle(QPushButton *button, QString imgsrc, int CutSec);
QPushButton *okBtn;
QPushButton *cancleBtn;
QPushButton *yesBtn;
QPushButton *noBtn;
QPushButton *closeBtn;
QLabel *titleLabel;
QLabel *textLabel;
QLabel *iconLabel;
BTNTYPE retType;//返回类型
protected:
QPoint move_point; //移动的距离
bool mouse_press; //鼠标按下
void mousePressEvent(QMouseEvent *qevent); //鼠标按下事件
void mouseReleaseEvent(QMouseEvent *qevent);//鼠标释放事件
void mouseMoveEvent(QMouseEvent *qevent); //鼠标移动事件
public:
inline BTNTYPE getRetValue() { return retType; }
private slots :
void btnOk();
void btnCancel();
void btnYes();
void btnNo();
void btnClose();
public:
//调用不同类型弹出框接口
static BTNTYPE Information(QWidget *parent, const QString& title, const QString& text, int type = BTNTYPE::OK);
static BTNTYPE Error(QWidget *parent, const QString& title, const QString& text, int type = BTNTYPE::OK);
static BTNTYPE Success(QWidget *parent, const QString& title, const QString& text, int type = BTNTYPE::OK);
static BTNTYPE Question(QWidget *parent, const QString& title, const QString& text, int type = BTNTYPE::OK);
static BTNTYPE Warning(QWidget *parent, const QString& title, const QString& text, int type = BTNTYPE::OK);
};
#endif // DEFMESSAGEBOX_H
defMessageBox.cpp:
#include "defmessagebox.h"
#include "ui_defmessagebox.h"
#include
QString g_BtnLeft = "QPushButton{"
"background-color: rgb(23,26,31);"
"border-style:outset;" //边框样式(inset/outset)
"border-width:1px;" //边框宽度像素
"border-radius:3px;" //边框圆角半径像素
"border-color:rgb(52,235,124);" //边框颜色
"font:14px;" //字体,字体大小
"color:rgb(0,255,127);" //字体颜色
"}"
"QPushButton:hover{border-color: rgb(56,255,136);}"
"QPushButton:pressed{border-color: rgb(52,235,124);}";
QString g_BtnRight = "QPushButton{"
"background-color: rgb(23,26,31);"
"border-style:outset;"
"border-width:1px;"
"border-radius:3px;"
"border-color:rgb(204,204,204);"
"font:14px;"
"color:rgb(255,255,255);"
"}"
"QPushButton:hover{border-color: rgb(229,229,229);}"
"QPushButton:pressed{border-color: rgb(204,204,204);}";
DefMessageBox::DefMessageBox(QWidget *parent, const QString& title, const QString& text, int type) :
QDialog(parent),
ui(new Ui::DefMessageBox)
{
ui->setupUi(this);
setFixedSize(392, 222);//固定窗口大小
setStyleSheet("#DefMsgBox {border:1px solid rgb(24,107,57);background-color: rgb(23,26,31) }");
setWindowFlags(Qt::FramelessWindowHint);//隐藏顶部栏
setWindowModality(Qt::ApplicationModal);
CreateControl(title, text, type);
}
DefMessageBox::~DefMessageBox()
{
delete ui;
}
void DefMessageBox::mousePressEvent(QMouseEvent *qevent)
{
if (qevent->button() == Qt::LeftButton)
{
mouse_press = true;
move_point = qevent->pos();
}
}
void DefMessageBox::mouseMoveEvent(QMouseEvent *qevent)
{
if (mouse_press)
{
QPoint move_pos = qevent->globalPos();
this->move(move_pos - move_point);
}
}
void DefMessageBox::mouseReleaseEvent(QMouseEvent *qevent)
{
mouse_press = false;
}
void DefMessageBox::SetButtonStyle(QPushButton *button, QString imgsrc, int CutSec)
{
int img_w = QPixmap(imgsrc).width();
int img_h = QPixmap(imgsrc).height();
int PicWidth = img_w / CutSec;
button->setFixedSize(PicWidth, img_h);
button->setStyleSheet(QString("QPushButton{color: rgb(255, 255, 255);border-width: %1px; border-image: url(%2) 0 0 0 %3 repeat repeat;border-width: 0px; border-radius: 0px;}")
.append("QPushButton::hover{border-image: url(%2) 0 0 0 %4 repeat repeat;}")
.append("QPushButton::checked{border-image: url(%2) 0 0 0 %4 repeat repeat;}")
.append("QPushButton::pressed{border-image: url(%2) 0 0 0 %5 repeat repeat;}")
.arg(img_w).arg(imgsrc).arg(0).arg(PicWidth * 1).arg(PicWidth * 2));
}
void DefMessageBox::CreateControl(const QString& title, const QString& text, int type)
{
int bottom = 10;//按钮距底部高度
int bw = 80;//按钮宽高
int bh = 25;
int w = this->width();
int h = this->height();
QHBoxLayout *layout = new QHBoxLayout;
if ((type & BTNTYPE::OK) == BTNTYPE::OK)
{
QPushButton *button = new QPushButton("ok");
button->setMinimumSize(QSize(60,25));
button->setStyleSheet(g_BtnLeft);
connect(button, &QPushButton::clicked, this, &DefMessageBox::btnOk);
layout->addWidget(button,0, Qt::AlignHCenter);
}
if ((type & BTNTYPE::CANCEL) == BTNTYPE::CANCEL)
{
QPushButton *button = new QPushButton("cancel");
button->setMinimumSize(QSize(60, 25));
button->setStyleSheet(g_BtnRight);
connect(button, &QPushButton::clicked, this, &DefMessageBox::btnCancel);
layout->addWidget(button, 0, Qt::AlignHCenter);
}
if ((type & BTNTYPE::YES) == BTNTYPE::YES)
{
QPushButton *button = new QPushButton("yes");
button->setMinimumSize(QSize(60, 25));
button->setStyleSheet(g_BtnLeft);
connect(button, &QPushButton::clicked, this, &DefMessageBox::btnYes);
layout->addWidget(button, 0, Qt::AlignHCenter);
}
if ((type & BTNTYPE::NO) == BTNTYPE::NO)
{
QPushButton *button = new QPushButton("no");
button->setMinimumSize(QSize(60, 25));
button->setStyleSheet(g_BtnRight);
connect(button, &QPushButton::clicked, this, &DefMessageBox::btnNo);
layout->addWidget(button, 0, Qt::AlignHCenter);
}
if ((type & BTNTYPE::CLOSE) == BTNTYPE::CLOSE)
{
QPushButton *button = new QPushButton("close");
button->setMinimumSize(QSize(60, 25));
button->setStyleSheet(g_BtnRight);
connect(button, &QPushButton::clicked, this, &DefMessageBox::btnClose);
layout->addWidget(button, 0, Qt::AlignHCenter);
}
//右上角close按钮
closeBtn = new QPushButton(this);
SetButtonStyle(closeBtn, ":/res/titlebar/sub.png", 3);
closeBtn->setGeometry(w - 22 - 1, 1, 22, 22);
connect(closeBtn, &QPushButton::clicked, this, &DefMessageBox::btnClose);
//title文字
titleLabel = new QLabel(this);
titleLabel->setText(title);
titleLabel->setStyleSheet("QLabel{font-family :微软雅黑;font-size: 14px;color: rgb(255,255,255);background-color: rgb(23,26,31);}");
titleLabel->move(5, 2);
//中间text
textLabel = new QLabel(this);
textLabel->setText(text);
textLabel->setStyleSheet("QLabel{font-family :微软雅黑;font-size: 14px;color: rgb(255,255,255);background-color: rgb(23,26,31);}");
textLabel->setGeometry(25, 25, w - 50, 27 * 4);
textLabel->setWordWrap(true);
textLabel->setAlignment(Qt::AlignCenter);
setLayout(layout);
}
void DefMessageBox::btnOk()
{
retType = BTNTYPE::OK;
close();
}
void DefMessageBox::btnCancel()
{
retType = BTNTYPE::CANCEL;
close();
}
void DefMessageBox::btnYes()
{
retType = BTNTYPE::YES;
close();
}
void DefMessageBox::btnNo()
{
retType = BTNTYPE::NO;
close();
}
void DefMessageBox::btnClose()
{
retType = BTNTYPE::CLOSE;
close();
}
void DefMessageBox::setIcon(const QString &icon)
{
// if(textLabel)
// {
// int w = this->width();
// textLabel->setGeometry(100,75,w-120,27*4);
// textLabel->setAlignment(Qt::AlignTop);
// }
// iconLabel = new QLabel(this);
// iconLabel->setPixmap(QPixmap(icon));
// iconLabel->move(30,70);
}
BTNTYPE DefMessageBox::Information(QWidget *parent, const QString& title, const QString& text, int type)
{
DefMessageBox box(parent, title, text, type);
box.exec();
return box.getRetValue();
}
BTNTYPE DefMessageBox::Error(QWidget *parent, const QString& title, const QString& text, int type)
{
DefMessageBox box(parent, title, text, type);
box.setIcon(":/error.png");
box.exec();
return box.getRetValue();
}
BTNTYPE DefMessageBox::Success(QWidget *parent, const QString& title, const QString& text, int type)
{
DefMessageBox box(parent, title, text, type);
box.setIcon(":/sucess.png");
box.exec();
return box.getRetValue();
}
BTNTYPE DefMessageBox::Question(QWidget *parent, const QString& title, const QString& text, int type)
{
DefMessageBox box(parent, title, text, type);
box.setIcon(":/qes.png");
box.exec();
return box.getRetValue();
}
BTNTYPE DefMessageBox::Warning(QWidget *parent, const QString& title, const QString& text, int type)
{
DefMessageBox box(parent, title, text, type);
box.setIcon(":/war.png");
box.exec();
return box.getRetValue();
}
调用方法:调用接口都封装成DefMessageBox类的静态成员函数了,示例:
int nRet = DefMessageBox::Warning(NULL, "提示", "Are you sure?", BTNTYPE::YES | BTNTYPE::No);
if (BTNTYPE::YES == nRet)
{
}