QThread不';工作不好
这是QTread的子对象。。。 在主螺纹中浇注混凝土 运行时错误如下所示: QCoreApplication::sendEvent中的断言失败:“无法将事件发送到 对象由其他线程拥有。当前线程176f0a8。接收器 ''(类型为'MainWindow')是在线程3976a0中创建的,文件 c:\ndk\u buildrepos\qt desktop\src\corelib\kernel\qcoreapplication.cpp, 第405行传递给C运行时函数的参数无效。无效的 参数传递给C运行时函数 这是大型机的构造函数:QThread不';工作不好,qt,qthread,Qt,Qthread,这是QTread的子对象。。。 在主螺纹中浇注混凝土 运行时错误如下所示: QCoreApplication::sendEvent中的断言失败:“无法将事件发送到 对象由其他线程拥有。当前线程176f0a8。接收器 ''(类型为'MainWindow')是在线程3976a0中创建的,文件 c:\ndk\u buildrepos\qt desktop\src\corelib\kernel\qcoreapplication.cpp, 第405行传递给C运行时函数的参数无效。无效的 参数传递给C运行时
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
tankPoint = new QRect(50, 50, 30, 30);
this->show();
PaintThread * pt = new PaintThread(this);
pt->start();
}
以下是主窗口的覆盖绘画事件
void paintEvent(QPaintEvent*) {
QPainter p(this);
p.setPen(Qt::red);
p.setBrush(Qt::red);
p.drawEllipse(*tankPoint);
tankPoint->setLeft(200);
}
有人能告诉我为什么吗?父线程(在本例中是主窗口)位于不同的线程中。根据Qt文档
您可以随时使用线程安全函数QCoreApplication::postEvent()将事件手动发布到任何线程中的任何对象。事件将由创建对象的线程的事件循环自动调度。
所有线程都支持事件筛选器,但限制是监视对象必须与监视对象位于同一线程中。类似地,QCoreApplication::sendEvent()(与postEvent()不同)只能用于将事件分派到调用函数的线程中的对象
因此,作为解决方案,我将提出以下建议:
- 在PaintThread类中定义一个信号
- 将此信号连接到QWidget子类中的paint()插槽
- 在run()函数中发出它
void paintEvent(QPaintEvent*) {
QPainter p(this);
p.setPen(Qt::red);
p.setBrush(Qt::red);
p.drawEllipse(*tankPoint);
tankPoint->setLeft(200);
}