首先,这个文档是有配套视频说明的:
https://www.bilibili.com/video/BV1f5411p7Dr?share_source=copy_web
正文
效果图:

自定义标题栏(不出意外的话,代码直接复制粘贴就能用)
不清楚还有什么BUG,有的话自己改改吧:
//里面内置了部分功能函数,具体能不能用我倒是还没试过就是了...讲道理应该可以的。
//设置标题栏下内部窗口
SetChildWidget(QWidget* child)
//设置标题栏高度
SetTitleHeight(int newHeight)
//设置标题栏的字符
SetTitleText(QString newTitle)
//设置标题栏背景图标,第二个参数是状态,默认0就好
SetTitleBackgroundImage(QString newUrl=":/QtCustomTitleBar/Resource/TiaoWen.jpg",int bkStyle=0)
//设置标题栏三个缩放按钮的宽度
SetTitleBottomWidth(int newWidth)
//设置标题栏边框大小
SetWindowBorder(int newBorderSize)
补充:动态读取外部qss文件,以“ }; ”为结尾,需要注意的是,如果还没到一整段styleSheet结束的位置,请不要在花括号后打分号,不然会判断会为结束,后面的都会作废。函数第一个参数是QWidget的ObjectName,所以在使用这个之前得先设置ObjectName
QString GetWidgetStyleSheetFromFile(QString objectName,QString path="./Resource/Config/TitleStyleSheet.qss");
setObjectName("CustomTitleWindow");
SetStyleSheet(GetWidgetStyleSheetFromFile("CustomTitleWindow"));
UpdateWidgetStyleSheet();
QSS例如:
局部多样式:

