PDA

View Full Version : QGraphicsItem subclass access to QGraphicsView size



rubenvb
21st January 2010, 20:50
Hi,

I would like to draw an arrow inside a QGraphicsScene linked to a QGraphicsview, but the arrow needs to reach from the left-bottom of the view to the right-bottom of the view. It also needs to move and stay +/- 10 pixels above the bottom. It should always be in view, but resize only one line (no transformation).
Something like this:
|-------------------->|
resized:
|---------------------------------------->|
note the arrow pointer thing (>) has not changed size.
It almost seems like I need to make a QPainterPath with three lineTo's and one moveTo, where one lineTo is subject to normal transformation, but the rest isn't.
My conclusion would be QPainterPath isn't what I'm looking for; maybe a QList<QGrapicsLineItem> is what I need. What do you think?

Thanks

ecanela
22nd January 2010, 06:20
if only need to draw a arrow in the top of the view is more simple subclass a view and draw the arrow in the drawForeground() function



class CustomView : public QGraphicsView
{
public:
explicit CustomView ( QWidget * parent = 0 )
: QGraphicsView (parent){};

protected:
void drawForeground ( QPainter * painter, const QRectF & rect )
{
painter->setPen(QPen(Qt::red,3));
painter->drawLine( rect.topLeft(), rect.bottomRight() );
painter->drawLine( rect.topRight(), rect.bottomLeft() );
}
};



this draw a big X in the view.
convert the code to draw a arrow must be trivial. but is late and i want to sleep

rubenvb
23rd January 2010, 10:37
That looks like a good idea, that way I won't have to worry about getting parent widget coordinates.

One question though: will it be possible to "print" the whole scene (including the arrows) to an image, or will the arrow not be included because it's not part of the scene?

Thanks!

rubenvb
23rd January 2010, 16:51
Note to self, must read docs:
http://doc.trolltech.com/4.2/qgraphicsview.html#render

It is the view that is rendered to the printer, not the scene, so drawing on the foreground it is!

thanks again

wysota
23rd January 2010, 21:36
There is also QGraphicsScene::render().