Solved 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 ?? -
@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); }
-
@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)); -
-
Make sure your label or it's parent widget is visible.
-
Make sure QPixmap object is not NULL.
-
Try whether plain text appears on QLabel.
-
-
-
QPixmap m_logo_pic; QLabel *m_pic_label; m_pic_label=new QLabel(); m_logo_pic.load(":/image/bank.png"); m_pic_label->setPixmap(m_logo_pic); ////Place the labe in the layout
-
@jsulm you are right bro , now it works ,thx :))
-
-
@joeQ I've just added the drive letter to my code and it works ! the solution you gave me works as well, thx a lot :D
-
Don't forget that using such a hardcoded value won't make your application portable.