Qt 自定义提示框

#pragma once

#include <QObject>
#include <QScopedPointer> 

class QLabel;
class QPushButton;
class QFrame;
class QDialog;

class TipMessage:public QObject
{
	Q_OBJECT

public:
	enum enMsgType
	{
		MT_SUCCESS = 0,
		MT_ERROR,
		MT_WARN,
		MT_INFO,
		MT_QUESTION,
		MT_CUSTOM,
	};

	enum enDialogCode
	{
		Fail = -1,
		Rejected = 0,
		Accepted,
	};
	static int showMessage(const QString& text, enMsgType msgType, bool autoClose = true ,int time = 1000);
	
private:
	static void initView();
	static bool m_bSet;
	static bool m_bInit;

	static QScopedPointer<QLabel> m_pPicLabel;
	static QScopedPointer<QLabel> m_pTextLabel;
	static QScopedPointer<QDialog> m_pTimeDlg;
	static QScopedPointer<QPushButton> m_pBtnClose;
	static QScopedPointer<QWidget> m_pContentWgt;
	static QScopedPointer<QFrame> m_spTextFrame;
	static QScopedPointer<QFrame> m_spButtonFrame;
	static QScopedPointer<QPushButton> m_spBtnOk;
	static QScopedPointer<QPushButton> m_spBtnCancel;
};

#include "TipMessage.h"
#include <QWidget>
#include <QLabel>
#include <QTimer>
#include <QSizePolicy>
#include <QPushButton>
#include <QDialog>
#include <QGridLayout>
#include <QHBoxLayout>

QScopedPointer<QDialog> TipMessage::m_pTimeDlg;
QScopedPointer<QLabel> TipMessage::m_pPicLabel;
QScopedPointer<QLabel> TipMessage::m_pTextLabel;
QScopedPointer<QPushButton> TipMessage::m_pBtnClose;
QScopedPointer<QWidget> TipMessage::m_pContentWgt;

QScopedPointer<QFrame> TipMessage::m_spTextFrame;
QScopedPointer<QFrame> TipMessage::m_spButtonFrame;
QScopedPointer<QPushButton> TipMessage::m_spBtnOk;
QScopedPointer<QPushButton> TipMessage::m_spBtnCancel;

bool TipMessage::m_bSet = false;
bool TipMessage::m_bInit = false;

