c++qtqpixmap

Get QPixmap dynamic image path from std::string


I have a problem with the QPixmap under Qt. I want to set the Pixmap Image Path dynamically from a std::string. I have tried some variations, but without success.

This is my code:

QPixmap pixmap("data/25eaad8879f1d018caec98546279804f.png"); // this is working
// label

ui->imageSite1->setPixmap(pixmap); //working

But if I try to set the pixmappath dynamically, I don't see the image (inside the label). e.g.

string path = "data/"+imgname+".png"; // imgname is a dynamic parameter

QPixmap pixmap(path); // this is not working

// label
ui->imageSite1->setPixmap(pixmap);

How can I do this correctly?


Solution

  • If you take a look at the Documentation of QPixmap, you'll see the load method declared as taking a QString as parameter. So you either have to build a QString directly (judging by your example, I think this is the better way) or convert the std::string to a QString with a constructor of the latter.