PDA

View Full Version : Image Not displaying in Qlabel



ranjithrajrrr
25th July 2016, 18:20
Hi guys..I am trying to display a series of image in qlabel..i dono where i am going wrong. when i click the qpushbutton it displays only the last image rather displaying from beginning. Below is my code

mainwindow.cpp


#include "mainwindow.h"
QLabel *FirstLabel = NULL;
QLabel *SecondLabel = NULL;
QLabel *ThirdLabel = NULL;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{

FirstLabel = new QLabel(this);
FirstLabel->move(0,240);
FirstLabel->resize(this->x()+400,this->y()+240);
FirstLabel->show();

QPushButton *train_button = new QPushButton(this);
train_button->setText(tr("something"));
train_button->move(200,200);
train_button->show();

connect(train_button, SIGNAL(clicked()),
this, SLOT (PlayImage()));
}

void MainWindow::PlayImage()
{
char* picture[5] = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};
for(int i = 0;i<=4 ; i++){
QPixmap pixmapObject(picture[i]);
FirstLabel->setPixmap(pixmapObject);
update();
}
}

MainWindow::~MainWindow()
{
}


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <string>
#include <QPushButton>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void updateinfo();
void Play();

public slots:
void PlayImage();
};

#endif // MAINWINDOW_H

main.cpp


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(800,480);
w.show();
return a.exec();
}

Tarko1976
26th July 2016, 08:25
put in your mainwindow.h:


#include <QPushButton> //delete this line
class QPushButton;
class QLabel;


then declare private section:


private:
QLabel *FirstLabel;
QLabel *SecondLabel;
QLabel *ThirdLabel;


in mainwidow.cpp


FirstLabel = new QLabel(this);
FirstLabel->move(0,240);
FirstLabel->resize(this->x()+400,this->y()+240);
FirstLabel->show();

train_button = new QPushButton(this);
train_button->setText(tr("something"));
train_button->move(200,200);
train_button->show();

connect(train_button, SIGNAL(clicked()),
this, SLOT (PlayImage()));


void MainWindow::PlayImage()
{
//char* picture[5] = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};
QStringList picture = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};
for(int i = 0;i<=picture.count() ; i++){
QPixmap pixmapObject(picture[i]);
FirstLabel->setPixmap(pixmapObject);
update(); //What is it?
}
}

anda_skoa
26th July 2016, 11:27
Hi guys..I am trying to display a series of image in qlabel..i dono where i am going wrong. when i click the qpushbutton it displays only the last image rather displaying from beginning.

You have a close loop in which you "display" the images.

So what your code does is:
- it loops through your array of image file names
- each iteration loads the image, sets it on the label and ask for update
- when the loop ends the event loop processed the update requests and the label displays the image that is now set

What you probably want to do is to use a QTimer or QTimeLine to advance through the array.
One image at a time, giving the event loop the chance to actually process the label's update request and the user to actually see the image.

Cheers,
_

ChrisW67
26th July 2016, 11:34
This:

char* picture[5] = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};

should probably be:


char* picture[5] = {"D:/1.png","D:/2.jpg","D:/3.jpg","D:/4.jpg","D:/5.jpg"};

to be sure the double forward-slashes are not causing files to not be found.

ranjithrajrrr
26th July 2016, 11:44
Thank you for your reply. I read that update function in qt refreshed the label every time it iterates.By the way i found what was the mistake i was doing. I need to call repaint function( FirstLabel->repaint ) instead of update function. with that change it works fine.

anda_skoa
26th July 2016, 11:51
Thank you for your reply. I read that update function in qt refreshed the label every time it iterates

Your update() call is actually for the main window itself, the label will call its own update() method in setPixmap() anyway.



By the way i found what was the mistake i was doing. I need to call repaint function( FirstLabel->repaint ) instead of update function. with that change it works fine.
It is still wrong, but of course your choice.

Cheers,
_

ranjithrajrrr
27th July 2016, 12:05
Thanks for ur reply anda_skoa. Please explain me why repaint is a bad idea.. it does the same thing as update() fucntion does r8t?

anda_skoa
27th July 2016, 12:38
update() schedules an update of a widget, multiple calls to update within one iteration of the event loop will only cause one update of the widget instead of unnecessarily for every update.

repaint forces a redraw that might not even be seen because it is immediately followed by another redraw or paint-for-update.

Your code's problem is that you loop through images without ever allowing any image to be shown.

I seriously wonder how even repaint would help here since the repaints for each image are immediately after each other.
The user will only see the last image unless each drawing is so slow that there is a tiny chance of glimpsing one of the others in between.

Actual image sequence displaying usually doesn't rely on slowness, it specifies in which interval images are shown.
Then triggers a new image to be shown whenever that happens.

Cheers,
_

ranjithrajrrr
28th July 2016, 07:41
understood anda.. Thanks for your help..