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. QMenu: Mac style inverts selected item's text, but not its icon

QMenu: Mac style inverts selected item's text, but not its icon

Scheduled Pinned Locked Moved Unsolved General and Desktop
menusvgstyleselected itemaction
6 Posts 3 Posters 199 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.
  • B Offline
    B Offline
    barbicels
    wrote on 29 Mar 2025, 03:13 last edited by
    #1

    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 a QIcon that has a different SVG file added for the QIcon::Selected mode, but that appears to have no effect.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zvoopz
      wrote on 29 Mar 2025, 07:16 last edited by
      #2

      Invert colors of an icon if selected
      example for button icon

      void 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
      }
      
      
      B 1 Reply Last reply 29 Mar 2025, 16:09
      0
      • Z zvoopz
        29 Mar 2025, 07:16

        Invert colors of an icon if selected
        example for button icon

        void 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
        }
        
        
        B Offline
        B Offline
        barbicels
        wrote on 29 Mar 2025, 16:09 last edited by barbicels
        #3

        @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.
        0189da61-4622-4380-a41d-56a522f38ec7-image.png f6bb0958-669f-4332-adcd-7db8870d3dff-image.png
        This ought to be easy, considering that a QIcon can have multiple modes including Selected, 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 same QAction object shows a grayed-out icon when it's added to a QToolBar.)

        Considering that we have to force the application's Qt::AA_DontShowIconsInMenus attribute to false even to see icons in menu items, Qt's answer might be that what I want is beyond consideration.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          IgKh
          wrote on 29 Mar 2025, 18:02 last edited by
          #4

          On Cocoa QMenu objects are thin wrappers of AppKit NSMenu objects. You can get access to the underlying NSMenu Objective-C object with QMenu::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...

          B 1 Reply Last reply 29 Mar 2025, 18:31
          0
          • I IgKh
            29 Mar 2025, 18:02

            On Cocoa QMenu objects are thin wrappers of AppKit NSMenu objects. You can get access to the underlying NSMenu Objective-C object with QMenu::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...

            B Offline
            B Offline
            barbicels
            wrote on 29 Mar 2025, 18:31 last edited by
            #5

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

            1 Reply Last reply
            0
            • B Offline
              B Offline
              barbicels
              wrote on 29 Mar 2025, 18:35 last edited by
              #6

              One data point about Apple's preference: Safari's Bookmarks menu uses a "globe" icon for pages that lack their own icon, and that monochrome globe icon does switch between light and dark when selected.

              1 Reply Last reply
              0

              5/6

              29 Mar 2025, 18:31

              • Login

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