|
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,19 @@ 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 | + // 会调用 QApplication::setPalette(),从而触发此信号。这是修复首次启动白色背景 |
| 51 | + // 的关键——确保 TitleBar 在 DTK 异步就绪后立即重绘。 |
| 52 | + QObject::connect(static_cast<QGuiApplication *>(qApp), &QGuiApplication::paletteChanged, |
| 53 | + this, [this](const QPalette &) { |
| 54 | + update(); |
| 55 | + }); |
40 | 56 | /********************* Modify by m000714 daizhengwen End ************************/ |
41 | 57 | m_layout->setContentsMargins(0, 0, 0, 0); |
42 | 58 |
|
@@ -78,6 +94,28 @@ void TitleBar::setVerResized(bool resized) |
78 | 94 | } |
79 | 95 |
|
80 | 96 |
|
| 97 | +void TitleBar::paintEvent(QPaintEvent *event) |
| 98 | +{ |
| 99 | + Q_UNUSED(event) |
| 100 | + // 优先读取 DTK 应用级调色板(applicationPalette),如与 widget 继承 palette 不一致 |
| 101 | + // 时以 DTK 的为准,保证深/浅主题均正确渲染。 |
| 102 | + // setAutoFillBackground(true) 已在构造时开启,作为 Qt 层面的兜底绘制; |
| 103 | + // 此 paintEvent 覆盖在其之上,确保每次重绘都实时读取最新 palette。 |
| 104 | + QPainter p(this); |
| 105 | + const QColor bgColor = DGuiApplicationHelper::instance()->applicationPalette().color(QPalette::Base); |
| 106 | + p.fillRect(rect(), bgColor); |
| 107 | +} |
| 108 | + |
| 109 | +void TitleBar::changeEvent(QEvent *event) |
| 110 | +{ |
| 111 | + // 捕获系统/应用 palette 变化事件,触发重绘以保持颜色正确 |
| 112 | + if (event->type() == QEvent::ApplicationPaletteChange |
| 113 | + || event->type() == QEvent::PaletteChange) { |
| 114 | + update(); |
| 115 | + } |
| 116 | + QWidget::changeEvent(event); |
| 117 | +} |
| 118 | + |
81 | 119 | void TitleBar::mousePressEvent(QMouseEvent *event) |
82 | 120 | { |
83 | 121 | // qCDebug(views) << "Enter TitleBar::mousePressEvent"; |
|
0 commit comments