Icon disappears when clicked - How can I set multiple QIcon::Modes for an icon?
-
I have a png format image of a play button in which I have stored as an embedded resource in my application.
<!DOCTYPE RCC><RCC version="1.0"> <qresource prefix="icon"> <file>play.png</file> </qresource> </RCC>
I have created a
QIcon
with the same source image set for bothNormal
andActive
modesQIcon play; play.addFile(":icon/play.png", QSize(), QIcon::Normal); play.addFile(":icon/play.png", QSize(), QIcon::Active);
From what I understand, this should display the icon when unclicked (
Normal
mode), and continue doing so when clicked (Active
mode)QIcon::Normal
: Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available.QIcon::Active
: Display the pixmap when the functionality represented by the icon is available and the user is interacting with the icon, for example, moving the mouse over it or clicking it.However, when I click on it it disappears (a blank box being displayed instead).
Here is the icon unclicked
Here is the icon clicked
Minimal working example:
I have created a minimal example replicating the behaviour I see
#include <QApplication> #include <QMainWindow> #include <QMenuBar> void initIcons() { Q_INIT_RESOURCE(view); } int main(int argc, char** argv) { QApplication* app = new QApplication(argc, argv); QMainWindow* window = new QMainWindow(); QMenuBar* menu = new QMenuBar(); window->setMenuBar(menu); QIcon play; play.addFile(":icon/play.png", QSize(), QIcon::Normal); play.addFile(":icon/play.png", QSize(), QIcon::Active); QAction* action = new QAction(play, "", nullptr); menu->addAction(action); window->show(); return app->exec(); }
Update:
I have also tried every combination of
Mode
andState
with no change in behaviour:play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::On); play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::Off); play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::On); play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::Off); play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::On); play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::Off);
I also tried
addPixmap
QIcon play; play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::On); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::Off); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::On); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::Off); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::On); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::Off);
None of these alter the behaviour at all unfortunately.
-
Hi,
AFAIK, you have to do it at the icon level. You can use the QIcon::addPixmap or QIcon::addFile method for that.
Hope it helps
-
@SGaist thanks for the suggestion.
I tried using
addFile
QIcon play; play.addFile(":icon/play.png", QSize(), QIcon::Normal); play.addFile(":icon/play.png", QSize(), QIcon::Active); QAction* action = new QAction(play, ""); menu->addAction(action);
Unfortunately the same effect occurs (icon visible when unclicked, black square when clicked)
I thought perhaps I was using the wrong
Mode
, so I triedSelected
too:play.addFile(":icon/play.png", QSize(), QIcon::Normal); play.addFile(":icon/play.png", QSize(), QIcon::Selected); play.addFile(":icon/play.png", QSize(), QIcon::Active);
I then tried tried every combination of
Mode
andState
:play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::On); play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::Off); play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::On); play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::Off); play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::On); play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::Off);
I also tried
QPixmap
QIcon play; play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::On); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::Off); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::On); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::Off); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::On); play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::Off);
None of these alter the behaviour at all unfortunately.
-
@ambershark I have created a minimal example replicating the behaviour I see
<!DOCTYPE RCC><RCC version="1.0"> <qresource prefix="icon"> <file>play.png</file> </qresource> </RCC>
#include <QApplication> #include <QMainWindow> #include <QMenuBar> void initIcons() { Q_INIT_RESOURCE(view); } int main(int argc, char** argv) { QApplication* app = new QApplication(argc, argv); QMainWindow* window = new QMainWindow(); QMenuBar* menu = new QMenuBar(); window->setMenuBar(menu); QIcon play; play.addFile(":icon/play.png", QSize(), QIcon::Normal); play.addFile(":icon/play.png", QSize(), QIcon::Active); QAction* action = new QAction(play, "", nullptr); menu->addAction(action); window->show(); return app->exec(); }
Here is the icon unclicked
Here is the icon clicked
-
@skebanga Ok so I played with it.. This seems like a bug in QMenuBar (maybe specific to linux).
Here's what I found... If you add text to your action, i.e.
new QAction(play, "play")
then when you click the play icon it will change to the text "play" as you hold the button down, highlighted with your selection color (in your case blackish gray, in mine blue).So the reason you are seeing the blank is because it is showing text only and highlighted... so when your text is "" it basically shows a highlighted blank text/space.
That feels like it shouldn't be happening. It shouldn't be changing to the text mode for the highlight, it should highlight the icon as the QToolBar does.
So you could do a few things:
- Override QMenuBar and handle that click highlight yourself.
- Switch to a QToolBar - the highlighting of icons works properly on these, see the Qt example widgets/mainwindows/application.
- Wait for a fix from Qt (assuming you report the bug and it is confirmed as a bug).
I don't see any function in QAction or QMenuBar that could change this behavior. It makes me think it was not intentional... Or I just missed the function that fixes it. ;)
-
@ambershark Thanks for giving it a try! I've submitted a bug report
-
I just realised one thing, you seem to be using Ubuntu. Are you using the Qt version from your distribution ? If not, then please try with it before opening a report. It might be related to their customisation.
-
This is one way and usually the only one to get the latest version when it comes out.
However, most Linux distribution provide Qt and its development environment because it's a widely used framework and there are usually several system tools using it. This is not only valid for KDE which is based on Qt but also for example for WireShark and other tools which front end are written using Qt.
Thus depending on your target customer/user/etc. you might also want to use your distribution provided Qt to build packages that fits in. In the Ubuntu case, IIRC, there where some customisation done to integrate with their Window Manager.
-
-
@ambershark Ok, thanks for confirming that