C++ Qt错误-无法将事件发送到其他线程所拥有的对象,该线程已通过鼠标追踪更正
我正在使用C++ Qt错误-无法将事件发送到其他线程所拥有的对象,该线程已通过鼠标追踪更正,c++,multithreading,qt,C++,Multithreading,Qt,我正在使用图形视图框架编程。我使用从QGraphicsView继承的自定义类来显示qgraphicscene。在主线程中新建qgraphicscene,在另一个线程中新建QGraphicsItem。当我调用scene.addItem()——这里QT将向场景发出信号,这是QT不允许的。但是如果我在我的QGraphicsView中写入setMouseTracking(true),这将纠正QT的错误。为什么? 我的代码: //centralWidget.cpp pGraphicsScene_ = ne
图形视图框架
编程。我使用从QGraphicsView
继承的自定义类来显示qgraphicscene
。在主线程中新建qgraphicscene
,在另一个线程中新建QGraphicsItem
。当我调用scene.addItem()
——这里QT将向场景发出信号,这是QT不允许的。但是如果我在我的QGraphicsView
中写入setMouseTracking(true)
,这将纠正QT的错误。为什么?
我的代码:
//centralWidget.cpp
pGraphicsScene_ = new QGraphicsScene(this);
pMonitorView_ = new MonitorView(pGraphicsScene_);
//monitorView.cpp
MonitorView::MonitorView(QGraphicsScene *scene, QWidget *parent):
QGraphicsView(scene, parent)
{
setMouseTracking(true);//If I comment this line,will get error--for this I don't confuse but write this statement will correct error!
}
//another thread start by std::thread
pItem = new GoodsItem();
pGraphicsScene_->addItem(pItem);
请显示您的代码。Qt不支持在不同线程中使用GUI对象。这不是答案,但您可能希望使用
QThread
,而不是使用std::thread
,因为它是为更好地处理Qt信号而设计的。除此之外,您的实际错误是什么?@thuga您可能误解了我的意思,您可能需要阅读我的标题。@GertWollny这应该是标题错误。但是当我添加setMouseTracking(true)时代码>,没有错误。