Adding Rectangular overlay on graphics view
To simplify my problem, lets assume that we have two different graphics views. Both of these views have the same scene applied to it. When the user clicks on a specific point in the first graphics view I want to draw a rectangle related to the point she clicked, but I want it to only be visible in that graphics view. So I'm assuming I can't just add a rectangle to the scene because it would be visible on both. I was looking into using QGraphicsView's foregroundBrush(), but I'm not sure if that is what I'm looking for either. Doesn't the foregroundBrush tile the brushes pixmap? Any ideas would be appreciated.
Re: Adding Rectangular overlay on graphics view
You probably want to override QGraphicsView::drawForeground().
Re: Adding Rectangular overlay on graphics view
I'm having problems writing the code to override drawForeground (or any virtual protected function for that matter). Does anyone know if there is an example I can look at? I'm trying to go through the message boards but no luck so far.
thanks for the tip spud...
Re: Adding Rectangular overlay on graphics view
Here's a little example how to override QGraphicsView::drawForeground():
Class declaration:
Code:
{
public:
MyGraphicsView
(QWidget* parent
= 0);
...
protected:
void drawForeground
(QPainter* painter,
const QRectF
& rect
);
};
Implementation:
Code:
void MyGraphicsView
::drawForeground(QPainter* painter,
const QRectF
& rect
) {
painter->drawRect(...);
}
Re: Adding Rectangular overlay on graphics view
thanks j-p,
I'm not seeing anything in the graphicsview's with:
Code:
{
painter->setPen(border);
painter->drawRect(10,10,100,300);
}
It compiles, but I'm not seeing any rectangle being drawn. The other thing I'm worried about is that I currently have more then 1 graphics view in this class. I only want this drawForeground function to run for one of them, is that do-able? Also, is there a way to call this function whenever I need the rectangle to be updated?
Re: Adding Rectangular overlay on graphics view
Both QGraphicsView and QGraphicsScene define a virtual drawForeground() function. If you reimplement the one in the scene, all views will see this foreground, but you can just reimplement it in a view to create a view-specific foreground.
These functions draw in scene coordinates, so you just need to make sure that what you are drawing fits inside the scene rect. If your scene rect (see QGraphicsScene::sceneRect()) is defined as (-1000, -1000, 2000, 2000), you could do this to draw a transparent green overlay:
Quote:
void MyScene::drawForeground(QPainter *painter, const QRectF &exposed)
{
// 50% transparent green overlay
painter->fillRect(-1000, -1000, 2000, 2000, QColor(0, 255, 0, 127));
// or
painter->fillRect(exposed, QColor(0, 255, 0, 127));
}
The exposed argument is an optimization for partial updates; you don't want to fill the whole scene it only parts of it are exposed.
Re: Adding Rectangular overlay on graphics view
How exactly am I supposed to call this function for a particular graphicsView? Is there a specific QPainter pointer that I need to pass as a parameter that is related to the specific graphicsView? I'm just unclear as to how and when this drawForeground function is being called.
Re: Adding Rectangular overlay on graphics view
u need not call this function... it is called automatically.
U just need to override it in ur scene class and do the drawing using the painter pointer provided :)
Re: Adding Rectangular overlay on graphics view
I think I'm beginning to understand what you've all been talking about. But the QGraphicsView's that I'm using were all added to the same widget using Qt Designer. Am I supposed to write a class for each graphicsView that I added in designer? If so, how can I write a class for each individual graphicsView? Or am I still way of target?
Re: Adding Rectangular overlay on graphics view
Qt Designer has a widget promotion feature. Subclass QGraphicsView, open context menu over the graphics view widget in the designer and choose "Promote to Custom Widget".
Re: Adding Rectangular overlay on graphics view
Thanks everyone! I got it working using a variety of all your tips. Mainly I used the widget promotion in designer, and then subclassed QGraphicsView to do what I wanted. :)