[Qt-interest] QGraphicsView QThread

Alexis Ménard alexis.menard at trolltech.com
Tue Mar 31 19:58:27 CEST 2009


On Tuesday 31 March 2009 18:09:39 Bas Terwijn wrote:
> hi Fred, Alexis
>
> First of all, thanks for your interest.
>
> I tried the solution offered by Alexis. It seems to be stable. Instead
> of calling the QGraphicsItem:setPos() in the QThread after each change,
> i now send for each item a signal to a newly created slot of each item
> that then calls setPose() which is now done in the main thread.
>
> However, in my old test I created the objects derived from QGraphicsItem
> in the main thread, if I now create items and connect their signal in
> the QThread it is again unstable.

As soon as you don't call methods of QGraphicsItem from your other thread it 
will works. But if you want to add members, methods on top of QGraphicsItem 
that you want to modify from your thread then the standard thread way of 
programming apply, you should protect the access to your new variable. As soon 
as you protect your stuff and don't use method of QGI from your thread then it 
should work.

>
> To give some background, what i want is the following. Say i am
> developing some simulation program. For debugging purposes I would like
> to visualize and manually manipulate it. For this I thought I use Qt.
> But in the end I also want to run my simulation without any Qt
> libraries. So I would like to be able to build the simulator both with
> and without Qt, use Qt as an add-on so to say. However, Qt imposes
> architectural constraints onto my simulation program. As Qt demands
> complete control over the main thread, I have to run the simulator in a
> QThread when compiling with Qt. Creating and changing the elements to be
> visualized (object derived from QGraphicsItem) in this thread is now my
> main problem. My simulation program should not be aware if it is running
> with or without QT so i can use a simple macro that indicates the use of
> Qt or not.
>

Then i don't think that will match here. If it is a simulation and Qt only a 
representation, what is the problem if the simulator run in a separate 
standard thread? And if you have Qt, then in a QThread.

> So here is my refined question:
>
>   How can I *create* and update (derived) QGraphicsItems in a QThread
> that are to be visualized in a QGraphicsView in the main thread?

You can't. That is not possible in any way. Since the QGraphicsView might 
crash if the item has been created/updated from a different thread. What you 
can do is what i said before is to add you stuff on top of, create the QGI in 
the main thread, protect the new data you want to call from your other thread 
but in any case don't call QGV/QGI method from the worker thread.

>
> kind regards,
> Bas Terwijn
>
> Alexis Ménard wrote:
> > On Monday 30 March 2009 18:03:27 Bas Terwijn wrote:
> > > hi all,
> > >
> > > I am trying for some time to use QGraphicsView in a multi-threaded
> > > application where in the main thread I create an QGraphicsView object
> > > and run the event loop while in a seperate QThread I create and and
> > > change many QGraphicsItems that are to be visualized in the main
> > > thread. For thread safety I use a mutex to synchronize the (overloaded)
> > > QGraphicsView::update() method and all changes to the QGraphicsItems.
> > >
> > > My approach doesn't work. I find that the QGraphicsView doesn't update,
> > > even when I send signals which I think should start the
> > > QGraphicsView::updateScene() from the main thread. I also experience
> > > core dumps.
> > >
> > > Did anybody find a way to use QGraphicsView to visualize QGraphicsItems
> > > that are changed by a seperate QThread?
> > >
> > > Thank you for your time,
> > > Bas Terwijn
> >
> > It won't work...QGraphicsView framework is not thread safe. So you
> > can't call any functions from your separate thread, it might crash.
> > But what is the point to do the changes into your thread, can't your
> > thread emit a signal when it finish to compute and then the main
> > thread change the item itself?
> >
> > > _______________________________________________
> > > Qt-interest mailing list
> > > Qt-interest at trolltech.com
> > > http://lists.trolltech.com/mailman/listinfo/qt-interest
> >
> > --
> >
> > Alexis Ménard
> > Software Engineer, Widgets Team 1
> > Qt Software, Nokia Norge AS, Sandakerveien 116, 0484 Oslo, Norway
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Qt-interest mailing list
> > Qt-interest at trolltech.com
> > http://lists.trolltech.com/mailman/listinfo/qt-interest

-- 

Alexis Ménard
Software Engineer, Widgets Team 1
Qt Software, Nokia Norge AS, Sandakerveien 116, 0484 Oslo, Norway
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090331/b4fb75e1/attachment.html 


More information about the Qt-interest-old mailing list