Repainting a QGraphicsView
Hi! Is it possible to redraw a QGraphicsView so that, in case it has the focus, it has a border around? I tried to paint it like any other widget, overriding the paintEvent method, but it seems nothing happens. Is there any other way?
Thanks for any advice!
Re: Repainting a QGraphicsView
Could you show the relevant code, please? Did you remember to open the painter on the viewport as instructed in QAbstractScrollArea::paintEvent() docs?
Re: Repainting a QGraphicsView
mmh... in the doc I found that the paintEvent method is reimplemented from QWidget::paintEvent(...). So, I did what I usually do with QWidgets.
Code:
painter.
setBrush(QBrush(Qt
::gray));
painter.
drawRect(QRect(0,
0, this
->width
(), this
->height
()));
Then, I painted the scene, ad I saw this cannot be omitted:
What do you mean by "open the painter"? It seems I'm lacking something...
Thanks for your answer!
Re: Repainting a QGraphicsView
Hi Can you please let me know where you are trying to draw the border? On Scene or View?
Re: Repainting a QGraphicsView
Show us the paintEvent of your view class.
Probably you are first painting the border and then calling the base class paintEvent. This will override your painting !
You will need to first call the base class function and then draw after it.
Re: Repainting a QGraphicsView
This is what I wrote:
Code:
painter.
setBrush(QBrush(Qt
::green));
painter.
drawRect(QRect(0,
0, this
->width
(), this
->height
()));
}
I can't understand. Maybe some other property I don't remember inserting is overwriting the background...
EDIT: I noticed in debug mode I get this:
warning: QPainter::begin: Paint device returned engine == 0, type: 1
warning: QPainter::setBrush: Painter not active
warning: QPainter::drawRects: Painter not active
Re: Repainting a QGraphicsView
try this :
Code:
{
// 1. draw scene items
// 2. draw zoom area if any
if( m_zoomArea.width()!=0 || m_zoomArea.height()!=0 )
{
painter.save() ;
// paint here
painter.restore() ;
}
}
i.e., draw on view's viewport instead
Re: Repainting a QGraphicsView
It seems it is not redrawing correctly the scene, lines remain in the area. This is what I wrote:
Code:
painter.save();
pen.setColor(SELECTION_BORDER_AREA_QCOLOR);
pen.setWidth(GoodiesStore::SELECTION_BORDER_AREA_WIDTH);
painter.setPen(pen);
painter.
drawRect(QRect(0,
0, this
->width
(), this
->height
()));
painter.restore();
}
When I scroll the view, it seems lines are scrolled too.
Thank you for your help!
Re: Repainting a QGraphicsView
In case anyone else need this, I didn't know how to solve this so I placed a label under the QGraphicsView and made it larger than the QGraphicsView. The result is exactly the same.