当然也没必要和我这样一样做,直接使用QT自带的读取QSS功能一样可以,只是我没找到它如何实现动态读取的功能,最终生成出来的程序是没有QSS文件的,也就不能自由的修改QSS了,下面视频展示下动态修改主题样式的功能:
以下是代码部分:
头文件:
#pragma once
#include <qwidget.h>
#include <qevent.h>
#include <qtoolbutton.h>
#include <qlabel.h>
#include "qpixmap.h"
#include "qapplication.h"
#include "qdesktopwidget.h"
#include "qstyle.h"
#include "qpainter.h"
#include "qstyleoption.h"
#include <qtimer.h>
#include <qfile.h>
#include <QTextStream>
class CustomTitleBar :public QWidget
{
Q_OBJECT
public:
CustomTitleBar(QWidget* parent = Q_NULLPTR);
void SetChildWidget(QWidget* child);
inline QWidget* GetChildWidget() const{ return p_child; }
//Set Title Height
inline void SetTitleHeight(int newHeight)
{
titleHeight = newHeight;
UpdateTitle();
}
//Set Title String
inline void SetTitleText(QString newTitle)
{
if (p_labelTitle)
{
p_labelTitle->setText(newTitle);
UpdateTitle();
}
}
//Set Image
inline void SetTitleBackgroundImage(QString newUrl=":/QtCustomTitleBar/Resource/TiaoWen.jpg",int bkStyle=0)
{
bkImage = QPixmap(newUrl);
bkUrl = newUrl;
bkImageState = bkStyle;
UpdateTitle();
}
inline void SetTitleBottomWidth(int newWidth)
{
if (newWidth>0)
{
titleBottonWidth = newWidth;
UpdateTitle();
}
}
inline void SetWindowBorder(int newBorderSize)
{
if(newBorderSize>0)
{
borderSize = newBorderSize;
UpdateTitle();
}
}
void UpdateTitle();
//inline void SetMinimumSize(QSize newSize) { setMinimumSize(newSize); }
//inline void SetMinimumSize(int width,int height) { setMinimumSize(QSize(width,height)); }
QToolButton* GetCloseButton()const{return p_closeButton;}
QToolButton* GetMaxButton()const { return p_maxButton; }
QToolButton* GetMinButton()const { return p_minButton; }
QLabel* GetTitleLabel()const {return p_labelTitle;}
QString GetWidgetStyleSheetFromFile(QString objectName,QString path="./Resource/Config/TitleStyleSheet.qss");
void UpdateWidgetStyleSheet();
private:
int titleHeight = 25;
int titleBottonWidth = 30;
QWidget* p_child = NULL; //嵌套的子窗口
QPoint mousePosition; //局部鼠标位置
QPoint lastMouseGlobalPosition;//上一帧的全屏鼠标位置
bool bIsMousePressed;
QToolButton* p_minButton; //最小化按钮
QToolButton* p_maxButton; //最大化按钮
QToolButton* p_closeButton; //关闭按钮
QLabel* p_labelTitle; //标题栏名称
bool bIsMax = false;
QPixmap normalPix; //缩小图标
QPixmap maxPix; //最大化图标
QLabel* p_backgroundImage; //标题栏背景图片组件
QString bkUrl; //标题栏背景图片路径
QPixmap bkImage; //标题栏背景图片
int bkImageState = 0; //状态,0为中心平铺,1为左上角平铺
/*---------窗口边框----*/
int borderSize = 5;
/*---------窗口缩放----*/
//上
QRect topHit;
bool bResizeByTopHit = false;
//下
QRect bottomHit;
bool bResizeByBottomHit = false;
//左
QRect leftHit;
bool bResizeByLeftHit = false;
//右
QRect rightHit;
bool bResizeByRightHit = false;
//右下
QRect rightBottomHit;
bool bResizeByRbHit = false;
//右上
QRect rightTopHit;
bool bResizeByRtHit = false;
//左下
QRect leftBottomHit;
bool bResizeByLbHit = false;
//左上
QRect leftTopHit;
bool bResizeByLtHit = false;
//计时器
QTimer* timer;
protected:
void ResetMouseBehavior();
virtual void mousePressEvent(QMouseEvent* event);
virtual void mouseReleaseEvent(QMouseEvent* event);
virtual void mouseDoubleClickEvent(QMouseEvent* event);
virtual void mouseMoveEvent(QMouseEvent* event);
virtual void resizeEvent(QResizeEvent* event);
virtual void showEvent(QShowEvent* event);
virtual void paintEvent(QPaintEvent* event);
virtual void focusOutEvent(QFocusEvent* event);
virtual void closeEvent(QCloseEvent* event);
//功能函数
static bool IsInside(QRect r, QPoint p)
{
if (p.x() >= r.left() && p.x() <= r.right() &&
p.y() >= r.top() && p.y() <= r.bottom())
return true;
else
return false;
}
private slots:
void actionMin(); //最小化窗口
void actionMax(); //最大化窗口
void actionClose(); //关闭窗口
void customUpdate();
};
CPP源文件:
#include "CustomTitleBar.h"
#include "qmessagebox.h"
CustomTitleBar::CustomTitleBar(QWidget* parent) :QWidget(parent)
{
p_backgroundImage = new QLabel(this);//背景图片
p_labelTitle = new QLabel(this);//标题
p_minButton = new QToolButton(this);//最小化按钮
p_maxButton = new QToolButton(this);//最大化按钮
p_closeButton = new QToolButton(this); //关闭按钮
SetTitleBackgroundImage();
this->resize(QSize(900, 600));//初始化窗口显示大小
QDesktopWidget* desktop = QApplication::desktop();
this->move((desktop->screen()->width() - this->width()) / 2,
(desktop->screen()->height() - this->height()) / 2);//程序显示在屏幕中间
QPixmap closePix= style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
p_closeButton->setIcon(closePix);
maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);
p_maxButton->setIcon(maxPix);
QPixmap minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
p_minButton->setIcon(minPix);
normalPix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);
p_minButton->setObjectName("TitleMinButton");
p_maxButton->setObjectName("TitleMaxButton");
p_closeButton->setObjectName("TitleCloseButton");
/******************信号与槽连接**************/
//最小化按钮
connect(p_minButton, SIGNAL(clicked(bool)), SLOT(actionMin()));
//最大化按钮
connect(p_maxButton, SIGNAL(clicked(bool)), SLOT(actionMax()));
//关闭按钮
connect(p_closeButton,SIGNAL(clicked(bool)),SLOT(actionClose()));
p_labelTitle->setText("MainWindow");
//setAutoFillBackground(true);
p_labelTitle->setObjectName("WindowTitleString");
//this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明,如果是要做渲染器的载体,就千万别开这个,会导致画面无法渲染出来!
this->setWindowFlags(Qt::FramelessWindowHint); //去掉标题栏
setObjectName("CustomTitleWindow");
setMinimumSize(110,50);
//创建定时器用于每帧更新
timer = new QTimer(this);
timer->setInterval(200);
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(customUpdate()));
timer->start();
//使用Palette
//setAutoFillBackground(true);
//p_minButton->setAutoFillBackground(true);
//p_maxButton->setAutoFillBackground(true);
//p_closeButton->setAutoFillBackground(true);
//p_labelTitle->setAutoFillBackground(true);
//p_backgroundImage->setAutoFillBackground(true);
//透明标题
//QPalette pal = palette();
//pal.setColor(QPalette::Background, QColor(255, 255, 255, 0));
//p_labelTitle->setPalette(pal);
UpdateWidgetStyleSheet();
UpdateTitle();
//重置一下鼠标行为
ResetMouseBehavior();
}
void CustomTitleBar::SetChildWidget(QWidget* child)
{
if (child&& p_child==NULL)
{
p_child = child;
p_child->setParent(this);
p_child->show();
p_child->setGeometry(borderSize, titleHeight + borderSize, this->width() - borderSize * 2, this->height() - titleHeight - borderSize * 2);
}
}
void CustomTitleBar::UpdateTitle()
{
//更新位置和大小
p_backgroundImage->setGeometry(borderSize, borderSize, this->width()- borderSize*2, titleHeight);
p_closeButton->setGeometry( this->width() - (titleBottonWidth) - borderSize, 2+ borderSize , titleBottonWidth -4, titleHeight-4 );
p_maxButton->setGeometry( this->width() - (titleBottonWidth) * 2- borderSize, 2+ borderSize , titleBottonWidth -4, titleHeight-4 );
p_minButton->setGeometry( this->width() - (titleBottonWidth) * 3- borderSize, 2+ borderSize , titleBottonWidth -4, titleHeight-4 );
p_labelTitle->move(10+ borderSize, borderSize);
if (p_child != NULL)
{
p_child->setGeometry(borderSize, titleHeight+ borderSize, this->width()- borderSize*2, this->height()- titleHeight - borderSize * 2);
}
//标题栏的图片样式
switch (bkImageState)
{
case 0:
break;
case 1:
p_backgroundImage->setPixmap(bkImage.scaled(this->width(), titleHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
break;
case 2:
break;
}
//其他的一些设置
QFont font("Microsoft YaHei", titleHeight / 2);
p_labelTitle->setFont(font);
//更新缩放点位置
topHit = QRect(borderSize, 0, this->width() - borderSize * 2, borderSize);
bottomHit = QRect(borderSize, this->height() - borderSize, this->width() - borderSize * 2, borderSize);
leftHit = QRect(0, borderSize, borderSize, this->height() - borderSize * 2);
rightHit = QRect(this->width() - borderSize, borderSize, borderSize, this->height() - borderSize * 2);
rightBottomHit = QRect(this->width() - borderSize, this->height() - borderSize, borderSize, borderSize);
rightTopHit = QRect(this->width() - borderSize, 0, borderSize, borderSize);
leftBottomHit = QRect(0, this->height() - borderSize, borderSize, borderSize);
leftTopHit = QRect(0, 0, borderSize, borderSize);
}
QString CustomTitleBar::GetWidgetStyleSheetFromFile(QString objectName, QString path)
{
QString result;
bool bFound=false;
if(!path.isEmpty())
{
QFile styleFile(path);
if (styleFile.open(QFile::ReadOnly | QIODevice::Text))
{
QTextStream in(&styleFile);
QString strLine;
while (!in.atEnd())
{
strLine = in.readLine();//逐行读取
int length = objectName.length();
QString objN = "#"+ objectName;
if (bFound ==true&&!result.isEmpty()&& strLine.contains("};", Qt::CaseSensitive))//是否到底结束位置
{
result += strLine.left(strLine.length()-1);
break;
}
if (bFound==false&&strcmp(strLine.left(length+1).toStdString().c_str(), objN.toStdString().c_str())==0)
{
bFound = true;
}
if (bFound)
{
result += strLine;
}
}
}
else
{
QMessageBox::information(this,"Error","Load ["+ objectName+"] style sheet file failed");
}
styleFile.close();
}
//QMessageBox::information(this, "Check", result);
return result;
}
void CustomTitleBar::mousePressEvent(QMouseEvent* event)
{
mousePosition = event->pos(); //当鼠标单击窗体准备拖动时,初始化鼠标在窗体中的相对位置
if (mousePosition.y() < 0)
return;
if (IsInside(rightBottomHit, mousePosition))
{
bResizeByRbHit = true;
this->setCursor(Qt::SizeFDiagCursor);
QCursor::setPos(this->geometry().bottomRight());
lastMouseGlobalPosition = QCursor::pos();
return;
}
else if (IsInside(leftBottomHit, mousePosition))
{
bResizeByLbHit = true;
this->setCursor(Qt::SizeBDiagCursor);
QCursor::setPos(this->geometry().bottomLeft());
lastMouseGlobalPosition = QCursor::pos();
return;
}
else if (IsInside(rightTopHit, mousePosition))
{
bResizeByRtHit = true;
this->setCursor(Qt::SizeBDiagCursor);
QCursor::setPos(this->geometry().topRight());
lastMouseGlobalPosition = QCursor::pos();
return;
}
else if (IsInside(leftTopHit, mousePosition))
{
bResizeByLtHit = true;
this->setCursor(Qt::SizeFDiagCursor);
QCursor::setPos(this->geometry().topLeft());
lastMouseGlobalPosition = QCursor::pos();
return;
}
else if (IsInside(topHit, mousePosition))
{
bResizeByTopHit = true;
this->setCursor(Qt::SizeVerCursor);
QCursor::setPos(QCursor::pos().x(), this->geometry().top());
lastMouseGlobalPosition = QCursor::pos();
return;
}
else if (IsInside(bottomHit, mousePosition))
{
bResizeByBottomHit = true;
this->setCursor(Qt::SizeVerCursor);
QCursor::setPos(QCursor::pos().x(), this->geometry().bottom());
lastMouseGlobalPosition = QCursor::pos();
return;
}
else if (IsInside(leftHit, mousePosition))
{
bResizeByLeftHit = true;
this->setCursor(Qt::SizeHorCursor);
QCursor::setPos(this->geometry().left(), QCursor::pos().y());
lastMouseGlobalPosition = QCursor::pos();
return;
}
else if (IsInside(rightHit, mousePosition))
{
bResizeByRightHit = true;
this->setCursor(Qt::SizeHorCursor);
QCursor::setPos(this->geometry().right(), QCursor::pos().y());
lastMouseGlobalPosition = QCursor::pos();
return;
}
else if (mousePosition.y() <= titleHeight)
{
bIsMousePressed = true;
return;
}
}
void CustomTitleBar::mouseReleaseEvent(QMouseEvent* event)
{
ResetMouseBehavior();
}
void CustomTitleBar::ResetMouseBehavior()
{
bIsMousePressed = false;
bResizeByRbHit = false;
bResizeByRtHit = false;
bResizeByLbHit = false;
bResizeByLtHit = false;
bResizeByTopHit = false;
bResizeByBottomHit = false;
bResizeByLeftHit = false;
bResizeByRightHit = false;
this->setCursor(Qt::ArrowCursor);
}
void CustomTitleBar::mouseDoubleClickEvent(QMouseEvent* event)
{
if (mousePosition.y() < 0)
return;
if (mousePosition.y() > titleHeight)
return;
if (bIsMax)
{
this->showNormal();
bIsMax = false;
p_maxButton->setIcon(maxPix);
}
else
{
this->showMaximized();
bIsMax = true;
p_maxButton->setIcon(normalPix);
}
}
void CustomTitleBar::mouseMoveEvent(QMouseEvent* event)
{
if (bIsMousePressed)
{
bResizeByRbHit = false;
bResizeByRtHit = false;
bResizeByLbHit = false;
bResizeByLtHit = false;
bResizeByTopHit = false;
bResizeByBottomHit = false;
bResizeByLeftHit = false;
bResizeByRightHit = false;
QPoint movePot = event->globalPos() - mousePosition;
move(movePot);
if (bIsMax)
{
this->showMaximized();
bIsMax = true;
p_maxButton->setIcon(normalPix);
}
}
if (bResizeByRbHit)
{
if (width() <= minimumSize().width() || height() <= minimumSize().height())
lastMouseGlobalPosition = QPoint(this->geometry().x() + width(), this->geometry().y() + height());//保持定点缩放
QPoint newPoint = event->globalPos()- lastMouseGlobalPosition;
this->setGeometry(this->geometry().left(), this->geometry().top(), this->geometry().width()+ newPoint.x(), this->geometry().height()+ newPoint.y());
lastMouseGlobalPosition = event->globalPos();
}
else if (bResizeByRtHit)
{
if (width() <= minimumSize().width() || height() <= minimumSize().height())
lastMouseGlobalPosition = QPoint(this->geometry().x() + width(), this->geometry().y());
QPoint newPoint = event->globalPos() - lastMouseGlobalPosition;
if (this->geometry().height() - newPoint.y() < minimumHeight())
newPoint.setY(0);
this->setGeometry(this->geometry().left(), this->geometry().top() + newPoint.y(), this->geometry().width() + newPoint.x(), this->geometry().height() - newPoint.y());
lastMouseGlobalPosition = event->globalPos();
}
else if(bResizeByLbHit)
{
if (width() <= minimumSize().width() || height() <= minimumSize().height())
lastMouseGlobalPosition = QPoint(this->geometry().x(), this->geometry().y() + height());
QPoint newPoint = event->globalPos() - lastMouseGlobalPosition;
if (this->geometry().width() - newPoint.x() < minimumWidth())
newPoint.setX(0);
this->setGeometry(this->geometry().left() + newPoint.x(), this->geometry().top(), this->geometry().width() - newPoint.x(), this->geometry().height() + newPoint.y());
lastMouseGlobalPosition = event->globalPos();
}
else if (bResizeByLtHit)
{
if (width() <= minimumSize().width() || height() <= minimumSize().height())
lastMouseGlobalPosition = QPoint(this->geometry().x(), this->geometry().y());
QPoint newPoint = event->globalPos() - lastMouseGlobalPosition;
if (this->geometry().width() - newPoint.x() < minimumWidth())
newPoint.setX(0);
if (this->geometry().height() - newPoint.y() < minimumHeight())
newPoint.setY(0);
this->setGeometry(this->geometry().left() + newPoint.x(), this->geometry().top() + newPoint.y(), this->geometry().width() - newPoint.x(), this->geometry().height() - newPoint.y());
lastMouseGlobalPosition = event->globalPos();
}
else if (bResizeByTopHit)
{
if (width() <= minimumSize().width() || height() <= minimumSize().height())
lastMouseGlobalPosition = QPoint(this->geometry().x(), this->geometry().y());
QPoint newPoint = event->globalPos() - lastMouseGlobalPosition;
if (this->geometry().height() - newPoint.y() < minimumHeight())
newPoint.setY(0);
this->setGeometry(this->geometry().left(), this->geometry().top() + newPoint.y(), this->geometry().width(), this->geometry().height() - newPoint.y());
lastMouseGlobalPosition = event->globalPos();
}
else if (bResizeByBottomHit)
{
if (width() <= minimumSize().width() || height() <= minimumSize().height())
lastMouseGlobalPosition = QPoint(this->geometry().x(), this->geometry().y() + height());
QPoint newPoint = event->globalPos() - lastMouseGlobalPosition;
this->setGeometry(this->geometry().left(), this->geometry().top(), this->geometry().width(), this->geometry().height() + newPoint.y());
lastMouseGlobalPosition = event->globalPos();
}
else if (bResizeByLeftHit)
{
if (width() <= minimumSize().width() || height() <= minimumSize().height())
lastMouseGlobalPosition = QPoint(this->geometry().x(), this->geometry().y());
QPoint newPoint = event->globalPos() - lastMouseGlobalPosition;
if (this->geometry().width() - newPoint.x() < minimumWidth())
newPoint.setX(0);
this->setGeometry(this->geometry().left() + newPoint.x(), this->geometry().top(), this->geometry().width() - newPoint.x(), this->geometry().height());
lastMouseGlobalPosition = event->globalPos();
}
else if (bResizeByRightHit)
{
if (width() <= minimumSize().width() || height() <= minimumSize().height())
lastMouseGlobalPosition = QPoint(this->geometry().x()+width(), this->geometry().y());
QPoint newPoint = event->globalPos() - lastMouseGlobalPosition;
this->setGeometry(this->geometry().left(), this->geometry().top(), this->geometry().width() + newPoint.x(), this->geometry().height());
lastMouseGlobalPosition = event->globalPos();
}
}
void CustomTitleBar::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
UpdateTitle();
}
void CustomTitleBar::closeEvent(QCloseEvent* event)
{
if(p_child){
p_child->setParent(NULL);
p_child->close();
}
}
void CustomTitleBar::showEvent(QShowEvent* event)
{
this->setAttribute(Qt::WA_Mapped);
QWidget::showEvent(event);
}
void CustomTitleBar::paintEvent(QPaintEvent* event)
{
QPainter p(this);
QStyleOption opt;
opt.init(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//使用QSS
QWidget::paintEvent(event);
}
void CustomTitleBar::focusOutEvent(QFocusEvent* event)
{
bIsMousePressed = false;
bResizeByRbHit = false;
bResizeByRtHit = false;
bResizeByLbHit = false;
bResizeByLtHit = false;
bResizeByTopHit = false;
bResizeByBottomHit = false;
bResizeByLeftHit = false;
bResizeByRightHit = false;
QWidget::focusOutEvent(event);
}
void CustomTitleBar::actionMin()
{
showMinimized();
}
void CustomTitleBar::actionMax()
{
if (bIsMax)
{
this->showNormal();
bIsMax = false;
p_maxButton->setIcon(maxPix);
}
else
{
this->showMaximized();
bIsMax = true;
p_maxButton->setIcon(normalPix);
}
}
void CustomTitleBar::actionClose()
{
close(); //关闭
}
void CustomTitleBar::customUpdate()
{
//最好判断下是否允许切换光标,不然每帧总在切换的话会导致缩放卡顿
QPoint CurrentPos = mapFromGlobal(QCursor::pos());
if (IsInside(rightBottomHit, CurrentPos)|| bResizeByRbHit)
{
if(cursor() != Qt::SizeFDiagCursor)
this->setCursor(Qt::SizeFDiagCursor);
}
else if (IsInside(rightTopHit, CurrentPos)|| bResizeByRtHit)
{
if(cursor() != Qt::SizeBDiagCursor)
this->setCursor(Qt::SizeBDiagCursor);
}
else if (IsInside(leftBottomHit, CurrentPos)|| bResizeByLbHit)
{
if(cursor() != Qt::SizeBDiagCursor)
this->setCursor(Qt::SizeBDiagCursor);
}
else if (IsInside(leftTopHit, CurrentPos)|| bResizeByLtHit)
{
if(cursor() != Qt::SizeFDiagCursor)
this->setCursor(Qt::SizeFDiagCursor);
}
else if (IsInside(topHit, CurrentPos)|| bResizeByTopHit)
{
if(cursor() != Qt::SizeVerCursor)
this->setCursor(Qt::SizeVerCursor);
}
else if (IsInside(bottomHit, CurrentPos) || bResizeByBottomHit)
{
if(cursor() != Qt::SizeVerCursor)
this->setCursor(Qt::SizeVerCursor);
}
else if (IsInside(leftHit, CurrentPos) || bResizeByLeftHit)
{
if(cursor() != Qt::SizeHorCursor)
this->setCursor(Qt::SizeHorCursor);
}
else if (IsInside(rightHit, CurrentPos)|| bResizeByRightHit)
{
if(cursor() != Qt::SizeHorCursor)
this->setCursor(Qt::SizeHorCursor);
}
else
{
if(cursor() != Qt::ArrowCursor)
this->setCursor(Qt::ArrowCursor);
}
}
void CustomTitleBar::UpdateWidgetStyleSheet()
{
//p_labelTitle->setStyleSheet("#WindowTitleString{color:rgb(200,200,200); }");
//p_minButton->setStyleSheet("#TitleMinButton{background-color:rgb(130,130,130);color:rgb(255,255,255);border-top-left-radius:5px;border-top-right-radius:5px; }#TitleMinButton:pressed{background-color:rgb(125,125,255);border-top-left-radius:5px;border-top-right-radius:5px;}");
//p_maxButton->setStyleSheet("#TitleMaxButton{background-color:rgb(130,130,130);color:rgb(255,255,255);border-top-left-radius:5px;border-top-right-radius:5px;}#TitleMaxButton:pressed{background-color:rgb(255,255,125);border-top-left-radius:5px;border-top-right-radius:5px;}");
//p_closeButton->setStyleSheet("#TitleCloseButton{background-color:rgb(130,130,130) ;color:rgb(255,255,255);border-top-left-radius:5px;border-top-right-radius:5px;}#TitleCloseButton:pressed{background-color:rgb(255,100,100);border-top-left-radius:5px;border-top-right-radius:5px;}");
//setStyleSheet("#CustomTitleWindow{background-color :rgb(25,25,25);\
// border-radius:5px;\
// }");
p_labelTitle->setStyleSheet(GetWidgetStyleSheetFromFile(p_labelTitle->objectName()));
p_minButton->setStyleSheet(GetWidgetStyleSheetFromFile(p_minButton->objectName()));
p_maxButton->setStyleSheet(GetWidgetStyleSheetFromFile(p_maxButton->objectName()));
p_closeButton->setStyleSheet(GetWidgetStyleSheetFromFile(p_closeButton->objectName()));
this->setStyleSheet(GetWidgetStyleSheetFromFile(this->objectName()));
//标题栏的图片样式
switch (bkImageState)
{
case 0:
p_backgroundImage->setStyleSheet("background-color:black;background-image:url(" + bkUrl + ");background-position:center;border-top-left-radius:5px;border-top-right-radius:5px; ");
break;
case 1:
p_backgroundImage->setStyleSheet("background-color:black;border-top-left-radius:5px;border-top-right-radius:5px; ");
break;
case 2:
p_backgroundImage->setStyleSheet("background-color:black;background-image:url(" + bkUrl + ");background-position:top left;border-top-left-radius:5px;border-top-right-radius:5px; ");
break;
}
}
QSS文件:
原理是以 “#ObjectName” 开始读取,直到 “};”结束一段
该文件和功能是自己写的,不会被QT所包含,所以生成程序后,需要设置生成后事件,把该文件和路径一起复制到程序的目录里。
或者自己手动复制也行。
#WindowTitleString
{
color:rgb(200,200,200);
};
#TitleMinButton
{
background-color:rgb(130,130,130);
color:rgb(255,255,255);
border-top-left-radius:5px;
border-top-right-radius:5px;
}
#TitleMinButton:pressed
{
background-color:rgb(125,125,255);
border-top-left-radius:5px;
border-top-right-radius:5px;
};
#TitleMaxButton
{
background-color:rgb(130,130,130);
color:rgb(255,255,255);
border-top-left-radius:5px;
border-top-right-radius:5px;
}
#TitleMaxButton:pressed
{
background-color:rgb(255,255,125);
border-top-left-radius:5px;
border-top-right-radius:5px;
};
#TitleCloseButton
{
background-color:rgb(130,130,130) ;
color:rgb(255,255,255);
border-top-left-radius:5px;
border-top-right-radius:5px;
}
#TitleCloseButton:pressed
{
background-color:rgb(255,100,100);
border-top-left-radius:5px;
border-top-right-radius:5px;
};
#CustomTitleWindow
{
background-color :rgb(25,25,25);
border-radius:5px;
};