QT QImage Read From Error
-
Why do you need a thread for this at all?
Simply create a new QImage object instead using a local / member var would be a first step. -
@mvsri said in QT QImage Read From Error:
@JonB
it get re-entered and re-load the same image variable.Then I'm thinking that is not a good idea?
Start by looking at @Christian-Ehrlicher's advice.
-
@JonB @Christian-Ehrlicher reason for using thread was the entire code snippet will be in a while loop, continuously grabbing image. and main GUI has other functions on it.
and also i have tried without using qthread but there was no real time image capturing happening there (i also used QTimer). -
@mvsri
Just for now: instead of a singleimage
declaration somewhere, in your code try:QImage *image = new QImage; bool result = image->load(image_path); ... emit sendImage(*image); // or, with corresponding change to signal/slot parameter emit sendImage(image);
I know this will leak the
image
! Assuming it's not too big, and you don't do it too often, so that memory is not an issue, does that make a difference to the "black" you see? -
@JonB No need to create it with new, a local instance on the stack will work fine
-
@Christian-Ehrlicher
I don't see that as safe? If the stack variable in the thread goes out of scope while the signal has beenemit()
ed passing the image for the main UI thread to use, won't that release the image while it is still needed by the main thread? -
No need for a mutex, please post code with stack variable.
will be in a while loop, continuously grabbing image. and main GUI has other functions on it.
So you say the image reading take too long?
-
@Christian-Ehrlicher
This is the codewhile(true) { bool check = doesFilexExist(image_path); if(check){ QImage image; bool result = image.load(image_path); QThread::msleep(20); if(!image.isNull()){ qDebug() << "==> Timestamp for image grabbing: " << QTime::currentTime(); emit sendImage(image); QFile::remove(image_path); QThread::msleep(50); } } else { // Do nothing } }
@Christian-Ehrlicher said in QT QImage Read From Error:
So you say the image reading take too long?
I'm not so sure about that.
The logic is very simple :
1. Check if image exists in folder.
2.if exists, load image
3.emit the image
4. delete image from the folderNote: 3rd party camera stores a single image in that folder
-
@mvsri So are you sure the camera does not override the image while you try to load it? Please try without the camera with a static image.
-
@mvsri Have you thought about using QFileSystemWatcher to listen to changes in the folder,?
No need to reinvent the wheel, you can simply react to the
directoryChanged
signal
If @Christian-Ehrlicher is right, and the camera overwrites the image, than the fileChanged signal will be much handier to use.
-
@Christian-Ehrlicher
I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly -
@J-Hilk said in QT QImage Read From Error:
@mvsri Have you thought about using QFileSystemWatcher to listen to changes in the folder,?
I haven't thought about that will try to use it now.
-
@mvsri said in QT QImage Read From Error:
I think the camera does override the image when i try to load it because when i tested it with static image the code worked perfectly
So you've your explanation of the issue - Qt can't do anything against this.
-
@Christian-Ehrlicher
yeah i got that, but the thing is if you see the code,
after reading the image i'm deleting the image from the folder, so the chances for image override is very less -
@mvsri Maybe try to rename it before you try to read it.
-
@mvsri
I really don't understand what is going on! What is it that produces thatimage_path
file? Something external? How do you know that whatever is creating it has finished writing the file and flushed it before your thread decides to read it in, such that it might read in something incomplete? :confused: -
@JonB said in QT QImage Read From Error:
@mvsri
I really don't understand what is going on! What is it that produces thatimage_path
file? Something external? How do you know that whatever is creating it has finished writing the file and flushed it before your thread decides to read it in, such that it might read in something incomplete? :confused:image_path is nothing but a QString which stores the path to a bmp image in a folder. the path is static it doesn't change.
I don't know whether the image writing is finished or not. that's why i used QFile exists to check if the file is created or not and read the image if the path exists.
I also have the same doubt whether i'm reading the image in an incomplete mode.@Christian-Ehrlicher said in QT QImage Read From Error:
@mvsri Maybe try to rename it before you try to read it.
I'm trying it now.
Thank you everyone for all the help. will try to update if i found something useful.