自定义跟随主题控件优化

This commit is contained in:
zhaominyong 2022-03-31 11:22:14 +08:00
parent 7b7ad2d13c
commit e9e2af5afb
4 changed files with 21 additions and 36 deletions

View File

@ -30,23 +30,12 @@ const QPixmap ImageUtil::loadTheme(const QString &theme, const QString &defaultT
{ {
int origSize = size; int origSize = size;
const auto ratio = qApp->devicePixelRatio(); const auto ratio = qApp->devicePixelRatio();
if ( 2 == ratio) { size = origSize * ratio;
size += origSize;
} else if (3 == ratio) {
size += origSize;
}
QIcon icon = QIcon::fromTheme(theme); QIcon icon = QIcon::fromTheme(theme, QIcon(defaultTheme));
// 在主题中未找到,则使用默认路径的图标 QPixmap pixmap = icon.pixmap(icon.actualSize(QSize(size, size)));
if (icon.isNull()) { pixmap.setDevicePixelRatio(ratio);
QIcon iconDefault(defaultTheme); return drawSymbolicColoredPixmap(pixmap, color);
QPixmap pixmap = iconDefault.pixmap(iconDefault.actualSize(QSize(size, size)));
pixmap.setDevicePixelRatio(ratio);
return drawSymbolicColoredPixmap(pixmap, color);
}
return icon.pixmap(icon.actualSize(QSize(size, size)));
} }
const QPixmap ImageUtil::loadTheme(const QString &theme, const QString &defaultTheme, const QString& color, QSize size) const QPixmap ImageUtil::loadTheme(const QString &theme, const QString &defaultTheme, const QString& color, QSize size)
@ -57,28 +46,17 @@ const QPixmap ImageUtil::loadTheme(const QString &theme, const QString &defaultT
size.setWidth(origWidth * ratio); size.setWidth(origWidth * ratio);
size.setHeight(origHeight * ratio); size.setHeight(origHeight * ratio);
QIcon icon = QIcon::fromTheme(theme); QIcon icon = QIcon::fromTheme(theme, QIcon(defaultTheme));
// 在主题中未找到,则使用默认路径的图标 QPixmap pixmap = icon.pixmap(icon.actualSize(size));
if (icon.isNull()) { pixmap.setDevicePixelRatio(ratio);
QIcon iconDefault(defaultTheme); return drawSymbolicColoredPixmap(pixmap, color);
QPixmap pixmap = iconDefault.pixmap(iconDefault.actualSize(size));
pixmap.setDevicePixelRatio(ratio);
return drawSymbolicColoredPixmap(pixmap, color);
}
return icon.pixmap(icon.actualSize(size));
} }
const QPixmap ImageUtil::loadPixmap(QIcon &icon, const QString &color, int size) const QPixmap ImageUtil::loadPixmap(QIcon &icon, const QString &color, int size)
{ {
int origSize = size; int origSize = size;
const auto ratio = qApp->devicePixelRatio(); const auto ratio = qApp->devicePixelRatio();
if ( 2 == ratio) { size = origSize * ratio;
size += origSize;
} else if (3 == ratio) {
size += origSize;
}
QPixmap pixmap = icon.pixmap(QSize(size, size)); QPixmap pixmap = icon.pixmap(QSize(size, size));

View File

@ -25,6 +25,7 @@ MyIconButton::MyIconButton(QWidget *parent) :
m_iconButton->setFixedSize(QSize(24, 24)); m_iconButton->setFixedSize(QSize(24, 24));
m_iconButton->setFocusPolicy(Qt::NoFocus); m_iconButton->setFocusPolicy(Qt::NoFocus);
m_iconButton->setFlat(true); m_iconButton->setFlat(true);
// m_iconButton->setProperty("useIconHighlightEffect", 0x8);
QString iconBtnQss = QString("QPushButton:checked{border: none;}" QString iconBtnQss = QString("QPushButton:checked{border: none;}"
"QPushButton:!checked{border: none;}"); "QPushButton:!checked{border: none;}");
m_iconButton->setStyleSheet(iconBtnQss); m_iconButton->setStyleSheet(iconBtnQss);

View File

@ -15,6 +15,7 @@ PixmapLabel::PixmapLabel(QWidget *parent) :
PixmapLabel::~PixmapLabel() PixmapLabel::~PixmapLabel()
{} {}
// 应用场景一:设置深浅主题分别展示的图片
void PixmapLabel::setLightAndDarkPixmap(const QString &light, const QString &dark) void PixmapLabel::setLightAndDarkPixmap(const QString &light, const QString &dark)
{ {
m_light = light; m_light = light;
@ -37,7 +38,7 @@ void PixmapLabel::on_styleNameChanged(bool isDark)
} }
} }
// 设置跟随主题图标 // 应用场景二:设置跟随主题风格图标,不随深浅背景色变化
void PixmapLabel::setUkuiIconSchema(const QString &schema, QSize size) void PixmapLabel::setUkuiIconSchema(const QString &schema, QSize size)
{ {
m_iconTheme = schema; m_iconTheme = schema;
@ -49,16 +50,21 @@ void PixmapLabel::setUkuiIconSchema(const QString &schema, QSize size)
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged); connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged);
} }
// 应用场景三:设置跟随主题风格图标(主题中也可能不存在该图标),随深浅色背景色变化
void PixmapLabel::setUkuiIconSchema(const QString &schema, const QString &schemaDefault, QSize size) void PixmapLabel::setUkuiIconSchema(const QString &schema, const QString &schemaDefault, QSize size)
{ {
m_iconTheme = schema; m_iconTheme = schema;
m_iconThemeDefault = schemaDefault; m_iconThemeDefault = schemaDefault;
m_iconSize = size; m_iconSize = size;
// setProperty("useIconHighlightEffect", 0x8);
on_themeIconChanged(); on_themeIconChanged();
disconnect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged); disconnect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged);
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged); connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::themeIconChanged, this, &PixmapLabel::on_themeIconChanged);
disconnect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, &PixmapLabel::on_themeIconChanged);
connect(GlobelBackupInfo::inst().getGlobalSignals(), &GlobalSignals::styleNameChanged, this, &PixmapLabel::on_themeIconChanged);
} }
void PixmapLabel::on_themeIconChanged() void PixmapLabel::on_themeIconChanged()

View File

@ -12,12 +12,12 @@ public:
explicit PixmapLabel(QWidget *parent = nullptr); explicit PixmapLabel(QWidget *parent = nullptr);
~PixmapLabel(); ~PixmapLabel();
// 设置深浅主题分别展示的图片 // 应用场景一:设置深浅主题分别展示的图片
void setLightAndDarkPixmap(const QString &light, const QString &dark); void setLightAndDarkPixmap(const QString &light, const QString &dark);
// 设置跟随主题图标 // 应用场景二:设置跟随主题风格图标,不随深浅背景色变化
void setUkuiIconSchema(const QString &schema, QSize size = QSize(24, 24)); void setUkuiIconSchema(const QString &schema, QSize size = QSize(24, 24));
// 设置跟随主题图标 // 应用场景三:设置跟随主题风格图标(主题中也可能不存在该图标),随深浅色背景色变化
void setUkuiIconSchema(const QString &schema, const QString &schemaDefault, QSize size = QSize(16, 16)); void setUkuiIconSchema(const QString &schema, const QString &schemaDefault, QSize size = QSize(16, 16));
private slots: private slots: