Question

I'm trying to display a picture on a QLabel with a QPixmap, but the image doesn't show. Can somebody help me?

onglets = new QTabWidget(this);    //création des onglets
page1 = new QWidget;  //création de la page 1
page2 = new QLabel; //création de la page 2

page1->setLayout(layoutPrincipal); //insere le layoutprincipale dans la page 1
page2->setPixmap(QPixmap("1.png"));
page2->setText("Alpha version 1.0");
onglets->addTab(page1, "Creation de compte"); //ajoute la page 1 dans les onglets
onglets->addTab(page2, "A propos");
Was it helpful?

Solution

The problem is in lines:

page2->setPixmap(QPixmap("1.png"));
page2->setText("Alpha version 1.0");

QLabel can't have both text and Pixmap options, so, when you setText, QPixmap is removed. To avoid this, you can create new QWidget for second tab of your QTabWidget, add there 2 QLabels, with 1, that have text and second - pixmap.

UPDATE: if after removing page2->setText("Alpha version 1.0"); QPixmap still is not present, try to find out, if there even exist file "1.png". Try this out, to determine, if file exists and it where you expect it to be

qDebug()<<"File exists -"<<QFileInfo("1.png").exists()<<" "<<QFileInfo("1.png").absoluteFilePath();

UPDATE2:

When you launch it from QtCreator (compile it), your file should be in your project directory (near *.pro file), when you launch it from executable, it should be near it, if you invoke it like "1.png". By the way, if you change your currentWorkingDirectory - then next problems will arise. The solution is to add qt recource file to project and add this image to recource and call it like QPixmap(:/1.png");. But for now, you can find the required directory with QDir::currentPath(), put your file there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top