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?
Re: image not getting refreshed in Qlabel
Quote:
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...
Re: image not getting refreshed in Qlabel
Here is the main code for writing:
Code:
void convert2bmp(unsigned int **Buffer_in,unsigned int x_size,unsigned int y_size,char *output_str)
unsigned char* data3d = new unsigned char[x_size*y_size];
fp = fopen (output_str, "wb");
for ( i = x_size; i >= 0; i--)
for (j = 0; j < y_size; j++)
{
data3d[k++]=Buffer_in[i][j];
}
fwrite( data3d, sizeof( char ),x_size*y_size, fp);
fclose(fp);
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.
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?
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.
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,
_
Re: image not getting refreshed in Qlabel
I have sent the pixel data using QImage as follows:
Code:
for (int x=0;x<400;x++)
for (int y=0;y<700;y++)
myImage.setPixel(x,y,qRgb(Buffer_in[y][x*3],Buffer_in[y][x*3+1],Buffer_in[y][x*3+2]));
emit ImageRefreshSignal(myImage.copy(0,0,400,700));
and used the slot as follows:
Code:
void Dialog_user
::ImageRefreshSlot(QImage img
) {
qDebug()<<"inside slot";
ui
->label
->setPixmap
(QPixmap::fromImage(img
));
}
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.
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
I have sent the pixel data using QImage as follows:
Code:
for (int x=0;x<400;x++)
for (int y=0;y<700;y++)
myImage.setPixel(x,y,qRgb(Buffer_in[y][x*3],Buffer_in[y][x*3+1],Buffer_in[y][x*3+2]));
emit ImageRefreshSignal(myImage.copy(0,0,400,700));
In this case (QImage having its own memory) you can do without the copy().
Quote:
Originally Posted by
prkhr4u
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,
_
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.
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
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,
_
1 Attachment(s)
Re: image not getting refreshed in Qlabel
I have attached the minimal test case.
Do have a look at the same
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,
_
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.
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
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,
_
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.
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.)
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
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
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
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
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
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,
_
Re: image not getting refreshed in Qlabel
I have connected the signal to slot in the main.cpp file.
Here is the code:
Code:
QObject::connect( & iThread,
SIGNAL( ImageRefreshSignal
(QImage) ),
& dlg,
SLOT( ImageRefreshSlot
(QImage) ) );
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.
Re: image not getting refreshed in Qlabel
Quote:
Originally Posted by
prkhr4u
I have connected the signal to slot in the main.cpp file.
Here is the code:
Code:
QObject::connect( & iThread,
SIGNAL( ImageRefreshSignal
(QImage) ),
& dlg,
SLOT( ImageRefreshSlot
(QImage) ) );
Yes, and it works as expected. The dialog shows all updates it gets from the thread.
Quote:
Originally Posted by
high_flyer
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
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,
_
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?