0

I am programming with Graphics View Framework. I use a customized class which inherit from QGraphicsView to show QGraphicsScene. I new QGraphicsScene in main thread and new QGraphicsItem in another thread. When I call scene.addItem()--here QT will emit signal to scene and it is not allowed by QT. But if I write setMouseTracking(true) in my QGraphicsView that would correct the QT's error. Why?

My code:

//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);
Jonas
  • 6,915
  • 8
  • 35
  • 53
Crawl.W
  • 403
  • 5
  • 17
  • show your code please. – eyllanesc May 23 '17 at 05:25
  • Using GUI objects in different threads is not supported in Qt. – thuga May 23 '17 at 07:41
  • Not an answer, but instead of using `std::thread` you might want to use `QThread` since it is designed to deal better with QT signals, [see also](https://stackoverflow.com/questions/40284850/qthread-vs-stdthread). Apart from that, what is the actual error you're getting? – Gert Wollny May 23 '17 at 08:10
  • @thuga You could misunderstand what I mean, you may need read my title. – Crawl.W May 23 '17 at 08:50
  • @GertWollny This should be a error as title. But when I add `setMouseTracking(true);`, there is no error. – Crawl.W May 23 '17 at 08:55
  • I didn't misunderstand. According to your code, you are using `QGraphicsScene` object in a different thread. This is not supported by Qt. The fact that the error goes away when you add `setMouseTracking(true)` is irrelevant. You are still doing something that you shouldn't be doing. See the second paragraph of [this answer](https://stackoverflow.com/a/24764337/2257050). – thuga May 23 '17 at 09:17
  • @Crawl.W Accurding to [this](https://doc.qt.io/qt-5/threads-qobject.html) signals can be send across threads, and maybe `setMouseTracking` sets up some threading related stuff that otherwise is missing (because std::thread does no QT related things), and hence the error message does no longer appear. I'm not a QT expert, but I think "using GUI objects in different threads not supported" only applies to drawing stuff in parallel, but to get things truly right you might have to add locking, which means that with a certain setup Qt will no longer issue an error but may still crash. – Gert Wollny May 23 '17 at 09:21
  • @GertWollny No, it doesn't apply only to drawing stuff in parallel. See [this documentation](http://doc.qt.io/qt-5/threads-qobject.html#qobject-reentrancy). – thuga May 23 '17 at 09:43
  • @thuga Yes,I know. But I am confused of reason that the error goes away. – Crawl.W May 24 '17 at 00:36
  • And GertWollny is your meaning.To GertWollny, moreover, set mouse tracking and the program run very well. – Crawl.W May 24 '17 at 01:14

0 Answers0