Fix: 解决qml图标改变时不实时跟随的问题

This commit is contained in:
wangyan 2023-01-10 15:19:03 +08:00 committed by 谭静
parent a9c00bc65d
commit adaeb641d5
2 changed files with 12 additions and 5 deletions

View File

@ -31,9 +31,11 @@ KyIcon::KyIcon(QQuickPaintedItem *parent)
emit sunkenChanged();
emit onChanged();
emit icontypeChanged();
emit iconNameChanged();
update();
});
}
connect(this, &KyIcon::iconNameChanged, this, &KyIcon::updateItem);
connect(this, &KyIcon::hoverChanged, this, &KyIcon::updateItem);
connect(this, &KyIcon::selectedChanged, this, &KyIcon::updateItem);
connect(this, &KyIcon::hasFocusChanged, this, &KyIcon::updateItem);
@ -42,6 +44,7 @@ KyIcon::KyIcon(QQuickPaintedItem *parent)
connect(this, &KyIcon::onChanged, this, &KyIcon::updateItem);
connect(this, &KyIcon::icontypeChanged, this, &KyIcon::updateItem);
}
@ -52,13 +55,15 @@ void KyIcon::setIcon(const QIcon &icon)
void KyIcon::setIconName(const QString &iconName)
{
if(!QIcon::hasThemeIcon(iconName))
m_iconName = iconName;
if(!QIcon::hasThemeIcon(m_iconName))
{
m_icon = QIcon();
qWarning() << "未找到名为 " << iconName << " 的图标!";
qWarning() << "未找到名为 " << m_iconName << " 的图标!";
return;
}
m_icon = QIcon::fromTheme(iconName);
m_icon = QIcon::fromTheme(m_iconName);
emit iconNameChanged();
}
void KyIcon::paint(QPainter *painter)

View File

@ -9,7 +9,7 @@ class QStyle;
class KyIcon : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QString iconName WRITE setIconName)
Q_PROPERTY(QString iconName READ iconName WRITE setIconName NOTIFY iconNameChanged)
Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
Q_PROPERTY( bool hover READ hover WRITE setHover NOTIFY hoverChanged)
Q_PROPERTY( bool selected READ selected WRITE setSelected NOTIFY selectedChanged)
@ -25,7 +25,7 @@ public:
QIcon icon() { return m_icon; }
void setIcon(const QIcon &icon);
QString iconName(){ return m_iconName; }
void setIconName(const QString &iconName);
bool hover() const { return m_hover; }
@ -72,6 +72,7 @@ Q_SIGNALS:
void sunkenChanged();
void onChanged();
void icontypeChanged();
void iconNameChanged();
protected:
bool m_hover;
@ -84,6 +85,7 @@ protected:
private:
QIcon m_icon;
QString m_iconName;
};