Asynchronous drawing problems
-
I have one thread that reads image data from an external API. This image data I want to draw in an Qt application. I have created a widget to draw the image. The thread calls MyWidget::setPicture. However I get lots of strange X server errors, so I'm guessing something is not threads safe. Any tips on how to do this?
@
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
~MyWidget();QPixmap pixmap; void setPicture(QImage);
protected:
void paintEvent(QPaintEvent *event);};
@@
void MyWidget::paintEvent(QPaintEvent */event/) {
QPainter painter(this);
painter.drawPixmap(this->rect(),pixmap);
}void MyWidget::setPicture(QImage i){
pixmap=QPixmap::fromImage(i);
update();
qApp->processEvents();
}@
-
Why do you call @qApp->processEvents();@? And can you show code block where thread calls setPicture
-
You have to call the QWidget methods inside the main thread. I think that is your problem. If MyWidget::setPicture is a slot, you can call it by QObject::invokeMethod(...Qt::QueuedConnection) to call from your thread to the main thread.