image not getting refreshed in Qlabel
I am trying to refresh a image in a QLabel.It is getting refreshed when i used this code on a button click:
Code:
void Dialog_user::on_btn_Stop_clicked()
{
flag=0;
sleep(2);
ui
->label
->setPixmap
(QPixmap("abc.bmp"));
}
When i am doing the same thing via a signal-slot mechanism,it is not getting refreshed.The code is :
Code:
void Dialog_user::ImageRefreshSlot()
{
qDebug()<<"inside slot";
sleep(2);
qDebug()<<"after sleep";
ui
->label
->setPixmap
(QPixmap("abc.bmp"));
}
I have verified that signal and slot is connected via a debug message. The message "inside slot" and "after sleep" is successfully printed,but the label is not refreshed again.
But again if i am clicking the button,the image is getting refreshed.
P.S.:
I have a image processing application,where the image changes every 2 secs.
Re: image not getting refreshed in Qlabel
What is that sleep(2) for? It is usually a very bad idea to block the UI thread, especially for several seconds.
Cheers,
_
Re: image not getting refreshed in Qlabel
It is a heavy image processing application,so it takes around 2 secs to process the image after a trigger is called by signal slot mechanism.
Hence I wait for 2 seconds and then display the image.
In Detail, I have a mutithreaded application,where in one thread some image processing is going and when the processing starts,I emit a signal which is caught by the ImageRefreshSlot and since the processing takes 2 seconds,I make it wait for 2 seconds.
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
It is a heavy image processing application,so it takes around 2 secs to process the image after a trigger is called by signal slot mechanism.
Hence I wait for 2 seconds and then display the image.
In Detail, I have a mutithreaded application,where in one thread some image processing is going and when the processing starts,I emit a signal which is caught by the ImageRefreshSlot and since the processing takes 2 seconds,I make it wait for 2 seconds.
Use QTimer::singleShot() instead of sleep and see if it changes pixmap or not.
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
In Detail, I have a mutithreaded application,where in one thread some image processing is going and when the processing starts,I emit a signal which is caught by the ImageRefreshSlot and since the processing takes 2 seconds,I make it wait for 2 seconds.
Then you have your synchronisation points wrong. It really doesn't matter for the main thread when the processing thread has started working, the only thing that matters is when it has finished. Because at that point you have a new image to display.
Cheers,
_
Re: image not getting refreshed in Qlabel
I have now removed the sleep and emitted a signal just after image has been formed.But still the image is not getting refreshed.
Here is what I am doing:
//GrabThread.cpp
Code:
void GrabThread::run()
{
ImageFormation();
check=0;
emit ImageRefreshSignal();
}
Image processing finishes in ImageFormation() and after that I emit a signal 'ImageRefreshSignal', which I am catching in a dialog as follows:
//dialog_user.cpp
Code:
Dialog_user
::Dialog_user(QWidget *parent
) : ui(new Ui::Dialog_user)
{
ui->setupUi(this);
ui
->label
->setPixmap
(QPixmap("abc.bmp"));
}
Dialog_user::~Dialog_user()
{
delete ui;
}
void Dialog_user::ImageRefreshSlot()
{
qDebug()<<"inside slot";
ui
->label
->setPixmap
(QPixmap("abc.bmp"));
}
I am successfully getting "inside slot" message,which indicates that ImageRefreshSlot() has been called,but still the image is not getting refreshed.
Have connected the signal-slot in main.cpp as follows:
Code:
int main(int argc, char *argv[])
{
MainWindow win;
win.show();
Dialog_user d_user;
GrabThread gthread;
QObject::connect( & gthread,
SIGNAL( ImageRefreshSignal
() ),
& d_user,
SLOT( ImageRefreshSlot
() ) );
gthread.start();
return a.exec();
}
Re: image not getting refreshed in Qlabel
Are you sure the image on disk has changed?
Any reason you do not just transfer the image within the application?
Cheers,
_
Re: image not getting refreshed in Qlabel
yes,the image on disk has changed.
In fact,I also have a button to check the same code.On button click the code works:
Code:
void Dialog_user::on_btn_Stop_clicked()
{
ui
->label
->setPixmap
(QPixmap("abc.bmp"));
}
I am not transferring the image within application because I am unable to refresh the image.It takes the same image as it took during the build time,even though the image on disk changes.
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
yes,the image on disk has changed.
In fact,I also have a button to check the same code.On button click the code works:
Hmm. In your other slot check the file timestamp using QFileInfo before loading and verify that it has indeed already been changed then.
Quote:
Originally Posted by
prkhr4u
I am not transferring the image within application because I am unable to refresh the image.It takes the same image as it took during the build time,even though the image on disk changes.
I meant: why doesn't your signal contain the image as a parameter?
Cheers,
_
Re: image not getting refreshed in Qlabel
Quote:
I am successfully getting "inside slot" message,which indicates that ImageRefreshSlot() has been called,but still the image is not getting refresh
Is your image processing thread closing the file (or at least flushing it) so the contents are physically written to the disk? If the thread simply keeps the file open while continuously updating it, the changes might simply be in the disk cache and not actually in the file itself.
Also, if your app doesn't return to the event loop after the "image finished" signal is handled, the UI won't update. Handling the button click *does* return the app to the event loop, so the pixmap *will* be updated.
And who is this "atathamquin" who keeps thanking every post, no matter what it says? Seems to be a few of these types running around the forum lately.
Re: image not getting refreshed in Qlabel
@anda_skoa: I have checked the file physically and also its timestamp also.It is indeed changing everytime.
Also I am not passing image as a parameter through my signal because I need to read the file again and again and hence I am doing it on a physical file and i know its location,so no need to pass.
@d_stranz: yes,the image processing thread is closing the file and the contents are physically written to the disk.
I think you are correct,that it is not returning to the event loop after "image finished" signal is handled..hence the UI is not updating.
Pl tell how to return to the event loop,as I am unable to do so..
Re: image not getting refreshed in Qlabel
You are using a relative path to the file. It seems quite possible you are writing to one file called abc.bmp and reading from another.
Re: image not getting refreshed in Qlabel
There is only 1 file abc.bmp.
I have verified it as I am initializing the label with it and also using external button_click event to run the same code and it is working correctly.
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
Also I am not passing image as a parameter through my signal because I need to read the file again and again and hence I am doing it on a physical file and i know its location,so no need to pass.
I am afraid I can't follow. It really doesn't matter how the file is being accessed, does it?
Your worker thread manipulates a QImage, right? It saves the data to file after it is done, right? Why not just also emit the image via signal for displaying?
That avoids any race condition on file access, avoids unnecessary disk I/O, avoid unnecessary image decoding.
Cheers,
_
Re: image not getting refreshed in Qlabel
My worker thread manipulates a bitmap image(stored on file) and I am using QPixmap to display it inside a label.
I emit the signal just after the thread finishes manipulating that bitmap image.
Hence i need to read the file everytime and for that I am using a slot mechanism to reset the image in the label everytime a signal is emmited.
Here is the code inside the slot:
Code:
ui
->label
->setPixmap
(QPixmap("abc.bmp"));
Re: image not getting refreshed in Qlabel
Yes but why do you need disk access ? Worker can process the image and send the processed result directly to the display thread, what's wrong with that :confused:
now you have this:
Code:
[load image from disk] --> process it --> [save to disk] --> inform gui that image is updated --> [load from disk] --> display
why not have this:
Code:
[load image from disk] --> process it --> inform gui that image is updated, send the image via signal --> display
:confused:
Re: image not getting refreshed in Qlabel
Can you pl tell how to pass image via signal,I am unable to do so.
Re: image not getting refreshed in Qlabel
You add QImage as a parameter of the signal
Code:
{
Q_OBJECT
signals:
void ImageRefreshSignal
(const QImage &image
);
};
And you pass the image at emit
Code:
emit ImageRefreshSignal(image); // or image.copy() if the thread continues to use "image"
Similar signature changes to slot and connect obviously
Cheers,
_
Re: image not getting refreshed in Qlabel
I have done the changes and now am sending the image via signal,but still I am unable to refresh the image.
Here is what I have done:
Code:
{
Q_OBJECT
private:
void run();
public:
signals:
void ImageRefreshSignal
(const QImage &image
);
};
Signal emmited like:
Code:
img.load("abc.bmp");
emit ImageRefreshSignal(img);
slot method called like:
Code:
void Dialog_user
::ImageRefreshSlot(QImage img
) {
qDebug()<<"inside slot";
ui
->label
->setPixmap
(QPixmap::fromImage(img
));
}
//dialog_user.h
Code:
{
Q_OBJECT
public:
explicit Dialog_user
(QWidget *parent
= 0);
~Dialog_user();
private slots:
void ImageRefreshSlot
(QImage img
);
the connection is as follows:
Code:
QObject::connect( & gthread,
SIGNAL( ImageRefreshSignal
(QImage) ),
& d_user,
SLOT( ImageRefreshSlot
(QImage) ) );
I have checked the mechanism, and the message "inside slot" is being printed,but the image is not getting refreshed.
Re: image not getting refreshed in Qlabel
Why do you load the image before sending it ? Can't you send the processed object ? Can we see the image processing code ?