参考: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自己有啥需求就用了,这个不贴了

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