如何使另一个线程拥有的对象的 canvas 无效?
How do I invalidate canvas of an object owned by another thread?
我想在单独的线程中加载数据,而不是调用 redraw
方法使 canvas 无效并绘制数据。但是,当我调用 redraw
方法时,它会触发断点并显示以下消息:
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by different thread..."
我正在寻找可用于在正确线程中调用函数的某种分派方法。
Qt5 的正确方法是什么?
总体思路是将信号从工作线程发送回主线程,然后触发重绘。像这样的东西(我还没有测试过):
class Worker : public QObject
{
Q_OBJECT
public:
...
void loadData()
{
... // Do stuff
emit dataLoaded();
}
}
class MainWindow : public QObject
{
Q_OBJECT
public:
MainWindow()
{
Worker *worker = new Worker;
worker->moveToThread(workerThread);
connect(&workerThread, &QThread::started, worker, &Worker::loadData);
connect(worker, &Worker::dataLoaded, this, &MainWindow::redraw);
workerThread.start();
}
public slots:
void redraw()
{
// Do your redrawing here
}
private:
QThread workerThread;
}
我想在单独的线程中加载数据,而不是调用 redraw
方法使 canvas 无效并绘制数据。但是,当我调用 redraw
方法时,它会触发断点并显示以下消息:
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by different thread..."
我正在寻找可用于在正确线程中调用函数的某种分派方法。
Qt5 的正确方法是什么?
总体思路是将信号从工作线程发送回主线程,然后触发重绘。像这样的东西(我还没有测试过):
class Worker : public QObject
{
Q_OBJECT
public:
...
void loadData()
{
... // Do stuff
emit dataLoaded();
}
}
class MainWindow : public QObject
{
Q_OBJECT
public:
MainWindow()
{
Worker *worker = new Worker;
worker->moveToThread(workerThread);
connect(&workerThread, &QThread::started, worker, &Worker::loadData);
connect(worker, &Worker::dataLoaded, this, &MainWindow::redraw);
workerThread.start();
}
public slots:
void redraw()
{
// Do your redrawing here
}
private:
QThread workerThread;
}