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. QSvgRenderer using an external CSS file
Forum Updated to NodeBB v4.3 + New Features

QSvgRenderer using an external CSS file

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 551 Views 1 Watching
  • 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
    KSierens
    wrote on last edited by
    #1

    Can someone please give me an example of using a SVG file having an external CSS file to control the colors for fill and stroke?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      The SVG support in Qt meets a subset of SVG Tiny specification. SVG Tiny only supports a subset of CSS styling and also states, "Authors must not rely on external, author stylesheets to style documents that are intended to be used with SVG Tiny 1.2 user agents."

      What are you trying to achieve, and what exactly is failing?

      1 Reply Last reply
      2
      • K Offline
        K Offline
        KSierens
        wrote on last edited by KSierens
        #3

        Depending on whether the UI is styled in a light or dark theme, we want to render parts of the SVG file in either black or white. It is my hope to override the stroke colors in an external CSS file.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KSierens
          wrote on last edited by
          #4

          Is there anyway to conditionally use properties in a SVG file?

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KSierens
            wrote on last edited by
            #5

            As to what is failing, I just have a simple SVG image in which I am trying to override the color used for the stoke color. I have tried many things, and I can not get anything to work from the CSS.

            That is why I was asking for a very simple example of getting CSS to work from SVG.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KSierens
              wrote on last edited by
              #6

              Also, "currentColor" is not working in the SVG file when using QSvgRenderer. I set the pen color in the painter before calling render, but it does not use it.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #7

                It seem to me that currentColor is designed to inherit from the CSS color property in the HTML environment in which the SVG is embedded. You do not have such an environment here.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  If there's no better way, you can read svg file to a QByteArray, change the stroke color to what you want, then load the modified QByteArray to QSvgRenderer.

                  1 Reply Last reply
                  2
                  • K Offline
                    K Offline
                    KSierens
                    wrote on last edited by KSierens
                    #9

                    Thank you all. I was able to figure out how to do this. I guess part of my problem is not understanding that when using a CSS with an SVG, the styling in the CSS will not override the values for the SVG element, just use it if it was not defined.

                    For example, if you have a 'path' element, and there is already a stroke color defined as in:

                    <path stroke="yellow" stroke-width="6" d="M 100,60 140,30 150,50 160,30 170,80" />

                    The following CSS value will not replace "yellow" with "red"

                    path {
                    stroke: red;
                    }

                    JonBJ 1 Reply Last reply
                    0
                    • K KSierens

                      Thank you all. I was able to figure out how to do this. I guess part of my problem is not understanding that when using a CSS with an SVG, the styling in the CSS will not override the values for the SVG element, just use it if it was not defined.

                      For example, if you have a 'path' element, and there is already a stroke color defined as in:

                      <path stroke="yellow" stroke-width="6" d="M 100,60 140,30 150,50 160,30 170,80" />

                      The following CSS value will not replace "yellow" with "red"

                      path {
                      stroke: red;
                      }

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @KSierens
                      But that is true for all CSS on any HTML element: any property set explicitly on any element overrides any rule in CSS, which is only used for defaults if nothing explicitly, and always has been like that.

                      Full CSS in HTML allows you to use the !important directive in a CSS rule (stroke: red !important;) and that would actually override even matching style explicitly placed on an element. But Qt's CSS is a subset of full CSS and does not support !important, so you can't do that.

                      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