Qt自定义 titlebr
【摘要】
效果图
代码路径: 包含了很多的小例子,可以给我个星哦。 https://github.com/hiwoshixiaoyu/Qt-Demo
titlebar.h
#ifndef TITILEBA...
效果图

代码路径:
包含了很多的小例子,可以给我个星哦。
https://github.com/hiwoshixiaoyu/Qt-Demo
titlebar.h
#ifndef TITILEBAR_H
#define TITILEBAR_H
#include <QLabel>
#include <QObject>
#include <QPushButton>
#include <QWidget>
enum BtnType
{
BTN_CLOSE,
BTN_MAX,
BTN_MIN,
};
class TitileBar : public QWidget
{
Q_OBJECT
public:
explicit TitileBar(QWidget *parent = nullptr);
virtual ~TitileBar();
private:
//void mouseDoubleClickEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
// 加载样式文件;
void loadStyleSheet(const QString &sheetName);
signals:
void signalTitlebarBtn(BtnType type);
private slots:
void sendSingalMax();
void sendSingalMin();
void sendSingalClose();
private:
// 移动窗口的变量;
bool m_isPressed;
QPoint m_startMovePos;
QPushButton *m_btnMax;
QPushButton *m_btnMin;
QPushButton *m_btnClose;
QLabel *m_titleText;
};
#endif // TITILEBAR_H
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
titlebar.cpp
#include "titilebar.h"
#include <QMouseEvent>
#include <QPushButton>
#include <QSizePolicy>
TitileBar::TitileBar(QWidget *parent) : QWidget(parent)
{
setObjectName("bg");
//如果不加这个属性,填充颜色会不好用
setAutoFillBackground(true);
QPalette palette;
//填充颜色
palette.setColor(QPalette::Background, QColor(150,150,150,100));
//添加自己的图片
//palette.setBrush(QPalette::Background,QBrush(QPixmap(":/1.png")));
setPalette(palette);
//设置大小
setFixedSize(800,50);
m_btnMax =new QPushButton(this);
m_btnMax->setText("最大化");
m_btnMin =new QPushButton(this);
m_btnMin->setText("最小化");
m_btnClose =new QPushButton(this);
m_btnClose->setText("关闭");
m_btnMax->move(500,20);
m_btnMin->move(600,20);
m_btnClose->move(700,20);
m_titleText = new QLabel(this);
m_titleText->setText("Qt demo 大全");
connect(m_btnMax,SIGNAL(clicked()),SLOT(sendSingalMax()));
connect(m_btnMin,SIGNAL(clicked()),SLOT(sendSingalMin()));
connect(m_btnClose,SIGNAL(clicked()),SLOT(sendSingalClose()));
//添加qss样式
// loadStyleSheet("maintitle");
}
TitileBar::~TitileBar()
{
}
//双击最大化没有实现
//void TitileBar::mouseDoubleClickEvent(QMouseEvent *event)
//{
//
//}
void TitileBar::mousePressEvent(QMouseEvent *event)
{
m_isPressed=true;
m_startMovePos = event->globalPos();
}
void TitileBar::mouseMoveEvent(QMouseEvent *event)
{
if (m_isPressed)
{
QPoint movePoint = event->globalPos() - m_startMovePos;
QPoint widgetPos = this->parentWidget()->pos();
m_startMovePos = event->globalPos();
this->parentWidget()->move(widgetPos.x() + movePoint.x(), widgetPos.y() + movePoint.y());
}
}
void TitileBar::mouseReleaseEvent(QMouseEvent *event)
{
m_isPressed=false;
}
void TitileBar::loadStyleSheet(const QString &sheetName)
{
// QString styleSheet="QWidget#bg{background-image: url(:/1.png);}";
//this->setStyleSheet(styleSheet);
QFile file(":/"+sheetName+".qss");
file.open(QFile::ReadOnly);
if (file.isOpen())
{
QString styleSheet = this->styleSheet();
styleSheet += QLatin1String(file.readAll());
this->setStyleSheet(styleSheet);
}
}
void TitileBar::sendSingalMax()
{
emit signalTitlebarBtn(BTN_MAX);
}
void TitileBar::sendSingalMin()
{
emit signalTitlebarBtn(BTN_MIN);
}
void TitileBar::sendSingalClose()
{
emit signalTitlebarBtn(BTN_CLOSE);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
文章来源: yujiang.blog.csdn.net,作者:鱼酱2333,版权归原作者所有,如需转载,请联系作者。
原文链接:yujiang.blog.csdn.net/article/details/84559979
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)