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. [SOLVED] Stylesheet from file - relative url paths
Forum Update on Monday, May 27th 2025

[SOLVED] Stylesheet from file - relative url paths

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 12.1k 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.
  • I Offline
    I Offline
    inspired
    wrote on 5 Dec 2012, 15:48 last edited by
    #1

    Ok, so in my application i apply a stylesheet from a file, like this:
    in main:
    @QFile file(QApplication::applicationDirPath() + "/themes/default/style.css");
    file.open(QFile::ReadOnly);
    QString StyleSheet = QLatin1String(file.readAll());
    a.setStyleSheet(StyleSheet);@

    and in the style.css, i want to use an image like this
    @image: url(down-arrow.png);@

    but the thing is that qt searches for the file "down-arrow.png" inside the folder of the exe (QApplication::applicationDirPath() )

    so i have to write
    @image: url(themes/default/down-arrow.png);@

    is there any way to use relative paths in the stylesheet file?

    or in other words, to change where qt searches for files when it parses the stylesheet

    Thank you

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hostel
      wrote on 5 Dec 2012, 23:37 last edited by
      #2

      You can in style.css write:
      @
      image: url(%down-arrow.png%);
      @
      then load stylesheet to QString and replace %down-arrow.png% to correct file. I don't know other solution.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        inspired
        wrote on 6 Dec 2012, 00:28 last edited by
        #3

        well that wouldnt help i think, cuz i give the user the abillity to set their own theme folder and so they can make their own style.css file.. (though i didnt understand how to change the %down-arrow.png% ... something with args?)

        but i figured a way when you said about QString replacement, so thank you! :)

        @QString styleSheetFromFile(QString file, QString folderForUrl)
        {
        QFile f(file);
        f.open(QFile::ReadOnly);
        QString styleSheet = QLatin1String(f.readAll());
        f.close();
        styleSheet.replace("url(","url("+folderForUrl+"/");
        return styleSheet;
        }@

        1 Reply Last reply
        0
        • U Offline
          U Offline
          ufas
          wrote on 1 Nov 2013, 10:20 last edited by
          #4

          You load the style sheet by yourself. With this, Qt has no idea where your style sheet comes from. This also means, that a relative path to the images makes no sense. Qt just looks relative to the current working directory; in your case relative to the executable.
          The solution is as simple as undocumented:
          @
          QApplication a(argc, argv);
          QString file = "/path/to/fancy.css";
          a.setStyleSheet( "file:///" + file);
          @

          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