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. How to load an SVG icon in QIcon with currentColor support?
Qt 6.11 is out! See what's new in the release blog

How to load an SVG icon in QIcon with currentColor support?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.6k 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.
  • K Offline
    K Offline
    Kevin Hoang
    wrote on last edited by
    #1

    I'm trying to load an SVG icon into QIcon while supporting currentColor. The SVG file contains elements using fill="currentColor", and I want it to take the color from the color property of the parent widget (e.g., QPushButton).

    Here’s my current approach:

    QIcon icon(":/icons/my_icon.svg");
    button->setIcon(icon);
    button->setStyleSheet("QPushButton { color: red; }");
    

    However, the icon remains black instead of inheriting the button's text color.

    Is there a built-in method in Qt to support this behavior, or do I have to manually render the SVG with the correct color before setting it in QIcon?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QtSvg does not support this yet. There is also a bug report about this.
      You can modify the svg file on disk for example.

      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
      0
      • K Offline
        K Offline
        Kevin Hoang
        wrote on last edited by
        #3

        It's unfortunate that QtSvg doesn't support currentColor yet. Do you happen to have a link to the bug report? I'd like to track its progress.
        For now, modifying the SVG file on disk could be an option, but I was hoping for a more dynamic solution.

        Thanks!

        1 Reply Last reply
        0

        • Login

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