0

I want an image to be shown in the same window if I click on a button. But the image can't be found, no matter if it is in the same directory or I write down the whole path. I saw several ways to integrate a picture and thought this was the best one:

MyApp::MyApp(QWidget *parent)
    : QWidget(parent)
{
    QPushButton *button = new QPushButton("&Randomize!");
    gitterlayout->addWidget(button, 5, 0);

    connect(button, SIGNAL(clicked()), this, SLOT(Randomizer()));
}

void MyApp::Randomizer()
{

    QLabel* bild = new QLabel;
    QPixmap pixmap("question.png"); // the important part, the picture is in my project folder
    if(!pixmap.isNull())
    {
        bild->setPixmap(pixmap);
        bild->show();
    }
    else
    {
        qDebug() << "Cannot find picture"; // I always get this message
    }

    /* I want this question image to appear because once you click on the button, 
       a text will be copied to the clipboard and you can't see it until you paste it 
       (I left it out for this topic) */
}

If I use QPixmap pixmap("C:/myproject/question.png"); (the full path) It doesn't work as well.

Wolf
  • 9,679
  • 7
  • 62
  • 108
GoYoshi
  • 253
  • 1
  • 3
  • 17

1 Answers1

1

Because Qt uses the build folder("C:\\build-myproject-5.4.2-debug" in this case) as the working folder by default( build-myProject-5.4.2-release if you run under the release mode). You can change it by swithing to Projects mode and balabala. Or just put your pic into that working folder. On windows, the path delimiter should be '\' rather than '/', which usually causes a lot of troubles.

All your resources should be put here when running through Qt creator.

Zen
  • 5,065
  • 8
  • 29
  • 49
  • The delimiter causes no problem since every version of Windows has accepted either slash. – stark Jul 18 '15 at 12:40
  • I put my image in the folder "build-myproject-Desktop_Qt_5_4_0_MinGW_32bit-Debug" and in my cpp i wrote: QPixmap pixmap("../build-myproject-Desktop_Qt_5_4_0_MinGW_32bit-Debug/question.png"); But it still doesn't work. If I use '\' I won't be able to go a directory back :/ – GoYoshi Jul 18 '15 at 12:46
  • I moved and renamed my build folder so I don't have to go a directory back. It's now called "build" and it's located in my project folder. QPixmap pixmap("build/question.png"); -> cannot find picture QPixmap pixmap("build\question.png"); -> unknown escape sequence: '\q' QPixmap pixmap("C:/myproject/build/question.png"); -> cannot find picture QPixmap pixmap("C:\myproject\build\question.png"); -> even more unknown escape sequences – GoYoshi Jul 18 '15 at 13:02
  • 1
    At uses \ to escape so you need to use \\ in strings. – stark Jul 18 '15 at 13:11
  • Okay with that I don't get this message anymore, but still my picture can not be found. If I put it in the "debug" folder (in my "build" folder) and change the path, doesn't work as well. pixmap("build\\question.png") pixmap("build\\debug\\question.png") pixmap("C:\\myproject\\build\\question.png") pixmap("C:\\myproject\\build\\debug\\question.png") The full path doesn't change anything. – GoYoshi Jul 18 '15 at 13:29
  • Put your picture into the parent fold of the exe file if you didn't change the working directory. You can get the location from Qt creator application output. In fact, when you run an app through Qt creator, the "current" directory will be the working directory rather than directory where the exe file locates. @GoYoshi – Zen Jul 18 '15 at 14:35
  • @Zen So once I start my app for the first time or I debug it manually, there's a folder called "build-myproject-Desktop_Qt_5_4_0_MinGW_32bit-Debug". I renamed it simply to "build" and moved it to my project folder. In "build" there is a folder called "debug" with an .exe file in it. If I put my image in this folder, it won't be found as well. I think I didn't understand your solution correctly, sorry. I'm a newcomer to Qt :) – GoYoshi Jul 18 '15 at 16:38
  • I think my working directory didn't change when I moved my debug folder, lol. However, I noticed that .png doesn't work somehow. I changed the picture to jpg and it shows up in a new window. Now I just have to work on that the picture shows up in the same window as my app. Thank you! – GoYoshi Jul 19 '15 at 11:25