feat: change *.svg color

This commit is contained in:
XMuli 2023-03-10 01:31:08 +08:00
parent c8771255e8
commit f7d7e710e6
No known key found for this signature in database
GPG Key ID: 9554B5DD5B8E986A
2 changed files with 24 additions and 18 deletions

View File

@ -69,44 +69,34 @@ Widget::Widget(QWidget *parent)
{ {
setWindowTitle("change .svg color"); setWindowTitle("change .svg color");
resize(1600, 1000); resize(1600, 1000);
const int width = 80;
QIcon icon(ChangeSVGColor(":/mosaic.svg", QColor(Qt::green).name(), QSize(width, width))); QIcon icon(ChangeSVGColor(":/mosaic.svg", QColor(Qt::green).name(), QSize(m_width, m_width)));
m_btn = new QToolButton(this); m_btn = new QToolButton(this);
m_btn->setCheckable(true); m_btn->setCheckable(true);
m_btn->setToolButtonStyle(Qt::ToolButtonIconOnly); m_btn->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_btn->setAutoRaise(true); // 自动浮动模式 m_btn->setAutoRaise(true); // 自动浮动模式
m_btn->setIcon(icon); m_btn->setIcon(icon);
m_btn->setIconSize(QSize(width, width)); m_btn->setIconSize(QSize(m_width, m_width));
// m_btn->resize(200, 200); // m_btn->resize(200, 200);
m_btn->move(0, 0); m_btn->move(0, 0);
QIcon icon2(":/mosaic.svg"); // QIcon icon2(":/mosaic.svg");
m_btn2 = new QToolButton(this); m_btn2 = new QToolButton(this);
m_btn2->setCheckable(true); m_btn2->setCheckable(true);
m_btn2->setToolButtonStyle(Qt::ToolButtonIconOnly); m_btn2->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_btn2->setAutoRaise(true); // 自动浮动模式 m_btn2->setAutoRaise(false); // 自动浮动模式
m_btn2->setIcon(icon); m_btn2->setIcon(icon);
m_btn2->setIconSize(QSize(width, width)); m_btn2->setIconSize(QSize(m_width, m_width));
// m_btn->resize(200, 200); // m_btn->resize(200, 200);
m_btn2->move(100, 100); m_btn2->move(200, 0);
m_btn->show(); m_btn->show();
m_btn2->show(); m_btn2->show();
connect(m_btn, &QToolButton::clicked, this, [&](bool checked){
QString path(":/mosaic.svg");
QIcon ico; connect(m_btn, &QToolButton::clicked, this, &Widget::onBtnChecked);
if (checked) connect(m_btn2, &QToolButton::clicked, this, &Widget::onBtnChecked);
ico.addFile(path);
else
ico = ChangeSVGColor(":/mosaic.svg", QColor(Qt::green).name(), QSize(width, width));
m_btn->setIcon(ico);
});
@ -138,6 +128,19 @@ void Widget::paintEvent(QPaintEvent *event)
update(); update();
} }
void Widget::onBtnChecked(bool checked)
{
QString path(":/mosaic.svg");
QIcon ico;
if (checked)
ico.addFile(path);
else
ico = ChangeSVGColor(path, QColor(Qt::green).name(), QSize(m_width, m_width));
auto bt = qobject_cast<QToolButton*>(sender());
bt->setIcon(ico);
}

View File

@ -18,10 +18,13 @@ public:
protected: protected:
virtual void paintEvent(QPaintEvent *event) override; virtual void paintEvent(QPaintEvent *event) override;
public slots:
void onBtnChecked(bool checked);
private: private:
QToolButton* m_btn; QToolButton* m_btn;
QToolButton* m_btn2; QToolButton* m_btn2;
const int m_width = 100;
}; };