tags:

views:

216

answers:

2

I am working on QT GUI project. In this application I have a QWidget as main window. I make the cursor from data coming from some source. When I set the cursor of widget. It gives me the following error. QPixmap: It is not safe to use pixmaps outside the GUI thread My code is as follows

void ImageWindow::setMouseCursor(unsigned char* data,unsigned char* maskbits,unsigned int length,int xHotSpot, int yHotSpot)

{

QBitmap bitmapData;
QBitmap bitmapMaskData;
bitmapData.loadFromData(data,length);
bitmapMaskData.loadFromData(maskbits,length);

this->setCursor(QCursor(bitmapData,bitmapMaskData,xHotSpot,yHotSpot));
this->update();

}

Function setMouseCursor is called from other class, which set the data of cursor. ImageWindow is my customized QWidget class.

+2  A: 

Apparently, the object which calls setMouseCursor lives outside the GUI thread as far as i can tell. In order to avoid this, make setMouseCursor a slot. Do not call the slot directly, instead emit a signal from the caller object, and connect that signal to setMouseCursor slot using Qt::QueuedConnection.

See : ConnectionType

erelender
+1  A: 

Two problems:

  • don't use a QBitmap outside the GUI-thread
  • don't call gui objects setCursor outside the GUI-thread

Creating a Paint Device
One advantage of using QImage as a paint device is that it is possible to guarantee the pixel exactness of any drawing operation in a platform-independent way. Another benefit is that the painting can be performed in another thread than the current GUI thread.

TimW