|
7 | 7 | #include "utils.h" |
8 | 8 |
|
9 | 9 | #include <DApplication> |
| 10 | +#include <DGuiApplicationHelper> |
10 | 11 | #include <DIconButton> |
11 | 12 |
|
| 13 | +#include <QGuiApplication> |
12 | 14 | #include <QIcon> |
| 15 | +#include <QPainter> |
13 | 16 | #include <QLabel> |
14 | 17 | #include <QDebug> |
15 | 18 | #include <QMouseEvent> |
@@ -37,6 +40,17 @@ TitleBar::TitleBar(QWidget *parent) : QWidget(parent), m_layout(new QHBoxLayout( |
37 | 40 | // this->setPalette(palette); |
38 | 41 | this->setBackgroundRole(DPalette::Base); |
39 | 42 | this->setAutoFillBackground(true); |
| 43 | + |
| 44 | + // 监听 DTK 主题类型变化,触发重绘(切换深/浅色时) |
| 45 | + QObject::connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [this](){ |
| 46 | + update(); |
| 47 | + }); |
| 48 | + |
| 49 | + // 监听 QApplication 全局调色板变化:DTK 通过 D-Bus 异步完成 palette 初始化时 |
| 50 | + QObject::connect(static_cast<QGuiApplication *>(qApp), &QGuiApplication::paletteChanged, |
| 51 | + this, [this](const QPalette &) { |
| 52 | + update(); |
| 53 | + }); |
40 | 54 | /********************* Modify by m000714 daizhengwen End ************************/ |
41 | 55 | m_layout->setContentsMargins(0, 0, 0, 0); |
42 | 56 |
|
@@ -78,6 +92,25 @@ void TitleBar::setVerResized(bool resized) |
78 | 92 | } |
79 | 93 |
|
80 | 94 |
|
| 95 | +void TitleBar::paintEvent(QPaintEvent *event) |
| 96 | +{ |
| 97 | + Q_UNUSED(event) |
| 98 | + |
| 99 | + QPainter p(this); |
| 100 | + const QColor bgColor = DGuiApplicationHelper::instance()->applicationPalette().color(QPalette::Base); |
| 101 | + p.fillRect(rect(), bgColor); |
| 102 | +} |
| 103 | + |
| 104 | +void TitleBar::changeEvent(QEvent *event) |
| 105 | +{ |
| 106 | + // 捕获系统/应用 palette 变化事件,触发重绘以保持颜色正确 |
| 107 | + if (event->type() == QEvent::ApplicationPaletteChange |
| 108 | + || event->type() == QEvent::PaletteChange) { |
| 109 | + update(); |
| 110 | + } |
| 111 | + QWidget::changeEvent(event); |
| 112 | +} |
| 113 | + |
81 | 114 | void TitleBar::mousePressEvent(QMouseEvent *event) |
82 | 115 | { |
83 | 116 | // qCDebug(views) << "Enter TitleBar::mousePressEvent"; |
|
0 commit comments