Qt自定义MessageBox窗口

Qt自定义MessageBox窗口

必要元素及其功能
  • 标题栏(表头,关闭按钮,自由拖动)
  • 内容(不同的图片,提示内容,取消确认按钮)
  • 状态阻塞式的模态窗口
#pragma once
#include <QDialog>
#include <QObject>
#include <QLabel>
#include <QPushButton>
#include <QToolButton>
#include <QMouseEvent>

class msg_box_t: public QDialog 
{
	Q_OBJECT
public:
	msg_box_t(QString);
	~msg_box_t();
protected:
	QPoint move_point;
	bool mouse_press;
	void mousePressEvent(QMouseEvent*);           
	void mouseReleaseEvent(QMouseEvent*);         
	void mouseMoveEvent(QMouseEvent*);   
public slots:
	void ok_btn_press();
	void cancle_btn_press();
	void close_btn_press();
};
#include "msg_box.hpp"

msg_box_t::msg_box_t(QString text) 
{
	this->resize(360,160);
	this->setObjectName("msg");
	int width = this->width();
	int height = this->height();
	this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
	auto title_lbl_ = new QLabel(this);
	title_lbl_->setObjectName("title_label_");
	title_lbl_->setGeometry(0, 0, width, 32);
	title_lbl_->setText(u8"提示");

	auto close_btn_ = new QPushButton(this);
	close_btn_->setObjectName("setting_button");
	close_btn_->setToolTip(u8"关闭");
	close_btn_->setGeometry(width - 33, 1, 32, 32);
	close_btn_->setIcon(QIcon(":/images/close.png"));

	auto info_lbl_ = new QLabel(this);
	info_lbl_->setObjectName("title_label_");
	info_lbl_->setText(text);
	info_lbl_->setGeometry(50, 30, width-100, 70);
	info_lbl_->setAlignment(Qt::AlignCenter);
	info_lbl_->setWordWrap(true);

	auto ok_btn_ = new QPushButton(this);
	ok_btn_->setText(u8"确定");
	ok_btn_->setGeometry(70, height - 20 -32, 100, 32);
	ok_btn_->setObjectName("msg_btn");
	auto cancle_btn_ = new QPushButton(this);
	cancle_btn_->setText(u8"取消");
	cancle_btn_->setGeometry(190, height - 20 - 32, 100, 32);
	cancle_btn_->setObjectName("msg_btn");

	connect(close_btn_, SIGNAL(clicked()), this, SLOT(close_btn_press()));
	connect(ok_btn_, SIGNAL(clicked()), this, SLOT(ok_btn_press()));
	connect(cancle_btn_, SIGNAL(clicked()), this, SLOT(cancle_btn_press()));
}

msg_box_t::~msg_box_t()
{
}

void msg_box_t::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		mouse_press = true;
		move_point = event->pos();
	}
}

void msg_box_t::mouseReleaseEvent(QMouseEvent*)
{
	mouse_press = false;
}

void msg_box_t::mouseMoveEvent(QMouseEvent* event)
{
	if (mouse_press)
	{
		QPoint move_pos = event->globalPos();
		this->move(move_pos - move_point);
	}
}

void msg_box_t::ok_btn_press()
{
	this->accept();
}

void msg_box_t::cancle_btn_press()
{
	this->reject();
}

void msg_box_t::close_btn_press()
{
	close();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值