0

I have some QActions in my QToolBar. QAction doesn't have any stylesheets, so I am trying to change the QAction icon on mouse hover. how can I change the QAction icon on mouse hover

Rishabh Bansal
  • 51
  • 1
  • 1
  • 9
  • I thought that QAction had a `hover()` signal you could connect to – infixed Jun 14 '16 at 12:43
  • Thankyou. Its working but when we remove the mouse hover from that action new icon appears (mouse hovered icon appears) how can we solve that? – Rishabh Bansal Jun 14 '16 at 13:07
  • @RishabhBansal please show the code which partially works for you. – maxik Jun 15 '16 at 08:43
  • QAction* m_pSettingsAction; m_pSettingsAction->setIcon(QIcon(":/settings.png")); connect(m_pSettingsAction,SIGNAL(triggered()),this,SLOT(OnSettingsTriggered())); (connect(m_pSettingsAction,SIGNAL(hovered()),this,SLOT(OnSettingsHovered())); void CMainWindow::OnSettingsHovered() { m_pSettingsAction->setIcon(QIcon(":/settings_active.png")); } – Rishabh Bansal Jun 15 '16 at 08:52
  • 1
    I have set active icon on mouse hover. but after removing mouse cursor(mouse hover). iT shows the active icon. but I want i should show the previous icon((QIcon(":/settings.png"))) – Rishabh Bansal Jun 15 '16 at 08:57

1 Answers1

-1

To change the icon on mouse hover you can set diferent pixmaps for diferent states of your QIcon, using setPixmap()

See here http://doc.qt.io/qt-5/qicon.html#pixmap

If your action is created in QtCreator or QtDesigner, you can change it in the properties menu at property windowIcon

I have an action in QToolBar, that changes depending on state, so i created an QIcon and a QPixmap in the constructor, like this:

MainConfigWindow::MainConfigWindow(QWidget *parent) :        
    QMainWindow(parent),
    icoDisconnected(QIcon(":/icons/connect_128.png")),
    pixmapDisconnected(QPixmap(":/icons/disconnect_128.png"))
    {
        ui->setupUi(this);
        icoConnected.addPixmap(pixmapDisconnected,QIcon::Selected,QIcon::On);
        icoConnected.addPixmap(pixmapDisconnected,QIcon::Selected,QIcon::Off);
        icoConnected.addPixmap(pixmapDisconnected,QIcon::Active,QIcon::On);
        icoConnected.addPixmap(pixmapDisconnected,QIcon::Active,QIcon::Off);
    }
Szpaqn
  • 545
  • 5
  • 17
  • Ok, but how do you trigger those different states by *hovering*? On some systems (eg Mac), `Active` only occurs when clicking. – Len May 17 '22 at 01:16