天天看點

Qt之 自定義QMessageBox(提示框)

參考:https://blog.csdn.net/q5512049/article/details/48438155

在原部落格的基礎上,封裝了一下,然後修改了QSS樣式

messageBox.h

#pragma once

#include "qdialog.h"
#include <QPushButton>
#include <QLabel>
#include <QMouseEvent>
class MsgBox :
	public QDialog
{
	Q_OBJECT
public:
	MsgBox(int style, QString text);
	~MsgBox(void);
public:
	QPushButton *okBtn;
	QPushButton *cancleBtn;
	QPushButton *closeBtn;
	QPushButton * msgBtn;
	QLabel * titleLabel;
	QLabel *askLabel;
protected:
	QPoint move_point;				//移動的距離
	bool mouse_press;				//滑鼠按下
	void mousePressEvent(QMouseEvent *qevent);			//滑鼠按下事件
	void mouseReleaseEvent(QMouseEvent *qevent);			//滑鼠釋放事件
	void mouseMoveEvent(QMouseEvent *qevent);			//滑鼠移動事件
	public slots:
	void okBtn_press();
	void cancleBtn_press();
	void closeBtn_press();

public:
	static void showBox(int messageType,QString str);
};

           

messageBox.cpp

#include "messageBox.h"


MsgBox::MsgBox(int style, QString text)
{
	this->resize(332, 167);

	//擷取主界面的寬度
	int width = this->width();
	int height = this->height();
	this->setObjectName("MsgBox");

	//設定标題欄隐藏
	this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
	titleLabel = new QLabel(this);
	titleLabel->setGeometry(0, 0, width, 30);
	if (style == 1)
		titleLabel->setText(QStringLiteral(" 警告 "));
	else
		titleLabel->setText(QStringLiteral(" 提示 "));
	titleLabel->setObjectName("msgTitleLabel");

	closeBtn = new QPushButton(this);
	closeBtn->setGeometry(width - 20, 8, 15, 15);
	closeBtn->setObjectName("msgcloseBtn");
	closeBtn->setStyleSheet("QPushButton{image:url(:/images/closeBtn.png);background: transparent;border:none;}QPushButton:hover{background: transparent;border:none;background-color:rgb(112, 116, 124);}QPushButton:pressed{background: transparent;border:none;}");

	msgBtn = new QPushButton(this);
	msgBtn->setGeometry(20, 56, 44, 44);
	msgBtn->setObjectName("msgBtn");
	if (style == 1)
		msgBtn->setStyleSheet("QPushButton{image:url(:/images/warning1.png);background: transparent;border:none;}");
	else if (style == 2)
		msgBtn->setStyleSheet("QPushButton{image:url(:/images/error1.png);background: transparent;border:none;}");
	else if (style == 3)
		msgBtn->setStyleSheet("QPushButton{image:url(:/images/doubt1.png);background: transparent;border:none;}");

	askLabel = new QLabel(this);
	askLabel->setGeometry(90, 56, width * 0.6, 50);
	askLabel->setText(text);
	//自動換行
	askLabel->setWordWrap(true);

	okBtn = new QPushButton(this);
	okBtn->setGeometry(90, 119, 85, 30);
	okBtn->setText(QStringLiteral("确定"));
	okBtn->setObjectName("msgokBtn");


	cancleBtn = new QPushButton(this);
	cancleBtn->setGeometry(187, 119, 85, 30);
	cancleBtn->setText(QStringLiteral("取消"));
	cancleBtn->setObjectName("cancleBtn");

	connect(okBtn, SIGNAL(clicked()), this, SLOT(okBtn_press()));
	connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeBtn_press()));
	connect(cancleBtn, SIGNAL(clicked()), this, SLOT(cancleBtn_press()));
}

void MsgBox::mousePressEvent(QMouseEvent *qevent)
{
	if (qevent->button() == Qt::LeftButton)
	{
		mouse_press = true;
		//滑鼠相對于窗體的位置(或者使用event->globalPos() - this->pos())
		move_point = qevent->pos();;
	}
}
void MsgBox::mouseMoveEvent(QMouseEvent *qevent)
{
	//若滑鼠左鍵被按下
	if (mouse_press)
	{
		//滑鼠相對于螢幕的位置
		QPoint move_pos = qevent->globalPos();

		//移動主窗體位置
		this->move(move_pos - move_point);
	}
}
void MsgBox::mouseReleaseEvent(QMouseEvent *qevent)
{
	//設定滑鼠為未被按下
	mouse_press = false;
}

void MsgBox::okBtn_press()
{
	//qDebug()<<1;
	this->accept();
}
void MsgBox::cancleBtn_press()
{
	this->reject();
}
void MsgBox::closeBtn_press()
{
	close();
}

//調用提示框
void MsgBox::showBox(int messageType, QString str)
{
	//1為警告框 //2為提示框 //3為詢問框
	MsgBox *msgBox = new MsgBox(messageType, str);//1為警告框
	msgBox->exec();
}

MsgBox::~MsgBox(void)
{
}
           

qss自己有啥需求就用了,這個不貼了

繼續閱讀