基本信息
源码名称:自写QMessageBox
源码大小:4.03KB
文件格式:.cpp
开发语言:C/C++
更新时间:2021-04-07
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
自己写的QMessageBox,界面要比原装的QMessageBox要好看的多
自己写的QMessageBox,界面要比原装的QMessageBox要好看的多
#include "CCustomMessageBox.h" #include <QLabel> #include <QHBoxLayout> #include <QToolButton> #define LAYOUT_SPACING 20 #define DEFAULT_HEIGHT (100) #define DEFAULT_WIDTH (350) #define MIN_WEIGHT (100) #define MIN_WIDTH (150) #define FONT_SIZE (14) CCustomMessageBox::CCustomMessageBox(CUSTOM_MESSAGE_TYPE type, const QString &strTitle, const QString &strInfo, QWidget *parent, Qt::WindowFlags flags) :QDialog(parent, flags), m_eCustomType(type) { initialize(strInfo); alignment(); setWindowTitle(strTitle); resize(DEFAULT_WIDTH, DEFAULT_HEIGHT); setMinimumSize(MIN_WIDTH, MIN_WEIGHT); } CCustomMessageBox::~CCustomMessageBox() { } //设置标签的内容 void CCustomMessageBox::setTextInfo(const QString &strInfo) { if (!strInfo.isEmpty()) m_pLabelInfo->setText(strInfo); } void CCustomMessageBox::setTextInfo(const QString &strTitle, const QString &strInfo) { if (strTitle.isEmpty()) this->setWindowTitle(strTitle); if (!strInfo.isEmpty()) m_pLabelInfo->setText(strInfo); } void CCustomMessageBox::setTextInfo(CUSTOM_MESSAGE_TYPE type, const QString &strTitle, const QString &strInfo) { if (strTitle.isEmpty()) this->setWindowTitle(strTitle); if (!strInfo.isEmpty()) m_pLabelInfo->setText(strInfo); else return; m_eCustomType = type; QString fileName; switch (m_eCustomType) { case CUSTOM_MESSAGE_QUESTION: fileName = ":/question"; break; case CUSTOM_MESSAGE_INFORMATION: fileName = ":/information"; break; case CUSTOM_MESSAGE_WARNING: fileName = ":/warning"; break; case CUSTOM_MESSAGE_CRITICAL: fileName = ":/error"; break; default: break; } QPixmap iconPix(fileName); m_pLabelIcon->setPixmap(iconPix); } void CCustomMessageBox::initialize(const QString &strInfo) { m_pLabelIcon = new QLabel(this); QString fileName; switch (m_eCustomType) { case CUSTOM_MESSAGE_QUESTION: fileName = ":/question"; break; case CUSTOM_MESSAGE_INFORMATION: fileName = ":/information"; break; case CUSTOM_MESSAGE_WARNING: fileName = ":/warning"; break; case CUSTOM_MESSAGE_CRITICAL: fileName = ":/error"; break; default: break; } QPixmap iconPix(fileName); m_pLabelIcon->setPixmap(iconPix); m_pLabelIcon->setFixedSize(45, 45); m_pLabelIcon->setObjectName("msgBoxIconLabel"); QFont font; font.setBold(true); font.setFamily("Consolas"); font.setPixelSize(FONT_SIZE); m_pLabelInfo = new QLabel(strInfo, this); m_pLabelInfo->setWordWrap(true); m_pLabelInfo->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); m_pLabelInfo->setFont(font); m_pLabelInfo->setObjectName("msgBoxInfoLabel"); m_pBtnYes = new QToolButton(this); QPixmap yesPix(":/yes_Btn"); m_pBtnYes->setIcon(yesPix); m_pBtnYes->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); m_pBtnYes->setIconSize(QSize(30, 30)); m_pBtnYes->setFont(font); m_pBtnYes->setObjectName("msgBoxYesBtn"); m_pBtnYes->setFocusPolicy(Qt::NoFocus); if (m_eCustomType == CUSTOM_MESSAGE_QUESTION) m_pBtnYes->setText(tr("Yes")); else m_pBtnYes->setText(tr("Ok")); connect(m_pBtnYes, SIGNAL(released()), this, SLOT(accept())); if (m_eCustomType == CUSTOM_MESSAGE_QUESTION) { m_pBtnNo = new QToolButton(this); QPixmap noPix(":/no_Btn"); m_pBtnNo->setIcon(noPix); m_pBtnNo->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); m_pBtnNo->setIconSize(QSize(30, 30)); m_pBtnNo->setText(tr("No")); m_pBtnNo->setFont(font); m_pBtnNo->setObjectName("msgBoxNoBtn"); m_pBtnNo->setFocusPolicy(Qt::NoFocus); connect(m_pBtnNo, SIGNAL(released()), this, SLOT(reject())); } } //界面布局 void CCustomMessageBox::alignment() { QHBoxLayout *hbLabelLayout = new QHBoxLayout; hbLabelLayout->addWidget(m_pLabelIcon); hbLabelLayout->addWidget(m_pLabelInfo); QHBoxLayout *hbBtnLayout = new QHBoxLayout; hbBtnLayout->addStretch(); hbBtnLayout->addWidget(m_pBtnYes); if (m_eCustomType == CUSTOM_MESSAGE_QUESTION) { hbBtnLayout->addStretch(); hbBtnLayout->addWidget(m_pBtnNo); } hbBtnLayout->addStretch(); QVBoxLayout *vbLayout = new QVBoxLayout; vbLayout->addLayout(hbLabelLayout); vbLayout->addSpacing(20); vbLayout->addLayout(hbBtnLayout); this->setLayout(vbLayout); } /** * @brief 自定义MessageBox * @file custommessagebox.h * @author 奋斗Andy * @version 1.0(版本号) * @date 2016-08-03 */ #ifndef _CUSTOM_MESSAGEBOX_H_ #define _CUSTOM_MESSAGEBOX_H_ #include <QWidget> #include <QDialog> class QLabel; class QToolButton; class QToolButton; /** * @brief 自定义消息提示框类 */ class CCustomMessageBox : public QDialog { Q_OBJECT public: /** * @brief 自定义枚举类型 消息的类型 */ enum CUSTOM_MESSAGE_TYPE { CUSTOM_MESSAGE_NOICON = 0, /**< 无 */ CUSTOM_MESSAGE_QUESTION, /**< 询问 */ CUSTOM_MESSAGE_INFORMATION, /**< 信息 */ CUSTOM_MESSAGE_WARNING, /**< 警告 */ CUSTOM_MESSAGE_CRITICAL, /**< 错误 */ }; /** * @brief 构造函数 * @param type [in] 消息类型 * @param strTitle [in]标题 * @param strInfo [in] 消息内容 * @param parent [in] 父类窗口 * @param flags [in] 窗口标志 */ CCustomMessageBox(CUSTOM_MESSAGE_TYPE type, const QString &strTitle, const QString &strInfo, QWidget *parent = 0, Qt::WindowFlags flags = Qt::CustomizeWindowHint | Qt::WindowTitleHint); ~CCustomMessageBox(); /** * @brief 设置显示内容 * @param strInfo [in] 信息内容 */ void setTextInfo(const QString &strInfo); /** * @brief 这是一个重载函数 * @see CCustomMessageBox::setTextInfo * @param strTitle [in] 标题 * @param strInfo [in] 信息内容 */ void setTextInfo(const QString &strTitle, const QString &strInfo); /** * @brief 这是一个重载函数 * @see CCustomMessageBox::setTextInfo * @param type[in] 消息的类型 * @param strTitle [in] 标题 * @param strInfo [in] 信息内容 */ void setTextInfo(CUSTOM_MESSAGE_TYPE type, const QString &strTitle, const QString &strInfo); private: /** * @brief 初始化 * @param strInfo [in] 信息内容 */ void initialize(const QString &strInfo); /** * @brief 布局 */ void alignment(); private: QLabel *m_pLabelIcon; /**< 提示信息类型图标 */ QLabel *m_pLabelInfo; /**< 提示信息 */ QToolButton *m_pBtnYes; /**< 是(确定)按扭 */ QToolButton *m_pBtnNo; /**< 否(取消)按扭 */ CUSTOM_MESSAGE_TYPE m_eCustomType; /**< 自定义类型 */ }; #endif //_CUSTOM_MESSAGEBOX_H_