Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change icon when QToolButton is pressed
QtWS25 Last Chance

Change icon when QToolButton is pressed

Scheduled Pinned Locked Moved Solved General and Desktop
qiconqtoolbuttonqiconmode
5 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saviz
    wrote on 5 Feb 2023, 03:50 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Saviz
      5 Feb 2023, 18:49

      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.

      S Offline
      S Offline
      SimonSchroeder
      wrote on 7 Feb 2023, 08:20 last edited by
      #4

      @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.

      S 1 Reply Last reply 8 Feb 2023, 16:44
      2
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 5 Feb 2023, 09:06 last edited by
        #2

        QIcon can contain difffent images for different states: QIcon::addPixmap()

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • S Offline
          S Offline
          Saviz
          wrote on 5 Feb 2023, 18:49 last edited by
          #3

          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.

          S 1 Reply Last reply 7 Feb 2023, 08:20
          0
          • S Saviz
            5 Feb 2023, 18:49

            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.

            S Offline
            S Offline
            SimonSchroeder
            wrote on 7 Feb 2023, 08:20 last edited by
            #4

            @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.

            S 1 Reply Last reply 8 Feb 2023, 16:44
            2
            • S SimonSchroeder
              7 Feb 2023, 08:20

              @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.

              S Offline
              S Offline
              Saviz
              wrote on 8 Feb 2023, 16:44 last edited by
              #5

              @SimonSchroeder Thank you so much. This was the most honest answer I ever seen in this forum up until now. I appreciate it.

              1 Reply Last reply
              0
              • S Saviz has marked this topic as solved on 8 Feb 2023, 16:44

              3/5

              5 Feb 2023, 18:49

              • Login

              • Login or register to search.
              3 out of 5
              • First post
                3/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved