Qt自定义标题栏_刻晴我大老婆的博客-CSDN博客_qt 自定义标题栏
Hello Qt——Qt自定义标题栏_天山老妖的博客-CSDN博客_qt自定义标题栏
Qt 之自定义界面(添加自定义标题栏)_一去丶二三里的博客-CSDN博客_qt自定义标题栏
最近程序需要一个通用的标题栏,多个窗口都需要用,需求大概是这样的:
1. 有图标(可显示可隐藏),图标可更改;
2. 有标题,而且字体大小可设置;
3. 有关闭按钮,最小化按钮(可显示可隐藏);
4. 支持双击,双击效果不同的窗体不同;
5. 窗体不需要拖拽。
6. 标题长度可设置,有的和父窗体长度不同。
参考了以上三篇文章,他们写的很好功能很全,我根据自己的需求作了一些更改,同时增加了UI文件。使用时,先在窗口布置好跟标题同样大小和位置的widget,然后提升成自己写的标题栏。

UI文件
- "1.0" encoding="UTF-8"?>
"4.0"> - <class>TitleBarclass>
-
class="QWidget" name="TitleBar"> -
"geometry"> -
-
0 -
0 -
400 -
30 -
-
-
"minimumSize"> -
-
0 -
30 -
-
-
"maximumSize"> -
-
16777215 -
30 -
-
-
"palette"> -
-
-
-
-
-
-
"windowTitle"> -
Form -
-
class="QGridLayout" name="gridLayout"> -
"leftMargin"> -
0 -
-
"topMargin"> -
0 -
-
"rightMargin"> -
0 -
-
"bottomMargin"> -
0 -
-
"spacing"> -
0 -
-
- "0" column="0">
-
class="QFrame" name="frame"> -
"minimumSize"> -
-
0 -
30 -
-
-
"maximumSize"> -
-
16777215 -
30 -
-
-
"frameShape"> - <enum>QFrame::NoFrameenum>
-
-
"frameShadow"> - <enum>QFrame::Plainenum>
-
-
class="QHBoxLayout" name="horizontalLayout"> -
"spacing"> -
0 -
-
"leftMargin"> -
5 -
-
"topMargin"> -
0 -
-
"rightMargin"> -
5 -
-
"bottomMargin"> -
0 -
-
-
class="QLabel" name="iconLabel"> -
"minimumSize"> -
-
20 -
20 -
-
-
"maximumSize"> -
-
20 -
20 -
-
-
"text"> -
-
-
"scaledContents"> - <bool>truebool>
-
-
-
-
-
class="QLabel" name="titleLabel"> -
"sizePolicy"> -
"MinimumExpanding" vsizetype="Fixed"> -
0 -
0 -
-
-
"toolTip"> -
Close -
-
"styleSheet"> -
"true">color: rgb(231, 231, 231); -
-
"indent"> -
10 -
-
-
-
-
class="QPushButton" name="minimizeButton"> -
"minimumSize"> -
-
27 -
22 -
-
-
"maximumSize"> -
-
27 -
22 -
-
-
"text"> -
-
-
"flat"> - <bool>truebool>
-
-
-
-
-
class="QPushButton" name="closeButton"> -
"minimumSize"> -
-
27 -
22 -
-
-
"maximumSize"> -
-
27 -
22 -
-
-
"text"> -
-
-
"flat"> - <bool>truebool>
-
-
-
-
-
-
-
-
-
-
头文件
- #ifndef TITLEBAR_H
- #define TITLEBAR_H
- #include
- #include
- #include
- #include
- namespace Ui {
- class TitleBar;
- }
- class TitleBar : public QWidget
- {
- Q_OBJECT
- public:
- explicit TitleBar(QWidget *parent = nullptr);
- ~TitleBar();
- void setTitleIcon(const QPixmap &icon);
- void setIconVisible(bool visible = true);
- void setMinButtonVisible(bool visible = true);
- bool isMinButtonVisible();
- void setTitle(const QString &title);
- void setTitleFont(const QFont &font);
- protected:
- virtual void mouseDoubleClickEvent(QMouseEvent *event);
- virtual bool eventFilter(QObject *obj, QEvent *event);
- public Q_SLOTS:
- void setWindowTitle(const QString &);
- signals:
- void closeClick();
- void minimizeClick();
- void changeLayout();
- private:
- Ui::TitleBar *ui;
- };
- #endif // TITLEBAR_H
源文件
- #include "titlebar.h"
- #include "ui_titlebar.h"
- #include
- #include
- TitleBar::TitleBar(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::TitleBar)
- {
- ui->setupUi(this);
- ui->closeButton->setIcon(QIcon("://images/model_close.svg"));
- ui->minimizeButton->setIcon(QIcon("://images/model_zoomout.svg"));
- connect(ui->closeButton, SIGNAL(clicked()), this, SIGNAL(closeClick()));
- connect(ui->minimizeButton, SIGNAL(clicked()), this, SIGNAL(minimizeClick()));
- //setStyleSheet("QFrame{border:1px gray;}");
- ui->titleLabel->installEventFilter(this);
- ui->iconLabel->installEventFilter(this);
- }
- TitleBar::~TitleBar()
- {
- delete ui;
- }
- void TitleBar::setTitleIcon(const QPixmap &icon)
- {
- ui->iconLabel->setPixmap(icon);
- }
- void TitleBar::setIconVisible(bool visible)
- {
- ui->iconLabel->setVisible(visible);
- this->update();
- }
- void TitleBar::setMinButtonVisible(bool visible)
- {
- ui->minimizeButton->setVisible(visible);
- this->update();
- }
- bool TitleBar::isMinButtonVisible()
- {
- return ui->minimizeButton->isVisible();
- }
- void TitleBar::setTitle(const QString &title)
- {
- ui->titleLabel->setText(title);
- }
- void TitleBar::setTitleFont(const QFont &font)
- {
- ui->titleLabel->setFont(font);
- }
- void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
- {
- emit changeLayout();
- QWidget::mouseDoubleClickEvent(event);
- }
- bool TitleBar::eventFilter(QObject *obj, QEvent *event)
- {
- switch (event->type())
- {
- case QEvent::WindowTitleChange:
- {
- QWidget *pWidget = qobject_cast
(obj); - if (pWidget)
- {
- setTitle(pWidget->windowTitle());
- return true;
- }
- break;
- }
- case QEvent::WindowIconChange:
- {
- QWidget *pWidget = qobject_cast
(obj); - if (pWidget)
- {
- QIcon icon = pWidget->windowIcon();
- setTitleIcon(icon.pixmap(ui->iconLabel->size()));
- return true;
- }
- break;
- }
- default:
- break;
- }
- return QWidget::eventFilter(obj, event);
- }
- void TitleBar::setWindowTitle(const QString &title)
- {
- setTitle(title);
- }