Page 1 of 3 123 LastLast
Results 1 to 20 of 41

Thread: image not getting refreshed in Qlabel

  1. #1
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. void Dialog_user::on_btn_Stop_clicked()
    2. {
    3. flag=0;
    4. sleep(2);
    5. ui->label->setPixmap(QPixmap("abc.bmp"));
    6. }
    To copy to clipboard, switch view to plain text mode 

    When i am doing the same thing via a signal-slot mechanism,it is not getting refreshed.The code is :
    Qt Code:
    1. void Dialog_user::ImageRefreshSlot()
    2. {
    3. qDebug()<<"inside slot";
    4. sleep(2);
    5. qDebug()<<"after sleep";
    6. ui->label->setPixmap(QPixmap("abc.bmp"));
    7. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  4. #4
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: image not getting refreshed in Qlabel

    Quote Originally Posted by prkhr4u View Post
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: image not getting refreshed in Qlabel

    Quote Originally Posted by prkhr4u View Post
    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,
    _

  6. The following 3 users say thank you to anda_skoa for this useful post:

    prkhr4u (4th December 2013)

  7. #6
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. void GrabThread::run()
    2. {
    3. ImageFormation();
    4. check=0;
    5. emit ImageRefreshSignal();
    6. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. Dialog_user::Dialog_user(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::Dialog_user)
    4. {
    5. ui->setupUi(this);
    6. ui->label->setPixmap(QPixmap("abc.bmp"));
    7. }
    8. Dialog_user::~Dialog_user()
    9. {
    10. delete ui;
    11. }
    12. void Dialog_user::ImageRefreshSlot()
    13. {
    14. qDebug()<<"inside slot";
    15. ui->label->setPixmap(QPixmap("abc.bmp"));
    16. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow win;
    5. win.show();
    6. Dialog_user d_user;
    7. GrabThread gthread;
    8. QObject::connect( & gthread, SIGNAL( ImageRefreshSignal() ),& d_user, SLOT( ImageRefreshSlot() ) );
    9. gthread.start();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  9. #8
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. void Dialog_user::on_btn_Stop_clicked()
    2. {
    3. ui->label->setPixmap(QPixmap("abc.bmp"));
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: image not getting refreshed in Qlabel

    Quote Originally Posted by prkhr4u View Post
    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 View Post
    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,
    _

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: image not getting refreshed in Qlabel

    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.

  12. #11
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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..

  13. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  14. #13
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  15. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: image not getting refreshed in Qlabel

    Quote Originally Posted by prkhr4u View Post
    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,
    _

  16. #15
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. ui->label->setPixmap(QPixmap("abc.bmp"));
    To copy to clipboard, switch view to plain text mode 

  17. #16
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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
    now you have this:
    Qt Code:
    1. [load image from disk] --> process it --> [save to disk] --> inform gui that image is updated --> [load from disk] --> display
    To copy to clipboard, switch view to plain text mode 
    why not have this:
    Qt Code:
    1. [load image from disk] --> process it --> inform gui that image is updated, send the image via signal --> display
    To copy to clipboard, switch view to plain text mode 

  18. The following 2 users say thank you to stampede for this useful post:

    prkhr4u (9th December 2013)

  19. #17
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: image not getting refreshed in Qlabel

    Can you pl tell how to pass image via signal,I am unable to do so.

  20. #18
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: image not getting refreshed in Qlabel

    You add QImage as a parameter of the signal

    Qt Code:
    1. class GrabThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. signals:
    6. void ImageRefreshSignal(const QImage &image);
    7. };
    To copy to clipboard, switch view to plain text mode 
    And you pass the image at emit
    Qt Code:
    1. emit ImageRefreshSignal(image); // or image.copy() if the thread continues to use "image"
    To copy to clipboard, switch view to plain text mode 

    Similar signature changes to slot and connect obviously

    Cheers,
    _

  21. The following 2 users say thank you to anda_skoa for this useful post:

    prkhr4u (9th December 2013)

  22. #19
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. class GrabThread : public QThread
    2. {
    3. Q_OBJECT
    4. private:
    5. void run();
    6. public:
    7. signals:
    8. void ImageRefreshSignal(const QImage &image);
    9. };
    To copy to clipboard, switch view to plain text mode 
    Signal emmited like:
    Qt Code:
    1. QImage img;
    2. img.load("abc.bmp");
    3. emit ImageRefreshSignal(img);
    To copy to clipboard, switch view to plain text mode 
    slot method called like:
    Qt Code:
    1. void Dialog_user::ImageRefreshSlot(QImage img )
    2. {
    3. qDebug()<<"inside slot";
    4. ui->label->setPixmap(QPixmap::fromImage(img));
    5. }
    To copy to clipboard, switch view to plain text mode 
    //dialog_user.h
    Qt Code:
    1. class Dialog_user : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Dialog_user(QWidget *parent = 0);
    7. ~Dialog_user();
    8.  
    9. private slots:
    10. void ImageRefreshSlot (QImage img);
    To copy to clipboard, switch view to plain text mode 
    the connection is as follows:
    Qt Code:
    1. QObject::connect( & gthread, SIGNAL( ImageRefreshSignal(QImage) ),& d_user, SLOT( ImageRefreshSlot(QImage) ) );
    To copy to clipboard, switch view to plain text mode 
    I have checked the mechanism, and the message "inside slot" is being printed,but the image is not getting refreshed.

  23. #20
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default 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 ?

Similar Threads

  1. Replies: 8
    Last Post: 6th November 2013, 04:36
  2. Checkboxes in Treeview do not get refreshed
    By mesch.t in forum Newbie
    Replies: 5
    Last Post: 13th April 2011, 21:53
  3. QTableView is not refreshed
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2009, 22:23
  4. Replies: 6
    Last Post: 21st September 2009, 11:55
  5. QLabel as an image.
    By RSX in forum Newbie
    Replies: 2
    Last Post: 4th April 2009, 20:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.