Qt中自定义标题栏(C++)(附源码)

5 篇文章 0 订阅

1、在开发软件时,往往会根据需求自定义标题栏,由于qt自带的标题栏不支持自定义。因此以下是一个自定义标题栏的class;

2、此自定义标题栏包括自定义标题栏icon、标题栏文本、标题栏背景、放大、缩小、关闭按钮图标自定义、以及自定义按钮组背景图片;

废话不多说,上源码,直接加入到工程中引用:

头文件 subTitleBar.h

#ifndef SUBTITLEBAR_H
#define SUBTITLEBAR_H


#include <QObject>
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QFont>
#include <QStyle>
#include <QDebug>
#include <QListView>
#include <QShortcut>
#include <QComboBox>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QGraphicsDropShadowEffect>
#include <QDesktopWidget>
#include <QApplication>
#include "windows.h"

class subTitleBar : public QWidget
{
	Q_OBJECT

public:
	explicit subTitleBar(QWidget *parentWidget = nullptr);
	~subTitleBar();
	void Init_Botton();                             //初始化按钮
	void setWindowTitle(const QString &title);      //设置标题
	void setWindowIcon(const QString &icon);        //设置图标
	void setMaxIcon(const QString &icon);			//设置图标
	void setMinIcon(const QString &icon);			//设置图标
	void setCloseIcon(const QString &icon);        //设置图标
	void Shadow_Warning();                          //设置阴影
	void Set_MaximizeFlag();                        //设置双击标题栏是否可以最大、最小化
	const static int TITLEBARHEIGHT = 50;           //标题栏高度
	const static int CONTROLWIDTH = 30;           //控件宽度


public slots:
	void showMax();                                 //最大化窗口
	void showMin();                                 //最小化窗口
	void showClose();                               //关闭窗口
	void set_ChildWindowColor(int R, int G, int B);   //设置标题栏背景色

signals:
	void send_close();                  //发送关闭信号
	void send_switch();
	void send_ResizePushButton();                   //全屏调整按钮大小

private:

	bool            mousePress;                     //按钮点击标志位
	QPoint          movePoint;                      //鼠标移动
	QPushButton     *maxButton;                     //最大化按钮
	QPushButton     *minButton;                     //最小化按钮
	QPushButton     *closeButton;                   //关闭按钮
	QLabel          *imgLabel;                      //图片框
	QWidget         *parentWidget;                  //父窗口
	QLabel          *titleLabel;                    //标题名

	int             switchFlag;
	int             MaximizeFlag;                   //双击是否最大化
	int             Original_window_x;              //窗口x坐标
	int             Original_window_y;              //窗口y坐标
	int             Original_window_Mywidth;        //窗口宽度
	int             Original_window_Myheight;       //窗口高度

	void initValue();                               //初始化值
	void mousePressEvent(QMouseEvent * event);      //鼠标点击事件
	void mouseReleaseEvent(QMouseEvent *);          //鼠标释放事件
	void mouseMoveEvent(QMouseEvent * event);       //鼠标移动事件
	void mouseDoubleClickEvent(QMouseEvent *event); //鼠标双击事件
};

#endif // SUBTITLEBAR_H


源文件subTitleBar.cpp

#include "subTitleBar.h"

/***************************            构造函数              ***************************/
subTitleBar::subTitleBar(QWidget *parent) : QWidget(parent)
{
	
	//初始化按钮
	Init_Botton();
	//设置背景色
	set_ChildWindowColor(23, 40, 68);
	//重置窗口大小
	this->resize(parent->width(), TITLEBARHEIGHT);
	//设置父类窗口
	parentWidget = parent;
	//初始化
	initValue();
	//设置阴影
	Shadow_Warning();	
}

subTitleBar::~subTitleBar()
{
	if (parentWidget)
	{
		parentWidget = nullptr;
		//delete parentWidget;
	}
}

