Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically setting the pixmap of a QLabel in Qt

Tags:

c++

qt

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 ?

like image 539
karim Avatar asked Aug 02 '11 14:08

karim


1 Answers

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);
like image 141
O.C. Avatar answered Oct 08 '22 22:10

O.C.