QMenu: Mac style inverts selected item's text, but not its icon
-
The current Mac style for
QMenu
follows macOS's habit of inverting the text of the selected item (e.g., to light-gray-on-accent from black-on-light-gray), but how can I prevent my monochrome SVG icon from disappearing into the dark accent color when its menu item is selected?I have tried setting the
QAction
's icon to aQIcon
that has a different SVG file added for theQIcon::Selected
mode, but that appears to have no effect. -
Invert colors of an icon if selected
example for button iconvoid MainWindow::invertButtonIcon(QPushButton *button) { QPixmap pixmap = button->icon().pixmap(button->iconSize()); // Get icon as pixmap QImage img = pixmap.toImage(); img.invertPixels(QImage::InvertRgb); // Invert colors button->setIcon(QIcon(QPixmap::fromImage(img))); // Apply new inverted icon }
-
@zvoopz That definitely works for a case where your code has control, but I was referring to a
QMenu
that the user is picking from, where my code is not in control. To stand out from the highlight color, the menu item's icon would need to render one way when the user mouses over the item and another way when mousing off it.Currently, I've put a half-opaque "rim" on the SVGs so that the monochrome icon stands out a little bit against the highlight color. What I really need is for that (rimless) icon to switch completely from dark to light when the user mouses over, in the same way as the menu item text and shortcut do.
This ought to be easy, considering that aQIcon
can have multiple modes includingSelected
, but nothing seems to work, and I can't find any working examples online. My suspicion is that Qt's "native Mac style" doesn't inform the icon-element renderer that it should use the "selected" mode in this situation. (Notice that it doesn't "disable" the cancel icon, either, even though the sameQAction
object shows a grayed-out icon when it's added to aQToolBar
.)Considering that we have to force the application's
Qt::AA_DontShowIconsInMenus
attribute tofalse
even to see icons in menu items, Qt's answer might be that what I want is beyond consideration. -
On Cocoa
QMenu
objects are thin wrappers of AppKitNSMenu
objects. You can get access to the underlyingNSMenu
Objective-C object withQMenu::toNSMenu
and patch out the icons, probably with something like the onStateImage and offStateImage properties. You could probably write a workaround of sorts using that.That said, no native macOS application I've seen recently has icons on menu items, despite AppKit supporting them. There doesn't seem to be any wording in Apple's HIG that discourages their use, but I guess the best way to blend in is to not have them as well...
-
@IgKh I agree; my impression is that Apple disfavors the practice and Qt has adapted its default to follow that. I just think it's odd that the optional with-icons treatment is inconsistent — for example, icons on disabled menu items are dimmed if they're pixmaps but not if they're SVG.
As long as Qt intends to be a toolkit that minimizes the need for platform-specific code, I think it should try to support things like this in the name of cross-platform consistency, even if devs whose app is Mac-only should be happy with iconless menu items as you described.