7

The Widget we should use to show pictures is a QLabel. we can do it directly from QtCreator, by setting its pixmap property.

we should first create a resource file and then add the image to that resource file. To create a Qt Resource File, we go to the menus: File > Qt > Qt Resource File.

we can set the image of the QLabel using Qt Creator...

but i would want to change the pic according to some input from the user

i tried to do the following :

#include "form1.h"
#include "form.h"
#include "ui_form.h"
#include "ui_form1.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    QPixmap * mypix = new QPixmap(":/karim/test.png");
    ui->label->setPixmap(mypix);
    delete mypix;
}

but i got this error

..\Project\form.cpp: In constructor 'Form::Form(QWidget*)':

..\Project\form.cpp:12: error: no matching function for call to 'QLabel::setPixmap(QPixmap*&)'

c:\QtSDK\Simulator\Qt\mingw\include/QtGui/qlabel.h:123: note: candidates are: void QLabel::setPixmap(const QPixmap&)

what could be the problem ?

ulidtko
  • 14,740
  • 10
  • 56
  • 88
karim
  • 217
  • 3
  • 5
  • 14

1 Answers1

16

The signature of the method you are trying to use is

setPixmap ( const QPixmap & )

but you are passing in a pointer. Try using a value instead.

QPixmap mypix (":/karim/test.png");
ui->label->setPixmap(mypix);
O.C.
  • 6,711
  • 1
  • 25
  • 26
  • Shouldn't QObjects be created on the heap anyway? – Tim Sep 28 '11 at 05:32
  • @OrcunC When I update the image with another image, the previous image doesn't clear. Any idea how to clear the pixmap before setting new image? – Dewsworld Mar 31 '12 at 04:28
  • Dewsworld Im having the same issue...It looks like the parent widget is not updating...If you fixed it, please post =) – fredcrs May 03 '12 at 19:14
  • OK, heres the answer: http://stackoverflow.com/questions/5653114/display-image-in-qt-to-fit-label-size thanks – fredcrs May 03 '12 at 19:20
  • `ui->label->setPixmap(QPixmap(":/karim/test.png"));` is enough. Run qMake before to make sure your resources are correctly added to the compile steps. – C-- Aug 25 '17 at 13:30