Important: Please read the Qt Code of Conduct -

How to specify images from different directories than the application startup directory

  • Hello,

    I am looking for a way to specify images in styles that will be read in from the current directory, and not from the directory the application starts.
    Here is part of a style I currently use:
    @QScrollBar::down-arrow:vertical {
    width: 9px;
    height: 10px;
    background: transparent;
    image: url(./scr_down_arrow.png);

    Is there a way to modify the url(...) so that it will load from the current directory ?


  • You might be able to do this by setting the applications working directory to the current directory, see QDir::setCurrent(), so paths gets resolved relative to the current directory.

  • Yes, I thought of this, and the application is started with a command like this:
    The current directory is not where the application is located, but Qt seems to always use the startup directory for picking up resource images from the filesystem. However, I'll check again in the code to make sure where the application thinks it is on startup.

  • At the C++ level, QT::currentPath() indeed returns the current directory, however, this does not solve the problem of the Qt style module always using the directory where the binary is located. I was looking for a quick solution, something that I may have missed in the manuals describing style syntax. Someone might ask 'why aren't you packing everything in the resources', well, the objective is to have different, and possibly user-supplied resources in different directories, so when the application is started from one of them, the resources in the current directory will be used. If it turns out that this is not supported at all, then I'll need to revise this part of the design entirely.

  • Is there something wrong with this approach:
    QString styles = QString(
    "QScrollBar::down-arrow:vertical { ...; image: url(%1/scr_down_arrow.png); }"
    "QScrollBar::up-arrow:vertical { ...; image: url(%1/scr_up_arrow.png); }"

  • Thank you, all. I finally used a variation of the approach suggested by ChrisW67 namely, adding a marker at the beginning of the path for each image and then replacing them all with the appropriate path just before applying the style.

Log in to reply