void subTitleBar::Init_Botton()
{
	//最大化按钮设置图标
	maxButton = new QPushButton(this);
	//最小化按钮设置图标
	minButton = new QPushButton(this);
	//关闭按钮设置图标
	closeButton = new QPushButton(this);
	//设置标签
	imgLabel = new QLabel(this);
	titleLabel = new QLabel(this);
	titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	titleLabel->setAlignment(Qt::AlignVCenter);

	//设置控件大小
	//imgLabel->setFixedSize(CONTROLWIDTH, CONTROLWIDTH);
	//imgLabel->setScaledContents(true);

	//设置控件大小
	minButton->setFixedSize(CONTROLWIDTH, CONTROLWIDTH);
	maxButton->setFixedSize(CONTROLWIDTH, CONTROLWIDTH);
	closeButton->setFixedSize(CONTROLWIDTH, CONTROLWIDTH);

	//设置鼠标移至按钮上的提示信息
	closeButton->setShortcut(tr("Ctrl+w"));

	//设置布局
	QHBoxLayout *hBoxLayout = new QHBoxLayout(this);
	//hBoxLayout->addSpacing(5);
	QLabel *label = new QLabel(this);
	label->setStyleSheet("QLabel{background-color: rgb(23, 40, 68);color: rgb(255, 255, 255);}");
	label->setMinimumSize(QSize(10,0));
	hBoxLayout->addWidget(label);
	hBoxLayout->addWidget(imgLabel);
	QLabel *label2 = new QLabel(this);
	label2->setStyleSheet("QLabel{background-color: rgb(23, 40, 68);color: rgb(255, 255, 255);}");
	label2->setMinimumSize(QSize(10, 0));
	hBoxLayout->addWidget(label2);
	hBoxLayout->addWidget(titleLabel);
	hBoxLayout->addSpacing(0);
	QWidget* widget = new QWidget(this);
	QHBoxLayout *widgetLayout = new QHBoxLayout(widget);
	widget->setContentsMargins(0, 0, 0, 0);
	widget->setObjectName("subTitle");
	widget->setStyleSheet("QWidget#subTitle{ background-image: url(:/style/image/state.png);min-height:50px;}");
	widgetLayout->addWidget(minButton);
	widgetLayout->addWidget(maxButton);
	widgetLayout->addWidget(closeButton);
	widgetLayout->setContentsMargins(30, 0, 20, 0);


	hBoxLayout->addWidget(widget);
	hBoxLayout->setSpacing(0);
	hBoxLayout->setContentsMargins(0, 0, 0, 0);
	this->setLayout(hBoxLayout);
	MaximizeFlag = 0;
}

/***************************            设置标题              ***************************/
void subTitleBar::setWindowTitle(const QString &title)
{
	QFont myFont;
	//设置文字大小
	myFont.setPointSize(18);
	//设置文字字体
	myFont.setFamily("Aqency FB");
	titleLabel->setFont(myFont);
	titleLabel->setText(title);
}



/***************************            设置图标              ***************************/
void subTitleBar::setWindowIcon(const QString &icon)
{
	imgLabel->setPixmap(QPixmap(icon));
}
void subTitleBar::setMinIcon(const QString &icon)
{
	minButton->setIcon(QPixmap(icon));
}
void subTitleBar::setMaxIcon(const QString &icon)
{
	maxButton->setIcon(QPixmap(icon));
}
void subTitleBar::setCloseIcon(const QString &icon)
{
	closeButton->setIcon(QPixmap(icon));
}

void subTitleBar::Shadow_Warning()
{
	//窗口添加阴影效果
	QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
	shadow_effect->setOffset(0, 2);
	shadow_effect->setColor(Qt::gray);
	shadow_effect->setBlurRadius(3);
	this->setGraphicsEffect(shadow_effect);
}

//设置双击标题栏是否可以最大、最小化
void subTitleBar::Set_MaximizeFlag()
{
	MaximizeFlag = 1;
}

