前言
原生的QT提示框总是让人觉得难以接受,因此特定重新封装一个类来替换原生的,使用过程跟原生的一致,使用比较方便。
一、演示效果
二、关键程序
#include "MessageBox.h"
#include "MessageBoxEx.h"
CMessageBox::CMessageBox(QWidget* parent)
{
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
setMessageBox(QMessageBox::NoIcon, "", "", QMessageBox::NoButton);
setShadow();
}
CMessageBox::CMessageBox(QMessageBox::Icon icon, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons, QWidget* parent)
: QDialog(parent)
{
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
setMessageBox(icon, title, text, buttons);
setShadow();
}
CMessageBox::~CMessageBox()
{
}
void CMessageBox::mousePressEvent(QMouseEvent* event)
{
mBeginPos = event->pos();
if (mBeginPos.y() < 62 && mBeginPos.y() >= 0)
mIsPress = true;
QWidget::mousePressEvent(event);
}
void CMessageBox::mouseMoveEvent(QMouseEvent* event)
{
if (mIsPress)
{
QPoint offset(QCursor::pos() - mBeginPos);
move(offset);
}
QWidget::mouseMoveEvent(event);
}
void CMessageBox::mouseReleaseEvent(QMouseEvent* event)
{
mIsPress = false;
QWidget::mouseReleaseEvent(event);
}
QMessageBox::StandardButton CMessageBox::information(QWidget* parent, const QString& title, const QString& text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
CMessageBoxEx widget(QMessageBox::Information, title, text, buttons, parent);
return QMessageBox::StandardButton(widget.exec());
}
QMessageBox::StandardButton CMessageBox::question(QWidget* parent, const QString& title, const QString& text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
CMessageBoxEx widget(QMessageBox::Question, title, text, buttons, parent);
return QMessageBox::StandardButton(widget.exec());
}
QMessageBox::StandardButton CMessageBox::warning(QWidget* parent, const QString& title, const QString& text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
{
CMessageBoxEx widget(QMessageBox::Warning, title, text, buttons, 0);
return QMessageBox::StandardButton(widget.exec());
}
QMessageBox::StandardButton CMessageBox::critical(QWidget* parent, const QString& title, const QString& text, QMessageBox::StandardButtons buttons, QMessageBox::QMessageBox::StandardButton defaultButton)
{
CMessageBoxEx widget(QMessageBox::Critical, title, text, buttons, parent);
return QMessageBox::StandardButton(widget.exec());
}
void CMessageBox::about(QWidget* parent, const QString& title, const QString& text)
{
CMessageBoxEx widget(QMessageBox::NoIcon, title, text, QMessageBox::Ok, parent);
widget.exec();
}
void CMessageBox::setIconPixmap(const QPixmap& pixmap)
{
mWidget->messageBox()->setIconPixmap(pixmap);
}
void CMessageBox::setTitle(const QString& title)
{
mWidget->messageBox()->setWindowTitle(title);
mWidget->update();
}
void CMessageBox::setText(const QString& text)
{
mWidget->messageBox()->setText(text);
}
void CMessageBox::setStandardButtons(QMessageBox::StandardButtons buttons)
{
mWidget->messageBox()->setStandardButtons(buttons);
}
void CMessageBox::setMessageBox(QMessageBox::Icon icon, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons)
{
QHBoxLayout* hMainLayout = new QHBoxLayout(this);
hMainLayout->setContentsMargins(MSGBOX_SHADOW_WEIGHT, MSGBOX_SHADOW_WEIGHT, MSGBOX_SHADOW_WEIGHT, MSGBOX_SHADOW_WEIGHT);
mWidget = new cMessageWidget(icon, title, text, buttons, this);
hMainLayout->addWidget(mWidget);
}
void CMessageBox::setShadow()
{
QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect(this);
shadow->setBlurRadius(MSGBOX_SHADOW_WEIGHT * 1.5);
shadow->setColor(MSGBOX_SHADOW_COLOR);
shadow->setOffset(0, 0);
mWidget->setGraphicsEffect(shadow);
}
cMessageWidget::cMessageWidget(QMessageBox::Icon icon, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons, QWidget* parent)
: QWidget(parent),
d_ptr(new sMessageWidgetData)
{
setMinimumSize(540, 300);
setIcon(icon);
setTitle(title);
setText(text);
setButtons(buttons);
initBox();
initLayout();
}
cMessageWidget::~cMessageWidget()
{
}
void cMessageWidget::initBox()
{
mMessageBox = new QMessageBox(d_ptr->icon, d_ptr->title, d_ptr->text, d_ptr->buttons, this);
mMessageBox->setStyleSheet("QDialog{background:transparent; }");
connect(mMessageBox, SIGNAL(accepted()), this->parent(), SLOT(accept()));
connect(mMessageBox, SIGNAL(rejected()), this->parent(), SLOT(reject()));
connect(mMessageBox, SIGNAL(finished(int)), this->parent(), SLOT(done(int)));
}
void cMessageWidget::initLayout()
{
QWidget* widget = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(2, 50, 2, 2);
layout->addWidget(widget);
QVBoxLayout* vMainLayout = new QVBoxLayout(widget);
widget->setAutoFillBackground(true);
QHBoxLayout* boxLayout = new QHBoxLayout();
QHBoxLayout* buttonLayout = new QHBoxLayout();
vMainLayout->addLayout(boxLayout);
vMainLayout->addLayout(buttonLayout);
boxLayout->addWidget(mMessageBox);
buttonLayout->setSpacing(30);
buttonLayout->insertItem(0, new QSpacerItem(20, 20, QSizePolicy::Expanding));
for (auto button : mMessageBox->buttons())
{
button->setMinimumSize(120, 50);
buttonLayout->addWidget(button);
}
buttonLayout->insertItem(mMessageBox->buttons().size() + 1, new QSpacerItem(20, 20, QSizePolicy::Expanding));
}
void cMessageWidget::setIcon(const QMessageBox::Icon& icon)
{
d_ptr->icon = icon;
switch (icon)
{
case QMessageBox::Icon::Information:
d_ptr->messageColor = BLUE;
break;
case QMessageBox::Icon::NoIcon:
d_ptr->messageColor = BLUE;
break;
case QMessageBox::Icon::Question:
d_ptr->messageColor = BLUE;
break;
case QMessageBox::Icon::Warning:
d_ptr->messageColor = YELLOW;
break;
case QMessageBox::Icon::Critical:
d_ptr->messageColor = RED;
break;
default:
break;
}
}
void cMessageWidget::setTitle(const QString& title)
{
d_ptr->title = title;
}
void cMessageWidget::setText(const QString& text)
{
d_ptr->text = text;
}
void cMessageWidget::setButtons(const QMessageBox::StandardButtons& buttons)
{
d_ptr->buttons = buttons;
}
void cMessageWidget::drawBorder(QPainter* painter)
{
if (d_ptr->borderWidth <= 0)
return;
painter->save();
QPen pen;
pen.setWidth(d_ptr->borderWidth);
pen.setColor(d_ptr->messageColor);
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
QRect rect(0, 0, width(), height());
painter->drawRect(rect);
painter->restore();
}
void cMessageWidget::drawTitleBar(QPainter* painter)
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(d_ptr->messageColor);
QRect rect(0, 0, width(), d_ptr->titleHeight);
painter->drawRect(rect);
painter->setFont(d_ptr->titleFont);
QRect textRect(0, 0, width(), d_ptr->titleHeight);
Qt::Alignment align;
align = d_ptr->titleAlignment;
painter->setPen(Qt::white);
painter->setFont(d_ptr->titleFont);
if (mMessageBox != nullptr && !mMessageBox->windowTitle().isEmpty())
d_ptr->title = mMessageBox->windowTitle();
painter->drawText(textRect, align, d_ptr->title);
painter->restore();
}
void cMessageWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
drawBorder(&painter);
drawTitleBar(&painter);
return QWidget::paintEvent(event);
}