PDA

View Full Version : QGraphicsItem or QGraphicsProxyWidget?



ttvo
25th August 2009, 21:31
Hi,

I need to create an image as below
3604
It contains:
1) one or more nodes with a text label. The node is not filled when created, but will be filled with progress
2) there are one or more ports on each node connecting them together

I first looked at the Diagram Scene example, but I'll need the port(s) to know which node connecting to which port and also need to add text to the node.

Should I subclass QGraphicsItem or QGraphicsProxyWidget or something else? Thanks in advance.

wysota
25th August 2009, 22:53
Definitely not proxy widget. I'd subclass QGraphicsRectItem or QGraphicsTextItem or even compose the item from one rect item, one text item and a couple of ellipse items (or others) for the ports.

ttvo
26th August 2009, 21:42
Thanks. I understood sub-class QGraphicsRectItem and QGraphicsTextItem. What do you mean by "compose the item from one rect item, one text item and a couple of ellipse items (or others) for the ports"? Have a class that contains a QGraphicsRectItem, QGraphicsTextItem, and ports? What will my paint method look like?

wysota
27th August 2009, 00:17
What will my paint method look like?

You won't have one.


QGraphicsRectItem *rect = new QGraphicsRectItem(QRectF(-50, -25, 100, 50));
rect->setFlags(QGraphicsItem::ItemIsMovable);
QGraphicsTextItem *text = new QGraphicsTextItem(rect);
QGraphicsEllipseItem *portN = new QGraphicsEllipseItem(QRectF(-5, -5, 10, 10), rect);
portN->setPos(0, -25);
QGraphicsEllipseItem *portS = new QGraphicsEllipseItem(QRectF(-5, -5, 10, 10), rect);
portS->setPos(0, 25);
QGraphicsScene scene;
scene.addItem(rect);
QGraphicsView v;
v.setScene(&scene);
v.show();

ttvo
28th August 2009, 17:15
Great, that works. I decided to subclass QGraphicsTextItem to include the draw of its boundary, eliminating the need for the QGraphicsRectItem *rect. So the ports are children of my custom text, but how am I going to be able to query how many ports a given custom text item has?

Should I make the ports as member variable of my custom text? If so, will I need to override the paint method to draw the ports (of type QGraphicsEllipseItem or QGraphicsRectItem). Thanks.

wysota
31st August 2009, 00:13
Great, that works. I decided to subclass QGraphicsTextItem to include the draw of its boundary, eliminating the need for the QGraphicsRectItem *rect. So the ports are children of my custom text, but how am I going to be able to query how many ports a given custom text item has?
You need to provide some API for this in your subclass.


Should I make the ports as member variable of my custom text? If so, will I need to override the paint method to draw the ports (of type QGraphicsEllipseItem or QGraphicsRectItem). Thanks.

Why does informing one object about a state of another object require reimplementing its drawing routines?