Joshua LI
Joshua LI

Reputation: 4733

Images Disappear in Running Status on Qt

  1. Original design :

    enter image description here

  2. Setting in property :

    enter image description here

  3. When the program is running :

    enter image description here

All the image inside the project folder "QuickRecorder/Images/MainWindow". How to solve this problem?

Thank you for your help.

Upvotes: 1

Views: 334

Answers (1)

hyde
hyde

Reputation: 62797

In cases like this the reason almost always is: You use relative path to the image, and the working directory is different when you run the application, and image is not found by the relative path.

To debug, add this to your main to print current working directory:

qDebug() << QDir::currentPath();

A few solutions:

  • Use absolute paths (preferably so that you construct them at runtime, for example using QCoreApplication::applicationDirPath(), instead of hard-coding).
  • Put images to Qt resources, so they are embedded to the executable.
  • Change working directory after application starts (might have unintended consequences if you for example launch child processes, or with file open/save dialogs).

Untested, moved from comment to answer: To automatically copy files from the source dir to the build dir, you could add a build step "Custom Process Step" in the Qt Creator project settings. The command you might want to use for the case of this question might be (again, untested):

cp -rv %{sourceDir}/QuickRecorder %{buildDir}

enter image description here

Upvotes: 1

Related Questions