Show picture using QLabel and Pixmap
-
I'm beginner in Qt and I couldn't display a picture using QPixmap and QLabel.
The code I wrote is :
QLabel *img = new QLabel(tab);
img->setPixmap(QPixmap(":/Users/Learner/Desktop/photo.png"));
tab is a pointer to the parent widget.
I don't know what's wrong with this code , there is no compilation error but , the picture is not displayed . Is there anyone who can help me ?? -
I'm beginner in Qt and I couldn't display a picture using QPixmap and QLabel.
The code I wrote is :
QLabel *img = new QLabel(tab);
img->setPixmap(QPixmap(":/Users/Learner/Desktop/photo.png"));
tab is a pointer to the parent widget.
I don't know what's wrong with this code , there is no compilation error but , the picture is not displayed . Is there anyone who can help me ??@Fedi said in Show picture using QLabel and Pixmap:
:/Users/Learner/Desktop/photo.png
Is your image in a resource file or are you trying to load a file from the file system? If file system then you're missing the drive letter.
-
I'm beginner in Qt and I couldn't display a picture using QPixmap and QLabel.
The code I wrote is :
QLabel *img = new QLabel(tab);
img->setPixmap(QPixmap(":/Users/Learner/Desktop/photo.png"));
tab is a pointer to the parent widget.
I don't know what's wrong with this code , there is no compilation error but , the picture is not displayed . Is there anyone who can help me ??@Fedi Hi, friend, welcome.
You can reference code snippet below.
QString filename = "xxxxx"; QLabel* lbl = new QLabel(this); /** set content to show center in label */ lbl->setAlignment(Qt::AlignCenter); QPixmap pix; /** to check wether load ok */ if(pix.load(filename)){ /** scale pixmap to fit in label'size and keep ratio of pixmap */ pix = pix.scaled(lbl->size(),Qt::KeepAspectRatio); lbl->setPixmap(pix); }
-
@Fedi ,
Your pixmap is not scaled.
QPixmap pix(":/Users/Learner/Desktop/photo.png");
int w = img->width ();
int h = img->height ();
img->setPixmap (pix.scaled (w,h,Qt::KeepAspectRatio)); -
I'm beginner in Qt and I couldn't display a picture using QPixmap and QLabel.
The code I wrote is :
QLabel *img = new QLabel(tab);
img->setPixmap(QPixmap(":/Users/Learner/Desktop/photo.png"));
tab is a pointer to the parent widget.
I don't know what's wrong with this code , there is no compilation error but , the picture is not displayed . Is there anyone who can help me ?? -
-
@Fedi said in Show picture using QLabel and Pixmap:
:/Users/Learner/Desktop/photo.png
Is your image in a resource file or are you trying to load a file from the file system? If file system then you're missing the drive letter.
-
-
@Fedi Hi, friend, welcome.
You can reference code snippet below.
QString filename = "xxxxx"; QLabel* lbl = new QLabel(this); /** set content to show center in label */ lbl->setAlignment(Qt::AlignCenter); QPixmap pix; /** to check wether load ok */ if(pix.load(filename)){ /** scale pixmap to fit in label'size and keep ratio of pixmap */ pix = pix.scaled(lbl->size(),Qt::KeepAspectRatio); lbl->setPixmap(pix); }
-
Don't forget that using such a hardcoded value won't make your application portable.