Qt自定义标题栏【即取即用模板】

头文件

#ifndef TITLEWDG_H
#define TITLEWDG_H

#include <QWidget>

namespace Ui {
class TitleWdg;
}

class TitleWdg : public QWidget
{
    Q_OBJECT

public:
    explicit TitleWdg(QWidget *parent = nullptr);
    ~TitleWdg();    

signals:
    void maximize();
    void minimize();
    void closeWindow();

protected:
    void paintEvent(QPaintEvent *event);

private slots:
    void on_close_clicked();
    void on_min_clicked();
    void on_max_clicked();

private:
    Ui::TitleWdg *ui;
};

#endif // TITLEWDG_H

源文件

#include "titlewdg.h"
#include "ui_titlewdg.h"

#include <QPainter>
#include <QStyleOption>

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

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

void TitleWdg::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QStyleOption opt;
    opt.init(this);

    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
}

void TitleWdg::on_close_clicked()
{
    closeWindow();
}

void TitleWdg::on_min_clicked()
{
    minimize();
}

void TitleWdg::on_max_clicked()
{
    maximize();
}

UI文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>TitleWdg</class>
 <widget class="QWidget" name="TitleWdg">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>636</width>
    <height>33</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QHBoxLayout" name="horizontalLayout">
   <property name="leftMargin">
    <number>9</number>
   </property>
   <property name="topMargin">
    <number>5</number>
   </property>
   <property name="rightMargin">
    <number>9</number>
   </property>
   <property name="bottomMargin">
    <number>5</number>
   </property>
   <item>
    <widget class="QLabel" name="logo">
     <property name="text">
      <string/>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QLabel" name="title">
     <property name="text">
      <string/>
     </property>
    </widget>
   </item>
   <item>
    <spacer name="horizontalSpacer">
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>366</width>
       <height>20</height>
      </size>
     </property>
    </spacer>
   </item>
   <item>
    <widget class="QToolButton" name="min">
     <property name="text">
      <string/>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QToolButton" name="max">
     <property name="text">
      <string/>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QToolButton" name="close">
     <property name="text">
      <string/>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

qss

#titleWdg
{
    color: #cdcdcd;
    background-color: #434343;
}

#titleWdg #max
{
    width: 26px;
    height: 26px;
    border-image: url(:/img/src/img/maximize-active.svg);
}

#titleWdg #max:hover
{
    border-image: url(:/img/src/img/maximize-hover.svg);
}

#titleWdg #min
{
    width: 26px;
    height: 26px;
    border-image: url(:/img/src/img/minimize-active.svg);
}

#titleWdg #min:hover
{
    border-image: url(:/img/src/img/minimize-hover.svg);
}

#titleWdg #close
{
    width: 26px;
    height: 26px;
    border-image: url(:/img/src/img/close-active.svg);
}

#titleWdg #close:hover
{
    border-image: url(:/img/src/img/close-hover.svg);
}

#titleWdg #logo
{
    min-width: 26px;
    height: 26px;
    border-image: url(:/img/YMTC.ico);
}

#titleWdg #title
{
    font: bold 12px;
    color: #FFFFFF;
    qproperty-text: "Working Log Tool v1.0.0";
}

相关推荐

  1. Qt定义标题模板

    2024-04-10 16:02:05       16 阅读
  2. 开箱之MyBatisPlus XML 定义分页

    2024-04-10 16:02:05       31 阅读
  3. Qt定义标题的多屏适配

    2024-04-10 16:02:05       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 16:02:05       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 16:02:05       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 16:02:05       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 16:02:05       18 阅读

热门阅读

  1. 如何利用ChatGPT提升学术论文写作效率

    2024-04-10 16:02:05       13 阅读
  2. C++笔试面试题整理

    2024-04-10 16:02:05       14 阅读
  3. minio本地文件上传/远程url上传

    2024-04-10 16:02:05       14 阅读
  4. vue项目引入代码编辑器

    2024-04-10 16:02:05       15 阅读
  5. 19、差分矩阵

    2024-04-10 16:02:05       12 阅读
  6. 蓝牙notify和indicate消息区别

    2024-04-10 16:02:05       13 阅读
  7. Unity之C#面试题(一)

    2024-04-10 16:02:05       11 阅读
  8. gin+sse实现离散的消息通知

    2024-04-10 16:02:05       14 阅读