QT-自定义消息提示框,好看大气

QT-自定义消息提示框,好看大气

前言

原生的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);
}


三、下载链接

https://download.csdn.net/download/u013083044/87570697

### 回答1: Qt自定义消息提示框是指开发者可以根据自己的需求设计出符合自己应用程序风格的消息提示框,从而提高应用程序的用户体验。可以使用Qt的QWidget或QDialog等控件来实现消息的展示和弹出。一般而言,自定义消息提示框通常包括标题、消息内容和按钮,通过控制按钮的显示和响应来实现交互。同时,还需要考虑消息提示框的美观和易读性,比如可以选择一些颜色和图标来增强视觉效果,并且还需要支持多语言,方便不同国家和地区的用户使用。另外,为了方便使用,还可以将自定义消息提示框封装成一个函数或类,供应用程序中其他地方调用。总之,Qt自定义消息提示框的使用可以大大提升应用程序的用户体验,增强用户的满意度和忠诚度。 ### 回答2: QT框架提供了一个简便实用的方法来实现自定义消息提示框。首先,我们可以创建一个继承自QWidget类的消息提示框,这个类可以包含我们自定义消息显示区域、确定按钮和取消按钮等控件。接下来,我们可以通过使用QHBoxLayout或QVBoxLayout等布局管理器来设置这些控件的位置和大小,使得它们在提示框中排版合理。同时,在确定按钮和取消按钮的点击事件中,我们可以使用accept()和reject()函数来确认或取消消息提示框的显示。 值得注意的是,在消息提示框的显示过程中,我们可以使用QPropertyAnimation等动画效果来增加提示框显示时的平滑感。另外,为了使得消息提示框更加美观,我们也可以在其样式表中设置背景、边框等相关样式。最终,我们可以通过调用自定义消息提示框类的实例来显示我们自己设计的消息提示框,在用户点击确定或取消按钮后,根据其结果来进行相应的操作。 综上所述,通过继承QWidget类并使用布局管理器、动画效果和样式表等技巧,我们可以实现自定义消息提示框,并在QT应用程序中使用它来提供更好的用户体验。 ### 回答3: QT是一款非常强大的跨平台图形用户界面库,开发者可以使用QT框架创建丰富功能的图形化应用程序。其中,消息提示框是在应用程序中非常常用的一种控件,使用者可以在提示框中显示各种类型的消息,比如警告、错误和信息等等。本文将介绍如何使用QT自定义消息提示框。 首先,我们需要创建一个新窗口,用于显示自定义消息提示框。然后,我们可以在窗口中添加各种控件,比如QLabel、QPushButton和QMessageBox等控件。控件的颜色、大小和位置等属性可以按照自己的设计来设置。 接下来,我们需要添加一些功能,比如设置提示框的标题、消息内容和图标等。在处理这些功能时,我们可以使用QT提供的信号和槽机制。比如,在点击按钮时,我们可以发出一个信号,然后使用槽函数来处理信号并显示消息内容。 最后,我们需要将自定义消息提示框集成到我们的应用程序中。这可以通过调用消息框的show函数来实现。如果我们希望用户能够在不关闭提示框的情况下执行其他操作,我们可以调用消息框的exec函数。 总之,使用QT自定义消息提示框是非常简单的,只需要创建一个新窗口并添加所需的控件和功能即可。同时,QT也提供了丰富的文档和示例,可以帮助我们更好地理解和使用自定义消息提示框
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的大海贼

联系博主,为您提供有价值的资源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值