Qt中自定义MessageBox提示框

28 篇文章 2 订阅

概述:

在做项目时,我们经常会用到QMessageBox这个控件,但有时候Qt自己提供的不能

满足我们项目的需求,于是打算用自己定义的MessageBox。下面是自己定义的一个

消息提示框的控件类,有什么不对的地方,希望大家一起交流!

头文件:

/**
  *  @brief    自定义MessageBox
  *  @file     custommessagebox.h
  *  @author   奋斗Andy
  *  @version  1.0(版本号)
  *  @date     2016-08-03
  */
#ifndef _CUSTOM_MESSAGEBOX_H_
#define _CUSTOM_MESSAGEBOX_H_

#include <QtGui>
#include <QDialog>

/**
 * @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_

源文件:

#include "custommessagebox.h"

#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);
}



  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值