/***************************            初始化              ***************************/
void subTitleBar::initValue()
{
	//设置样式表
	closeButton->setStyleSheet("QPushButton{background-color:transparent;}QPushButton:hover{padding-left:6px;padding-top:6px;}");
	minButton->setStyleSheet("QPushButton{background-color:transparent;}QPushButton:hover{padding-left:6px;padding-top:6px;}");
	maxButton->setStyleSheet("QPushButton{background-color:transparent;}QPushButton:hover{padding-left:6px;padding-top:6px;}");
	imgLabel->setStyleSheet("QLabel{background-color: rgb(23, 40, 68);color: rgb(255, 255, 255);}");
	titleLabel->setStyleSheet("QLabel{background-color: rgb(23, 40, 68);color: rgb(255, 255, 255);}");
	
	//连接信号与槽
	connect(minButton, SIGNAL(clicked(bool)), this, SLOT(showMin()));
	connect(maxButton, SIGNAL(clicked(bool)), this, SLOT(showMax()));
	connect(closeButton, SIGNAL(clicked(bool)), this, SLOT(showClose()));

	//按钮点击标志位
	mousePress = false;

	//将该窗口添加到父类窗口中
	QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
	vBoxLayout->addWidget(this);
	vBoxLayout->addStretch();
	vBoxLayout->setSpacing(0);
	vBoxLayout->setContentsMargins(0, 0, 0, 0);
	parentWidget->setLayout(vBoxLayout);
}

/***************************            最大化              ***************************/
void subTitleBar::showMax()
{
	if (parentWidget->isTopLevel())
	{
		parentWidget->isMaximized() ? parentWidget->showNormal() : parentWidget->showMaximized();
	}
}

/***************************            最小化              ***************************/
void subTitleBar::showMin()
{
	parentWidget->showMinimized();
}

/***************************      发送关闭信号给主窗口        ***************************/
void subTitleBar::showClose()
{
	emit send_close();
}

void subTitleBar::set_ChildWindowColor(int R, int G, int B)
{
	//设置背景色
	QPalette paletteColor(palette());
	paletteColor.setColor(QPalette::Background, QColor(R, G, B));
	this->setAutoFillBackground(true);
	this->setPalette(paletteColor);
}


/***************************           鼠标点击             ***************************/
void subTitleBar::mousePressEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton)
	{
		mousePress = true;
	}
	movePoint = event->globalPos() - parentWidget->pos();
}

/**************************             鼠标释放              ***************************/
void subTitleBar::mouseReleaseEvent(QMouseEvent *)
{
	mousePress = false;
}

/**************************             鼠标移动              **************************/
void subTitleBar::mouseMoveEvent(QMouseEvent *event)
{
	if (parentWidget->isFullScreen())
	{
		//判断当前是全屏就不允许拖动窗口
	}
	else
	{
		//当前不是全屏可以拖动窗口
		if (mousePress)
		{
			QPoint movePos = event->globalPos();
			parentWidget->move(movePos - movePoint);
		}
	}
}

/**************************             鼠标双击              **************************/
void subTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton)
	{
		int x = event->x();
		int y = event->y();
		if ((x > 0 && x < parentWidget->width()) && (y > 0 && y < TITLEBARHEIGHT)) //如果双击的范围在标题栏的范围之内
		{
			showMax(); //调用全屏、退出全屏函数
		}
	}
}


3、在需要自定义标题栏处调用对应的接口即可实现自定义标题

ui.widget_title->setWindowTitle(QStringLiteral("Qt自定义标题 V 1.0.1"));
ui.widget_title->setWindowIcon(":/style/image/title.png");
ui.widget_title->setMinIcon(":/style/image/min.png");
ui.widget_title->setMaxIcon(":/style/image/max.png");
ui.widget_title->setCloseIcon(":/style/image/close.png");
connect(ui.widget_title, &subTitleBar::send_close, [=]() {this->close(); });

版权声明:本文为博主原创文章,转载请附上博文链接!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值