窗口基类(一)
#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);
}