PDA

View Full Version : Access to custom widget functions in a QGraphicsScene



meazza
15th April 2011, 08:57
Hello fellow Qt programmers.

I would like to see if you have any ideas to my problem.

Here we go. I have a library with custom widgets which I use in my project. I use these widgets in a QGraphicsScene. I add them with the use of QGraphicsProxyWidget. Now to get to the problem. After I have added these widgets i want to access the functions that the initial widget has. ex. my widget has a setValue function.

And i want to access its functions when ex. you right click on the item in the scene, scene returns the QGraphicsItem with the use of function itemAt. Now I want to cast this QGraphicsItem to the original widget. But this is a problem if I have 20 widgets and I dont know what to cast it to.

What I am looking for is something similar to the type() functions in QGraphicsItem but for my widget. So I can set a specific type for my widget and i can access it in someway to compare with the QGraphicsItem I get returned.

Any ideas would be helpful for mapping this in a good way. I have thought of QHash and storing the pointer of the inital widget together the a string key which i also pass for the QGraphicsProxyWidget as data. But this gets messy when you have 20 diffrent widgets.

SixDegrees
15th April 2011, 09:09
This sounds like a poor design. Create an abstract base class that defines a common interface, then derive all of your QGraphicsItem classes from that interface. Then all items added to a scene will be instances of the same base class and can be treated uniformly. Handle variant behavior at the level of the derived classes.

meazza
15th April 2011, 09:21
It is a design still in work hence my post here to utilize my design. I dont have any QGraphicsItem classes. The items I add are classes derived from QWidget. But I can see what you mean with the base class.