Change icon when QToolButton is pressed
-
I am currently setting the icon of my QToolButton (named toolButton) by using the QIcon::fromTheme approach seen below:
#include "mainwindow.h" #include "./ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QIcon::setThemeName("feather-light"); ui->toolButton->setIcon(QIcon::fromTheme("camera-icon")); } MainWindow::~MainWindow() { delete ui; }
This works fine. However, now I need something extra. My question is how can I set custom icons for when my button is pressed, disabled, and other states? For example, let's say I want my icon to change from camera-icon to audio-icon. How can I achieve this?
-
@Saviz Unfortunately, once you change the theme you also need to change the individually added pixmaps one by one again.
addPixmap
tells the icon to use exactly this pixmap – no matter what.Our software has a function
updateToolbars()
which is called when dark mode is changed (or the widget is moved to a monitor with a different resolution). This will just rebuild all icons in the toolbar. I don't think there is a shortcut. -
QIcon can contain difffent images for different states: QIcon::addPixmap()
-
This is what I managed to figure out until now:
#include "mainwindow.h" #include "./ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QIcon::setThemeName("feather-dark"); QIcon icon; icon.addPixmap( QIcon::fromTheme("align-justify").pixmap(ui->toolButton->iconSize()), QIcon::Active, QIcon::On ); ui->toolButton->setIcon(icon); QIcon::setThemeName("feather-light"); // This line won't change my icon style. } MainWindow::~MainWindow() { delete ui; }
It certainly works, but there is still one issue. Since I am using a QPixmap with this method I am unable to set the icon theme after that. If I attempt to change the icon theme like above it won't effect my icon style.
-
@Saviz Unfortunately, once you change the theme you also need to change the individually added pixmaps one by one again.
addPixmap
tells the icon to use exactly this pixmap – no matter what.Our software has a function
updateToolbars()
which is called when dark mode is changed (or the widget is moved to a monitor with a different resolution). This will just rebuild all icons in the toolbar. I don't think there is a shortcut. -
@SimonSchroeder Thank you so much. This was the most honest answer I ever seen in this forum up until now. I appreciate it.
-