Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Dredvard
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers
Accepted Solution

ERROR called from non-UI thread QThread

I'm trying to concurrently load some image using QtConcurrent:map and I've recevied this error.

 

Warning: ApplicationPrivate::context: ERROR called from non-UI thread QThread(0xf9ff68, name = "Thread (pooled)")
Fatal: ApplicationPrivate::context: Method called from non-UI thread

 

I've simplified the coded to the following:

 

 

imagescaler::queuescaling{
..
    mimageScaling = new QFutureWatcher<bb::cascades::Image>(this); mimageScaling->setFuture(QtConcurrent::mapped(mImageList, scaledImage)); .. } Image scaledImage(QString file_name) { QImage qImage; bb::ImageData imageData(bb::PixelFormat::RGBA_Premultiplied, qImage.width(), qImage.height()); // causes error return imageData; }

Does anyone know what is the issue or  how to get around it?

---
If you find my post helpful please "like" it and "accept as a solution"
Please use plain text.
Developer
djogon
Posts: 93
Registered: ‎10-29-2012
My Device: Z10
My Carrier: Various

Re: ERROR called from non-UI thread QThread

Not clear what exactly is going on, but obviously there is some UI based object being processed inside your "scaledImage() function. That function will be called from a non UI thread and trying to access any UI objects from there might what's going on.

 

I am not sure what the exact hierarchy of bb::ImageData class, but it is part ot cascades so it might be somehow connected to a UI based parent class.

 

In addition - you are returning bb::ImageData, but your function signature calls for "Image" return parameter - not sure if that's just a typo in your post though.

 

Hope this gives you some ideas.

Please use plain text.
Developer
Dredvard
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers

Re: ERROR called from non-UI thread QThread

Thanks for point out the bb:ImageData.  Never noticed the mismatch.  Image converts ImageData on initialization so it doesn't create an an error.  However if I return an ImageData instead of the Image it doesn't crash.  I then just convert ImageData to Image in the UI thread and it works fine.

 

I'm curious why I can return a blank image,and  I can return an imageData, but I can't return an Image thats been converted from ImageData.  Don't quite understand what can or cannot be done outside the UI thread.

---
If you find my post helpful please "like" it and "accept as a solution"
Please use plain text.
Developer
djogon
Posts: 93
Registered: ‎10-29-2012
My Device: Z10
My Carrier: Various

Re: ERROR called from non-UI thread QThread

ImageData seems to be a simple data container while Image is a Resource. Resources are asynchronous in nature. See the following excerpt from the Resource class.

 

All resources go through several states during loading. To listen to the state changes and get other data from the resource, a ResourceTracker must be used (for example, an ImageTracker is used when listening for state changes on an Image). The ResourceTracker encapsulates the asynchronous behavior of a resource by providing means to receive signals and access resource data and states.

 As you can see - changing a state of an Image may not be synchronous and one must use signals and slots along with ImageTracker class (descendant of ResourceTracker) to get notified when data is really ready. This is because you might be loading your data from a network for example and do not know when the data will be loaded.

 

This asynchronous behaviour of an Image object may be the source of trouble here.

 

Having said all this - I noticed that after you construct your QImage object - you are not initializeing it in any way.

the default constructor for QImage creates a NULL image. A NULL image has no data associated with it and therefore accessing the parameters of it may give you an error.

 

You should create an image with certain size and format instead. Then you have to call the fill() function to initialize the data. Here is a warning from the QImage constructor that take width, height and format information.

 

Warning: This will create a QImage with uninitialized data. Call fill() to fill the image with an appropriate pixel value before drawing onto it with QPainter.

 

So I guess it must be one of these two things - happy hacking!

 

Please use plain text.
Developer
Dredvard
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers

Re: ERROR called from non-UI thread QThread

[ Edited ]

Its probably related to the first - the asynchronous nature. In my real code the Qimage actually is loaded from a file. I just simplified the code to identify what logic must be causing the error.  I had already tried using 1,1 instead of qImage.height() with no difference. Thanks for the input.

---
If you find my post helpful please "like" it and "accept as a solution"
Please use plain text.