在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例桌面应用界面/GUI → 自写QMessageBox

自写QMessageBox

桌面应用界面/GUI

下载此实例
  • 开发语言:C/C++
  • 实例大小:4.03KB
  • 下载次数:12
  • 浏览次数:167
  • 发布时间:2021-04-07
  • 实例类别:桌面应用界面/GUI
  • 发 布 人:Daniel12
  • 文件格式:.cpp
  • 所需积分:2
 相关标签: MessageBox MES ES ss sa

实例介绍

【实例简介】自己写的QMessageBox,界面要比原装的QMessageBox要好看的多

【实例截图】

from clipboard

【核心代码】

#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_




标签: MessageBox MES ES ss sa

实例下载地址

自写QMessageBox

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警