void TipMessage::initView()
{
	if (m_pTimeDlg.isNull())
	{
		m_pTimeDlg.reset(new QDialog());
		m_pTimeDlg->setFixedSize(QSize(300,90));
		m_pTimeDlg->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
		m_pTimeDlg->setObjectName("TipDialog");
		m_pTimeDlg->setStyleSheet("#TipDialog{background-color: rgb(255, 255, 255);border:1px solid black;}");

	}
	if (m_pPicLabel.isNull())
	{
		m_pPicLabel.reset(new QLabel());
	}
	if (m_pTextLabel.isNull())
	{
		m_pTextLabel.reset(new QLabel());
		m_pTextLabel->setAlignment(Qt::AlignCenter);
	}

	if (m_pBtnClose.isNull())
	{
		m_pBtnClose.reset(new QPushButton());
		m_pBtnClose->setFixedSize(QSize(24, 24));
 		m_pBtnClose->setStyleSheet("border-image: url(:/images/title/close.png);");
 		connect(m_pBtnClose.data(), &QPushButton::clicked, m_pTimeDlg.data(), [=]() {
 			m_pTimeDlg->accept();
 		});
	}

	if (m_pContentWgt.isNull())
	{
		m_pContentWgt.reset(new QWidget());
	}

	if (m_spTextFrame.isNull())
	{
		m_spTextFrame.reset(new QFrame());
	}
	if (m_spButtonFrame.isNull())
	{
		m_spButtonFrame.reset(new QFrame());
	}

	if (m_spBtnOk.isNull())
	{
		m_spBtnOk.reset(new QPushButton());
		m_spBtnOk->setText(QString::fromLocal8Bit("是"));
		m_spBtnOk->setFixedSize(QSize(64, 25));
		m_spBtnOk->setStyleSheet("QPushButton{background-color:#ffffff;border:1px solid #D5DFE5;}");
		m_spBtnOk->setStyleSheet("QPushButton:hover{background-color:#f3f3f3;}");
		connect(m_spBtnOk.data(), &QPushButton::clicked, m_spBtnOk.data(), [=]() {
			m_pTimeDlg->accept();
		});
	}
	if (m_spBtnCancel.isNull())
	{
		m_spBtnCancel.reset(new QPushButton());
		m_spBtnCancel->setText(QString::fromLocal8Bit("否"));
		m_spBtnCancel->setFixedSize(QSize(64, 25));
		m_spBtnCancel->setStyleSheet("QPushButton{background-color:#ffffff;border:1px solid #D5DFE5;}");
		m_spBtnCancel->setStyleSheet("QPushButton:hover{background-color:#f3f3f3;}");
		connect(m_spBtnCancel.data(), &QPushButton::clicked, m_spBtnCancel.data(), [=]() {
			m_pTimeDlg->reject();
		});
	}

	QGridLayout* pGridLayout1 = new QGridLayout(m_pTimeDlg.data());
	pGridLayout1->addWidget(m_pContentWgt.data());
	pGridLayout1->setContentsMargins(0, 0, 0, 0);
	m_pTimeDlg->setLayout(pGridLayout1);

	QGridLayout* pGridLayout2 = new QGridLayout(m_pContentWgt.data());
	pGridLayout2->addWidget(m_spTextFrame.data());
	pGridLayout2->addWidget(m_spButtonFrame.data());
	pGridLayout2->setContentsMargins(0, 0, 0, 0);
	m_pContentWgt->setLayout(pGridLayout2);

	QHBoxLayout* pHLayout1 = new QHBoxLayout(m_spTextFrame.data());
	pHLayout1->addWidget(m_pPicLabel.data());
	pHLayout1->addWidget(m_pTextLabel.data());
	pHLayout1->addWidget(m_pBtnClose.data());
	pHLayout1->addStretch();
	m_spTextFrame->setLayout(pHLayout1);

	QHBoxLayout* pHLayout2 = new QHBoxLayout(m_spButtonFrame.data());
	pHLayout2->addStretch();
	pHLayout2->addWidget(m_spBtnOk.data());
	pHLayout2->addWidget(m_spBtnCancel.data());
	m_spButtonFrame->setLayout(pHLayout2);

}

int TipMessage::showMessage(const QString& text, enMsgType msgType, bool autoClose /*= true*/ ,int time /* = 1000 */)
{
	if (!m_bInit)
	{
		initView();
		m_bInit = true;
	}
	switch (msgType)
	{
	case enMsgType::MT_SUCCESS:
		m_pPicLabel->setPixmap(QPixmap(":/images/tip/success.png"));
		break;
	case enMsgType::MT_ERROR:
		m_pPicLabel->setPixmap(QPixmap(":/images/tip/error.png"));
		break;
	case enMsgType::MT_WARN:
		m_pPicLabel->setPixmap(QPixmap(":/images/tip/warn.png"));
		break;
	case enMsgType::MT_INFO:
		m_pPicLabel->setPixmap(QPixmap(":/images/tip/info.png"));
		break;
	case enMsgType::MT_QUESTION:
		m_pPicLabel->setPixmap(QPixmap(":/images/tip/question.png"));
		break;
	default:
		break;
	}
	m_pTextLabel->setText(text);
	QTimer timer;
	if (autoClose)
	{
		timer.setSingleShot(true);
		timer.setInterval(time);
		connect(&timer, &QTimer::timeout, m_pTimeDlg.data(), &QDialog::accept);
		timer.start();
	}
	m_spButtonFrame->setHidden(autoClose);
	return m_pTimeDlg->exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值