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 specify image path in Qt project?

How to specify image path in Qt project?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 995 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.
  • J Offline
    J Offline
    Jsla
    wrote on 14 Jan 2024, 13:31 last edited by
    #1

    Hello! I'm currently working on a Qt project, and I would like to specify the path to the images I have included in my project directory. Images are located in a folder called "images" at the root of the project. My intention is to make the path flexible so that the project can be shared and run from different computers without having to manually adjust the paths.

    Currently I'm using something like this in my code and it doesn't work:

    Cancelbutton->setIcon(QIcon("images/cancel.png"));

    J 1 Reply Last reply 14 Jan 2024, 14:07
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Jan 2024, 13:36 last edited by
      #2

      Hi and welcome to devnet,

      One possible simple way to achieve what you want is to use Qt's resources system. This allows to embed files within the executable.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • J Jsla
        14 Jan 2024, 13:31

        Hello! I'm currently working on a Qt project, and I would like to specify the path to the images I have included in my project directory. Images are located in a folder called "images" at the root of the project. My intention is to make the path flexible so that the project can be shared and run from different computers without having to manually adjust the paths.

        Currently I'm using something like this in my code and it doesn't work:

        Cancelbutton->setIcon(QIcon("images/cancel.png"));

        J Offline
        J Offline
        JonB
        wrote on 14 Jan 2024, 14:07 last edited by
        #3

        @Jsla
        The problem is that the "project directory" which exists while you develop simply does not "exist" at runtime when you deploy elsewhere/to other people. By which I mean there is no way to "locate" it.

        You then have 3 choices:

        • Use Qt's QStandardPaths Class to generate a full path to one of the standard directories and use that. The problem is that it's not easy to deploy your file(s) there in the first place,.

        • Use Qt's QCoreApplication::applicationDirPath() to locate where the executable lives. Use that to generate a full path to some images sub-directory from there where the images have been provided.

        • As @SGaist says use Qt's resource system to embed the images into the actual executable and access them there. This is perhaps the easiest, requires you to specify you are using resources at development time.

        1 Reply Last reply
        3

        3/3

        14 Jan 2024, 14:07

        • Login

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