On Windows 7 64 bit using Qt 4.8.4 I try to defer the updating of a pretty big QGraphicsScene to a thread, so that the main app can immediately continue. I want the drawing to take place in the background.
I wrote a little test app to reproduce the problem. The test app also produces a crash, but a different one from the main app. The test app crashes on application exit, the message says something about sending signals to objects belonging to a different thread. In my real application it happens on scene->clear(). I hope the two effects are related, so what’s wrong in the following code (Just remove the two slashes in front of “#define crash’):
File mainwin.h:
class mainWin : public QMainWindow {
Q_OBJECT
public:
mainWin(QWidget *parent = 0) : QMainWindow(parent) {
QWidget *widget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(widget);
QPushButton *btn = new QPushButton("Redraw");
view = new QGraphicsView(widget);
scene = new QGraphicsScene(this);
layout->addWidget(view);
layout->addWidget(btn);
scene->addText("Test");
view->setScene(scene);
setCentralWidget(widget);
connect(btn, SIGNAL(clicked()), this, SLOT(redraw()));
countClicks = 0;
};
~mainWin() {};
virtual void doRedraw() {
scene->clear();
scene->addText("Test " + QString::number(countClicks++));
};
public slots:
virtual void redraw() {
//#define crash
#ifdef crash
QtConcurrent::run(this, &mainWin::doRedraw);
#else
doRedraw();
#endif
};
private:
QGraphicsView *view;
QGraphicsScene *scene;
int countClicks;
};
File main.cpp:
#include "mainwin.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mainWin *w = new mainWin;
w->show();
a.exec();
}
↧