窗口基类(一)

在这里插入图片描述
在这里插入图片描述

窗口基类(一)


#pragma once

#include <QDialog>
#include"titlebar.h"

class BasicWindow : public QDialog
{
	Q_OBJECT

public:
	BasicWindow(QWidget *parent = nullptr);
	virtual ~BasicWindow();

public:
	//加载样式表
	void loadStyleSheet(const QString &sheetName);
	

	//获取圆头像
	QPixmap getRoundImage(const QPixmap &src,QPixmap &mask,QSize masksize = QSize(0,0));

private:
	void initBackGroundColor();//初始化背景

protected:
	void paintEvent(QPaintEvent*);		//绘制事件
	void mousePressEvent(QMouseEvent* event); //鼠标事件
	void mouseMoveEvent(QMouseEvent* event);	//鼠标移动事件
	void mouseReleasEvent(QMouseEvent*);	//鼠标松开事件

protected:
	void initTitleBar(ButtonType buttontype = MIN_BUTTON);
	void setTitleBarTitle(const QString& title,const QString& icon = "");

public slots:
	void onShowClose(bool);
	void onShowMin(bool);
	void onShowHide(bool);
	void onShowNormal(bool);
	void onShowQuit(bool);
	void onSignalSkinChanged(const QColor& color);

	void onButtonMinClicked();
	void onButtonRestoreClicked();
	void onButtonMaxClicked();
	void onButtonCloseClicked();

protected:
	QPoint m_mousePoint;	//鼠标位置
	bool m_mousePressed;	//鼠标是否按下
	QColor m_colorBackGround;	//背景色
	QString m_styleName;	//样式文件名称
	TitleBar* _titleBar;	//标题栏
};


#include "basicwindow.h"

#include<QFile>

BasicWindow::BasicWindow(QWidget *parent)
	: QDialog(parent)
{
	setWindowFlags(Qt::FramelessWindowHint);
	setAttribute(Qt::WA_TranslucentBackground, true);
}

BasicWindow::~BasicWindow()
{
}

void BasicWindow::loadStyleSheet(const QString& sheetName)
{
	m_styleName = sheetName;
	QFile file(":/Resouces/QSS/"+sheetName+".css");
	file.open(QFile::ReadOnly);

	if (file.isOpen())
	{
		setStyleSheet("");
		QString qsstyleSheet = QLatin1String(file.readAll());


		//获取用户当前的皮肤RGB值
		QString r = QString::number(m_colorBackGround.red());
		QString g = QString::number(m_colorBackGround.green());
		QString b = QString::number(m_colorBackGround.blue());

		qsstyleSheet += QString("QWidget[titleskin=true]\
					{background-color:rgb(%1,%2,%3);\
					 border-top-left-radius:4px;}\
					 QWidget[bottomskin=true]\
					 {border-top:1px solid rgba(%1,%2,%3,100);\
					 background-color:rgba(%1,%2,%3,50);\
				     border-bottom-left-radius:4px;\
					 border-bottom-right-radius:4px;}")
					 .arg(r).arg(g).arg(b);
		setStyleSheet(qsstyleSheet);
	}

	file.close();
}

void BasicWindow::onSignalSkinChanged(const QColor& color)
{
	m_colorBackGround = color;
	loadStyleSheet(m_styleName);
}

void BasicWindow::initTitleBar(ButtonType buttontype)
{
	_titleBar = new TitleBar(this);
	_titleBar->setButtonType(buttontype);
	_titleBar->move(0, 0);

	connect(_titleBar, SIGNAL(signalButtonMinClicked()), this, SLOT(onButtonMinClicked()));
	connect(_titleBar, SIGNAL(signalButtonRestoreClicked()), this, SLOT(onButtonRestoreClicked()));
	connect(_titleBar,SIGNAL(signalButtonMaxClicked()),this,SLOT(onButtonMaxClicked()));
	connect(_titleBar,SIGNAL(signalButtonCloseClicked()),this,SLOT(onButtonCloseClicked()));
}

void BasicWindow::setTitleBarTitle(const QString& title, const QString& icon)
{
	_titleBar->setTitleIcon(icon);
	_titleBar->setTitleContent(title);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值