Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 41

Thread: image not getting refreshed in Qlabel

  1. #21
    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 a separate image processing thread,which processes the image object as well as writes it to a bmp file "abc.bmp"
    The image processing is entirely different from GUI thread and works continuously in the background.
    As the GUI thread is only meant for display purposes,I am not sending the processed object for writing in GUI thread.

    Is there other way to send the image via a signal without loading it?

  2. #22
    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

    I have a separate image processing thread,which processes the image object as well as writes it to a bmp file "abc.bmp"
    The image processing is entirely different from GUI thread and works continuously in the background.
    As the GUI thread is only meant for display purposes,I am not sending the processed object for writing in GUI thread.
    Yes we know that ! Show us the code where you do this "abc.bmp" file writing in image processing thread. We want you to just emit the changed image apart from writing it to file. You have some image object on which you call save() method, right ? Just emit a signal, passing this object as parameter...

  3. #23
    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

    Here is the main code for writing:
    Qt Code:
    1. void convert2bmp(unsigned int **Buffer_in,unsigned int x_size,unsigned int y_size,char *output_str)
    2. unsigned char* data3d = new unsigned char[x_size*y_size];
    3. fp = fopen (output_str, "wb");
    4. for ( i = x_size; i >= 0; i--)
    5. for (j = 0; j < y_size; j++)
    6. {
    7. data3d[k++]=Buffer_in[i][j];
    8. }
    9. fwrite( data3d, sizeof( char ),x_size*y_size, fp);
    10. fclose(fp);
    To copy to clipboard, switch view to plain text mode 

    Buffer_in: buffer containing the processed image data
    x_size,y_size: dimension of image
    Output_str: name of output file:"abc.bmp"

    After some image processing,which occurs on the buffer,I am writing it to a bitmap file.The Buffer_in is only available to me inside this(image processing thread) and then I am displaying the image in a Qlabel in GUI thread.
    The method "convert2bmp" is being called after every image processing iteration and the processed image needs to be displayed on a GUI thread everytime.
    But the image is not getting refreshed every time.

  4. #24
    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

    The variable k is used uninitialised.

    Just from looking at what you are doing to the image data I think it is exceptionally unlikely you have a valid BMP at the end of the process.
    Ignoring your GUI for a moment. Is the BMP file being written? Is it the correct size in bytes? Is it a valid BMP and can you see the right image in Microsoft Paint or something similar?

  5. #25
    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

    The bmp file is being written properly.
    I have excluded the code for bmp header and small variables like i,j,k.
    The code for main file writing is being enclosed.

  6. #26
    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

    If Buffer_in is your pixel data, you can probably use it to create a QImage instance pointing to the same image data.

    And then use QImage::copy() to send it through the signal.

    Cheers,
    _

  7. #27
    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 sent the pixel data using QImage as follows:
    Qt Code:
    1. QImage myImage(400,700,QImage::Format_RGB888);
    2. for (int x=0;x<400;x++)
    3. for (int y=0;y<700;y++)
    4. myImage.setPixel(x,y,qRgb(Buffer_in[y][x*3],Buffer_in[y][x*3+1],Buffer_in[y][x*3+2]));
    5. emit ImageRefreshSignal(myImage.copy(0,0,400,700));
    To copy to clipboard, switch view to plain text mode 

    and used the slot as follows:
    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 

    still the label containg the image is not getting refreshed.
    The slot is being called correctly and the debug message "Inside slot" is being printed.

  8. #28
    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
    I have sent the pixel data using QImage as follows:
    Qt Code:
    1. QImage myImage(400,700,QImage::Format_RGB888);
    2. for (int x=0;x<400;x++)
    3. for (int y=0;y<700;y++)
    4. myImage.setPixel(x,y,qRgb(Buffer_in[y][x*3],Buffer_in[y][x*3+1],Buffer_in[y][x*3+2]));
    5. emit ImageRefreshSignal(myImage.copy(0,0,400,700));
    To copy to clipboard, switch view to plain text mode 
    In this case (QImage having its own memory) you can do without the copy().

    Quote Originally Posted by prkhr4u View Post
    still the label containg the image is not getting refreshed.
    The slot is being called correctly and the debug message "Inside slot" is being printed.
    Very strange.
    Only thing left I can think of is something blocking the UI thread, like some code calling sleep() or blocking API.

    If you don't have any of those, can you try to create a minimal test case? I.e. something where the thread just does some random pixel value changes?

    Cheers,
    _

  9. #29
    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 created a test case and changed some pixel values.
    I was able to successfully change the pixel values,when I was doing it in MainWindow.
    But If I do the same in another dialog,The pixel values are not even set and QLabel remains as such.
    I checked in the same way,the debug message "inside slot" is being printed for the dialog.

    I have some login code,which I want to put in Main Window and then open a new dialog(on successful login),where I show the processed image in a Qlabel.

  10. #30
    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
    I created a test case and changed some pixel values.
    Can you attach that test case? The idea of a minimal test case it that others can try it and look for potential errors.

    Cheers,
    _

  11. #31
    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 attached the minimal test case.
    Do have a look at the same
    Attached Files Attached Files

  12. #32
    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

    Your code works nicely (after uncommenting the dialog's show() call of course, can display something that is hidden)

    The image thread emits the image and the dialog it is connected to gets the slot invocation and displays the image.

    I modified the thread's run() method it randomly use a different color and indeed those changes are visible in the dialog connected to the thread.

    Cheers,
    _

  13. #33
    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 know that this way it is working,but I want to show the dialog only after clicking the button "go to dialog" from main window.In that case it is not working.
    I suspect this is due to the event loop not closing,but I am unable to close it and hence the label is not working.

    Similarly there is some login code in main window,after clicking login button,the dialog opens up and I want to show the image in the label.The label is then unable to set the image at that moment.
    I have tried with both QImage and bitmap image.In both the cases,the QLabel is unable to show the image.

  14. #34
    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
    I know that this way it is working,but I want to show the dialog only after clicking the button "go to dialog" from main window.In that case it is not working.
    I don't see why this would not also work.

    Why didn't you put that into the testcase?

    Right now your code does exactly what it is supposed to do. The thread emits the image, the label in the connected dialog is updated.
    There is a button which opens another dialog and it also shows its label content as expected.
    The event loop is nicely running. If the thread is modified to emit images in a loop, the label in the dialog it is connected to will change every time there is a new image.

    Cheers,
    _

  15. #35
    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 had commented the dialog.show() in my test case.It Is not working as expected.
    Comment out the line dialog.show() and then:
    When I click on the button in the main window,the dialog opens up and the label shows "Text label" and the image is not shown at all.

    If I am showing the dialog at the start( uncommenting the dialog.show()),then it is working.But I want the dialog to be open only after clicking the button.

  16. #36
    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 never connect the signal from the thread to the dialog you create in the MainWindow::on_btn_dialog_clicked(). Why would you expect it to update?
    (The way your code is written the thread object is not available to be connected in this routine anyway.)
    Last edited by ChrisW67; 23rd December 2013 at 04:33.

  17. #37
    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
    I had commented the dialog.show() in my test case.It Is not working as expected.
    It is. I tested it.

    Quote Originally Posted by prkhr4u View Post
    Comment out the line dialog.show() and then:
    When I click on the button in the main window,the dialog opens up
    No, a second dialog opens up.

    Quote Originally Posted by prkhr4u View Post
    and the label shows "Text label" and the image is not shown at all.
    As expected, that is what its code, generated from the designer UI file, is doing.

    Quote Originally Posted by prkhr4u View Post
    If I am showing the dialog at the start( uncommenting the dialog.show()),then it is working.
    No, time of showing has nothing to do with that.

    You have two places were you create dialog instances. One in main() and one in the main window's slot.

    The instance from main() is connected to the thread and updates its label when ever there is a new image. Any instance created from the main window's slot has no interaction after it has been created and thus shows whatever you had in its UI file.


    Quote Originally Posted by prkhr4u View Post
    But I want the dialog to be open only after clicking the button.
    Then you need to connect the button to the dialog' show() slot, or use a slot that calls show() on the dialog.

    Cheers,
    _

  18. #38
    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 connected the signal to slot in the main.cpp file.
    Here is the code:
    Qt Code:
    1. QObject::connect( & iThread, SIGNAL( ImageRefreshSignal(QImage) ),& dlg, SLOT( ImageRefreshSlot(QImage) ) );
    To copy to clipboard, switch view to plain text mode 
    I have checked it also,as inside the slot method the debug message "inside slot" is being printed.

    Even calling the connect method inside the MainWindow:: on_btn_dialog_clicked() does the same thing.

  19. #39
    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
    I have connected the signal to slot in the main.cpp file.
    Here is the code:
    Qt Code:
    1. QObject::connect( & iThread, SIGNAL( ImageRefreshSignal(QImage) ),& dlg, SLOT( ImageRefreshSlot(QImage) ) );
    To copy to clipboard, switch view to plain text mode 
    Yes, and it works as expected. The dialog shows all updates it gets from the thread.

    Quote Originally Posted by high_flyer View Post
    I have checked it also,as inside the slot method the debug message "inside slot" is being printed.
    Indeed it does.

    Quote Originally Posted by high_flyer View Post
    Even calling the connect method inside the MainWindow:: on_btn_dialog_clicked() does the same thing.
    Exactly! Connecting these other dialogs to the thread will make them update their label as well.

    Cheers,
    _

  20. #40
    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

    Here is the sequence of events I want:

    load main window,start image processing thread -> click on button 'go to dialog' -> open a new dialog -> refresh image(QImage from thread via signal-slot) in Qlabel in new dialog

    Here is what is happening:

    load main window,start image processing thread -> click on button 'go to dialog' -> open a new dialog -> Qlabel showing only 'Text Label' and image is not loaded.

    Only the dialogs which start from main can interact with the thread and can update themselves with the thread,but In my case the dialog is opening from main window and is not able to interact with the thread.
    pl suggest how to overcome this problem?
    Is there any design changes I have to do?

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.