You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, February 8th 2010, 7:57pm

How can a QGraphicsItem signal a scene update?

I have a QGraphicsScene with a number of items in some arbitrary hierarchy.
In my application there are some events which may occur for a single item that should cause the whole scene to be recalculated.
For example an event on an item way down in the hierarchy might create a need to reposition all top level QGraphicsItems in the scene.

The scene recalculation code is a method on my mainwindow.

So I need some way to allow an item to tell the mainwindow to recalculate. A signal seems obvious, and I have read up on inheriting both QObject and QGraphicsItem to allow my items to emit signals.

Problem is signal/slot connections are always made on instances, so I would have to connect every item instance on the scene to the mainwindow slot. Somehow this doesn't feel right. Or is it?

Is there any better way?

/j

Junior

Professional

  • "Junior" is male

Posts: 1,622

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Monday, February 8th 2010, 8:57pm

If you do decide to derive a class with both QObject and QGraphicsItem as base classes, QObject should be the first base class before QGraphicsItem.
Since your deriving (sub-classing) you could create a marker as well in the derived class ( bool useSignal ) and default to false. Either set by passing in the constructor or create a public function() or slot to turn it on and off. This way to can control which graphic items can signal. If( useSignal ) emit someSignal(); // example only of course //

Junior

3

Monday, February 8th 2010, 9:05pm

Thanks, but I already figured the "QObject and QGraphicsItem as base classes" stuff.

My issue is I don't want to have to make thousands of connect calls (one for each item) to connect the signal of each item to the mainwindow slot. It feels inefficient. So I'm asking about OTHER ways to do it...?

/j

Junior

Professional

  • "Junior" is male

Posts: 1,622

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

4

Monday, February 8th 2010, 9:52pm

It feels inefficient? I guess you could create two separate derived classes ( one with signals, the other without ) and when creating the objects, stipulate which one to create.
Sorry I can't help you further on this.
Junior