yhkylin-backup-tools/kybackup/gsettingswrapper.cpp

43 lines
1.7 KiB
C++

/**
* @brief 由于当前仅用于主题变化时,备份还原工具的一些图标跟随变化,此处暂时先简单封装。后续根据使用情况另行优化。
*/
#include "gsettingswrapper.h"
#include <QIcon>
#include <QLabel>
#include <QGSettings/qgsettings.h>
#define FITTHEMEWINDOW "org.ukui.style"
/**
* @brief 绑定连接UKui风格的主题
* @param QWidget *, 跟随风格变化的窗体对象指针
*/
void GSettingsWrapper::connectUkuiStyleSchema(QWidget * widgetPtr, QSize size)
{
if (widgetPtr != nullptr && QGSettings::isSchemaInstalled(FITTHEMEWINDOW)) {
// c++11后能确保多线程并发场景的局部静态对象的唯一性
static QGSettings *pGsettingThemeData = new QGSettings(FITTHEMEWINDOW);
QObject::connect(pGsettingThemeData, &QGSettings::changed, [=](const QString &key) {
if (key == "iconThemeName") {
QIcon titleIcon = QIcon::fromTheme("yhkylin-backup-tools");
if (widgetPtr->inherits("QLabel")) {
QLabel *labelPtr = qobject_cast<QLabel *>(widgetPtr);
const QPixmap * pixmapPtr = labelPtr->pixmap();
if (pixmapPtr != nullptr) {
//labelPtr->setPixmap(titleIcon.pixmap(titleIcon.actualSize(pixmapPtr->size())));
labelPtr->setPixmap(titleIcon.pixmap(titleIcon.actualSize(size)));
} else {
labelPtr->setPixmap(titleIcon.pixmap(titleIcon.actualSize(QSize(24, 24))));
}
} else {
widgetPtr->setWindowIcon(titleIcon);
}
}
});
}
}