0

I created a window and a picture in label. It' working when i give a localization of a whole file, but i can't let it be like that. I should leave it like this:

ui->setupUi(this);
ui->label_10->setPixmap(QPixmap("C:/Users/JDober/Desktop/UnknownPrinter.jpg"));

And it should be something like that:

 ui->setupUi(this);   
 ui->label_10->setPixmap(QPixmap("UnknownPrinter.jpg"));

But this doesn't work. Ofc i put this file in same folder as a project is. Please help.

Jay Dober
  • 1
  • 2
  • Where is the file located relative to the CWD (Current Working Directory - see also `getcwd()`) when the program is running? – Jesper Juhl Jun 03 '18 at 14:07
  • Im not really sure if i understood u good. Whole project (Project3.pro) and other files (.cpp and .h) are located in same folder as image is. – Jay Dober Jun 03 '18 at 14:10
  • @Jay Dober project directory is not *necessarily* the runtime/working directory when you run the program. – Jesper Juhl Jun 03 '18 at 14:23

1 Answers1

2

You have a few options. One is to specify the path relative to your application's directory, by utilising QCoreApplication::applicationDirPath, or some other well-known path.

Another option is to embed the image directly in your executable using Qt's resource system and rcc compiler, and then use the image from resources instead of directly from disk.

An example of this would be adding a file resources.qrc to your project, wiht the following contents:

<!DOCTYPE RCC>
<RCC version="1.0">
  <qresource>
    <file>UnknownPrinter.jpg/file>
  </qresource>
</RCC>

If you're using QMake, add a line to your .pro file to register the resource file:

RESOURCES = resources.qrc

You can then use the image resource in code like this:

ui->setupUi(this);
ui->label_10->setPixmap(QPixmap(":/UnknownPrinter.jpg"));
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • In .pro i've added this code: `resources.qrc` under this: `RESOURCES += \` and changed other lines as u said. I have an error now. Tried to clean, rebuild etc. Nothing works. `:-1: error : [debug/qrc_resources.cpp] Error 1` – Jay Dober Jun 03 '18 at 15:11
  • @JayDober Which program is giving the error? RCC? The compiler (which one are you using, BTW)? Something else? And is that the *full* text of the error? – Angew is no longer proud of SO Jun 03 '18 at 15:16
  • I don't really understand your questions. Im using Qt creator with qmake. Program puts this error in "Problems". Yes, this is the full text of the error. – Jay Dober Jun 03 '18 at 15:28
  • 1
    @JayDober Qt Creator is an IDE (a glorified text editor). QMake is a build driver, which launches the individual tools (compiler, linker, UIC, MOC, RCC) which do the actual build. Which one of these is giving the error? If Qt Creator is not giving you more access, you may have to launch your build using a command-line once, to get at the actual errors. – Angew is no longer proud of SO Jun 03 '18 at 15:31
  • Wait. You asked for this? `mingw32-make[1]: *** [debug/qrc_resources.cpp] Error 1` – Jay Dober Jun 03 '18 at 15:41
  • @JayDober Getting closer, but this seems to be just the final line of `make` reporting the error. Before that, there's got to be some error output (probably from the compiler, likely gcc in your case) which caused the build to fail. `make` is then reporting that the build failed, which is the line you're showing. But it's the originating error that contains information which can be used to resolve it. – Angew is no longer proud of SO Jun 03 '18 at 16:36
  • So what i need to do now? – Jay Dober Jun 03 '18 at 18:43
  • @JayDober Get the complete text of all the error messages would be a start. Run your build from the command line if you can't get Qt Creator to show it all to you. I don't use Qt Creator, so I can't help here. – Angew is no longer proud of SO Jun 03 '18 at 19:52