1. 程式人生 > >Qt中自定義MessageBox提示框

Qt中自定義MessageBox提示框

概述:

在做專案時,我們經常會用到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);
}