Image Not displaying in Qlabel
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
Code:
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
FirstLabel
= new QLabel(this);
FirstLabel->move(0,240);
FirstLabel->resize(this->x()+400,this->y()+240);
FirstLabel->show();
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++){
FirstLabel->setPixmap(pixmapObject);
update();
}
}
MainWindow::~MainWindow()
{
}
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <string>
#include <QPushButton>
{
Q_OBJECT
public:
~MainWindow();
void updateinfo();
void Play();
public slots:
void PlayImage();
};
#endif // MAINWINDOW_H
main.cpp
Code:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.resize(800,480);
w.show();
return a.exec();
}
Re: Image Not displaying in Qlabel
put in your mainwindow.h:
Code:
#include <QPushButton> //delete this line
then declare private section:
in mainwidow.cpp
Code:
FirstLabel
= new QLabel(this);
FirstLabel->move(0,240);
FirstLabel->resize(this->x()+400,this->y()+240);
FirstLabel->show();
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++){
FirstLabel->setPixmap(pixmapObject);
update(); //What is it?
}
}
Re: Image Not displaying in Qlabel
Quote:
Originally Posted by
ranjithrajrrr
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,
_
Re: Image Not displaying in Qlabel
This:
Code:
char* picture[5] = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};
should probably be:
Code:
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.
Re: Image Not displaying in Qlabel
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.
Re: Image Not displaying in Qlabel
Quote:
Originally Posted by
ranjithrajrrr
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.
Quote:
Originally Posted by
ranjithrajrrr
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,
_
Re: Image Not displaying in Qlabel
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?
Re: Image Not displaying in Qlabel
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,
_
Re: Image Not displaying in Qlabel
understood anda.. Thanks for your help..