QT自定义TitleBar

头文件

#ifndef TITLEBAR_H
#define TITLEBAR_H

/*************************************************

功能:重写标题栏
作者: YYC
时间:2017-10-13

*************************************************/

#include
#include
#include
#include
#include
#include
#include


#ifndef SAFEDELETE
#define SAFEDELETE(pointer) \
{ \
if(pointer) \
{ \
delete pointer; \
} \
pointer = NULL; \
}
#endif

namespace TITLEBAR
{
enum WIDGETBYTTONTYPE
{
MAXWIDGET,
MINWIDGET,
MAXMINWIDGET,
};
}

class TitleBar : public QWidget
{
Q_OBJECT
public:
explicit TitleBar(QWidget *parentWidget = 0);

void setWindowTitle(const QString &title); //设置标题
void setWindowIcon(const QString &icon); //设置图标
void subWindowButton(const int &type); //设置按钮

const static int TITLEBARHEIGHT = 50; //标题栏高度
const static int CONTROLWIDTH = 30; //控件宽度

public slots:
void showMax(); //最大化窗口
void showMin(); //最小化窗口
void showClose(); //关闭窗口

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

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

#endif // TITLEBAR_H


源文件

#include "titlebar.h"
#include
#include
#include
#include

/*************************** 构造函数 ***************************/
TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
{
//最大化按钮设置图标
QPixmap pixMap = this->style()-> standardPixmap(QStyle:: SP_TitleBarMaxButton);
maxButton = new QPushButton(this);
maxButton->setIcon(pixMap);

//最小化按钮设置图标
pixMap = this->style()-> standardPixmap(QStyle:: SP_TitleBarMinButton);
minButton = new QPushButton(this);
minButton->setIcon(pixMap);

//关闭按钮设置图标
pixMap = this->style()-> standardPixmap(QStyle:: SP_TitleBarCloseButton);
closeButton = new QPushButton(this);
closeButton->setIcon(pixMap);

//设置标签
imgLabel = new QLabel(this);
titleLabel = new QLabel(this);
titleLabel->setSizePolicy(QSizePolicy:: Expanding, QSizePolicy:: Fixed);
titleLabel->setStyleSheet("color:white");

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

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

//设置布局
QHBoxLayout *hBoxLayout = new QHBoxLayout(this);
hBoxLayout->addWidget(imgLabel);
hBoxLayout->addSpacing(5);
hBoxLayout->addWidget(titleLabel);
hBoxLayout->addWidget(minButton);
hBoxLayout->addWidget(maxButton);
hBoxLayout->addWidget(closeButton);
hBoxLayout->setSpacing(0);
hBoxLayout->setContentsMargins(5, 0, 5, 0);
this->setLayout(hBoxLayout);

//设置背景色
QPalette paletteColor(palette());
paletteColor.setColor(QPalette:: Background, QColor(44, 62, 80));
this->setAutoFillBackground(true);
this->setPalette(paletteColor);

//重置窗口大小
this->resize(parent->width(), TITLEBARHEIGHT);

//设置父类窗口
parentWidget = parent;

this->initValue();
}

/*************************** 设置标题 ***************************/
void TitleBar::setWindowTitle(const QString &title)
{
titleLabel->setText(title);
}

/*************************** 设置图标 ***************************/
void TitleBar::setWindowIcon(const QString &icon)
{
imgLabel->setPixmap(QPixmap(icon));
}

/*************************** 设置按钮 ***************************/
void TitleBar::subWindowButton(const int &type)
{
if(type == TITLEBAR:: MINWIDGET)
{
SAFEDELETE(minButton);
}
else if(type == TITLEBAR:: MAXWIDGET)
{
SAFEDELETE(maxButton);
}
else if(type == TITLEBAR:: MAXMINWIDGET)
{
SAFEDELETE(minButton);
SAFEDELETE(maxButton);
}
}

/*************************** 初始化 ***************************/
void TitleBar::initValue()
{
//设置样式表
closeButton->setStyleSheet("background-color:transparent;");
minButton->setStyleSheet("background-color:transparent;");
maxButton->setStyleSheet("background-color:transparent;");

//连接信号与槽
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(parentWidget);

vBoxLayout->addWidget(this);
vBoxLayout->addStretch();
vBoxLayout->setSpacing(0);
vBoxLayout->setContentsMargins(0, 0, 0, 0);
parentWidget->setLayout(vBoxLayout);
}

/*************************** 最大化 ***************************/
void TitleBar::showMax()
{
static int count = 0;

if(count % 2 == 0)
{
QPixmap normalPix = style()-> standardPixmap(QStyle:: SP_TitleBarNormalButton);
maxButton->setIcon(normalPix);

parentWidget->showMaximized();
}
else
{
QPixmap maxPix = style()-> standardPixmap(QStyle:: SP_TitleBarMaxButton);
maxButton->setIcon(maxPix);
parentWidget->showNormal();
}

count ++;
}

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

/*************************** 关闭 ***************************/
void TitleBar::showClose()
{
parentWidget->close();
}

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

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

/************************** 鼠标移动 **************************/
void TitleBar:: mouseMoveEvent(QMouseEvent *event)
{
if(mousePress)
{
QPoint movePos = event->globalPos();
parentWidget->move(movePos - movePoint);
}
}

在窗口中使用

mainwindow.h 文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include
#include "titlebar.h"
#include
#include

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~ MainWindow();

private:
Ui::MainWindow *ui;
TitleBar *titleBar;
void resizeEvent(QResizeEvent *event);
};

#endif // MAINWINDOW_H


mainwindow.cpp文件

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

this->setWindowFlags(Qt:: FramelessWindowHint);

titleBar = new TitleBar(this);

titleBar->setWindowIcon(":/image/image/image.png");
titleBar->setWindowTitle("灏夏星辰");
}

MainWindow::~ MainWindow()
{
delete ui;
}

/************************ 改变事件 ************************/
void MainWindow:: resizeEvent(QResizeEvent *event)
{
titleBar->resize(this->width(), TitleBar::TITLEBARHEIGHT);
}

具体效果:
QT自定义TitleBar_第1张图片
下载链接可到我有道云笔记下载附件http://note.youdao.com/noteshare?id=b88a24b0117b30772074400611635fb7&sub=AAF079588C854419B55B76D1543EFAA8

你可能感兴趣的:(Qt)