• 带UI的Qt自定义标题栏


            

    Qt自定义标题栏_刻晴我大老婆的博客-CSDN博客_qt 自定义标题栏

    Hello Qt——Qt自定义标题栏_天山老妖的博客-CSDN博客_qt自定义标题栏

    Qt 之自定义界面(添加自定义标题栏)_一去丶二三里的博客-CSDN博客_qt自定义标题栏

    最近程序需要一个通用的标题栏,多个窗口都需要用,需求大概是这样的:

    1. 有图标(可显示可隐藏),图标可更改;

    2. 有标题,而且字体大小可设置;

    3. 有关闭按钮,最小化按钮(可显示可隐藏);

    4. 支持双击,双击效果不同的窗体不同;

    5. 窗体不需要拖拽。

    6. 标题长度可设置,有的和父窗体长度不同。

    参考了以上三篇文章,他们写的很好功能很全,我根据自己的需求作了一些更改,同时增加了UI文件。使用时,先在窗口布置好跟标题同样大小和位置的widget,然后提升成自己写的标题栏。

     UI文件

    1. "1.0" encoding="UTF-8"?>
    2. "4.0">
    3. <class>TitleBarclass>
    4. class="QWidget" name="TitleBar">
    5. "geometry">
    6. 0
    7. 0
    8. 400
    9. 30
    10. "minimumSize">
    11. 0
    12. 30
    13. "maximumSize">
    14. 16777215
    15. 30
    16. "palette">
    17. "windowTitle">
    18. Form
    19. class="QGridLayout" name="gridLayout">
    20. "leftMargin">
    21. 0
    22. "topMargin">
    23. 0
    24. "rightMargin">
    25. 0
    26. "bottomMargin">
    27. 0
    28. "spacing">
    29. 0
    30. "0" column="0">
    31. class="QFrame" name="frame">
    32. "minimumSize">
    33. 0
    34. 30
    35. "maximumSize">
    36. 16777215
    37. 30
    38. "frameShape">
    39. <enum>QFrame::NoFrameenum>
    40. "frameShadow">
    41. <enum>QFrame::Plainenum>
    42. class="QHBoxLayout" name="horizontalLayout">
    43. "spacing">
    44. 0
    45. "leftMargin">
    46. 5
    47. "topMargin">
    48. 0
    49. "rightMargin">
    50. 5
    51. "bottomMargin">
    52. 0
    53. class="QLabel" name="iconLabel">
    54. "minimumSize">
    55. 20
    56. 20
    57. "maximumSize">
    58. 20
    59. 20
    60. "text">
    61. "scaledContents">
    62. <bool>truebool>
    63. class="QLabel" name="titleLabel">
    64. "sizePolicy">
    65. "MinimumExpanding" vsizetype="Fixed">
    66. 0
    67. 0
    68. "toolTip">
    69. Close
    70. "styleSheet">
    71. "true">color: rgb(231, 231, 231);
    72. "indent">
    73. 10
    74. class="QPushButton" name="minimizeButton">
    75. "minimumSize">
    76. 27
    77. 22
    78. "maximumSize">
    79. 27
    80. 22
    81. "text">
    82. "flat">
    83. <bool>truebool>
    84. class="QPushButton" name="closeButton">
    85. "minimumSize">
    86. 27
    87. 22
    88. "maximumSize">
    89. 27
    90. 22
    91. "text">
    92. "flat">
    93. <bool>truebool>

    头文件

    1. #ifndef TITLEBAR_H
    2. #define TITLEBAR_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. namespace Ui {
    8. class TitleBar;
    9. }
    10. class TitleBar : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit TitleBar(QWidget *parent = nullptr);
    15. ~TitleBar();
    16. void setTitleIcon(const QPixmap &icon);
    17. void setIconVisible(bool visible = true);
    18. void setMinButtonVisible(bool visible = true);
    19. bool isMinButtonVisible();
    20. void setTitle(const QString &title);
    21. void setTitleFont(const QFont &font);
    22. protected:
    23. virtual void mouseDoubleClickEvent(QMouseEvent *event);
    24. virtual bool eventFilter(QObject *obj, QEvent *event);
    25. public Q_SLOTS:
    26. void setWindowTitle(const QString &);
    27. signals:
    28. void closeClick();
    29. void minimizeClick();
    30. void changeLayout();
    31. private:
    32. Ui::TitleBar *ui;
    33. };
    34. #endif // TITLEBAR_H

    源文件

    1. #include "titlebar.h"
    2. #include "ui_titlebar.h"
    3. #include
    4. #include
    5. TitleBar::TitleBar(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::TitleBar)
    8. {
    9. ui->setupUi(this);
    10. ui->closeButton->setIcon(QIcon("://images/model_close.svg"));
    11. ui->minimizeButton->setIcon(QIcon("://images/model_zoomout.svg"));
    12. connect(ui->closeButton, SIGNAL(clicked()), this, SIGNAL(closeClick()));
    13. connect(ui->minimizeButton, SIGNAL(clicked()), this, SIGNAL(minimizeClick()));
    14. //setStyleSheet("QFrame{border:1px gray;}");
    15. ui->titleLabel->installEventFilter(this);
    16. ui->iconLabel->installEventFilter(this);
    17. }
    18. TitleBar::~TitleBar()
    19. {
    20. delete ui;
    21. }
    22. void TitleBar::setTitleIcon(const QPixmap &icon)
    23. {
    24. ui->iconLabel->setPixmap(icon);
    25. }
    26. void TitleBar::setIconVisible(bool visible)
    27. {
    28. ui->iconLabel->setVisible(visible);
    29. this->update();
    30. }
    31. void TitleBar::setMinButtonVisible(bool visible)
    32. {
    33. ui->minimizeButton->setVisible(visible);
    34. this->update();
    35. }
    36. bool TitleBar::isMinButtonVisible()
    37. {
    38. return ui->minimizeButton->isVisible();
    39. }
    40. void TitleBar::setTitle(const QString &title)
    41. {
    42. ui->titleLabel->setText(title);
    43. }
    44. void TitleBar::setTitleFont(const QFont &font)
    45. {
    46. ui->titleLabel->setFont(font);
    47. }
    48. void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
    49. {
    50. emit changeLayout();
    51. QWidget::mouseDoubleClickEvent(event);
    52. }
    53. bool TitleBar::eventFilter(QObject *obj, QEvent *event)
    54. {
    55. switch (event->type())
    56. {
    57. case QEvent::WindowTitleChange:
    58. {
    59. QWidget *pWidget = qobject_cast(obj);
    60. if (pWidget)
    61. {
    62. setTitle(pWidget->windowTitle());
    63. return true;
    64. }
    65. break;
    66. }
    67. case QEvent::WindowIconChange:
    68. {
    69. QWidget *pWidget = qobject_cast(obj);
    70. if (pWidget)
    71. {
    72. QIcon icon = pWidget->windowIcon();
    73. setTitleIcon(icon.pixmap(ui->iconLabel->size()));
    74. return true;
    75. }
    76. break;
    77. }
    78. default:
    79. break;
    80. }
    81. return QWidget::eventFilter(obj, event);
    82. }
    83. void TitleBar::setWindowTitle(const QString &title)
    84. {
    85. setTitle(title);
    86. }

  • 相关阅读:
    CSDN云IDE 初体验
    python 随机数生成
    深度学习CNN--眼睛姿态识别联练习
    qt判断当前日期的当月的最后一天是几号
    文件打包下载excel导出和word导出
    论文阅读NAM:Normalization-based Attention Module
    udev 挂载SD卡 USB设备
    开发者,你对云计算可能有些误解
    docker入门加实战—docker数据卷
    go-zero 是如何做路由管理的?
  • 原文地址:https://blog.csdn.net/gdizcm/article/details/128196631