PDA

View Full Version : Repainting a QGraphicsView



Luc4
27th April 2010, 09:18
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!

jpn
27th April 2010, 16:42
Could you show the relevant code, please? Did you remember to open the painter on the viewport as instructed in QAbstractScrollArea::paintEvent() docs?

Luc4
27th April 2010, 21:34
mmh... in the doc I found that the paintEvent method is reimplemented from QWidget::paintEvent(...). So, I did what I usually do with QWidgets.

QPainter painter;
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:

QGraphicsView::paintEvent(event);
What do you mean by "open the painter"? It seems I'm lacking something...
Thanks for your answer!

gopikrishnav
28th April 2010, 04:18
Hi Can you please let me know where you are trying to draw the border? On Scene or View?

aamer4yu
28th April 2010, 06:03
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.

Luc4
28th April 2010, 09:09
This is what I wrote:

void MyView::paintEvent(QPaintEvent* event) {
QGraphicsView::paintEvent(event);
QPainter painter(this);
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

totem
28th April 2010, 14:59
try this :



void MyView::paintEvent(QPaintEvent * e)
{
// 1. draw scene items
QGraphicsView::paintEvent(e);

// 2. draw zoom area if any
if( m_zoomArea.width()!=0 || m_zoomArea.height()!=0 )
{
QPainter painter( viewport() );
painter.save() ;
// paint here
painter.restore() ;
}
}

i.e., draw on view's viewport instead

Luc4
28th April 2010, 15:35
It seems it is not redrawing correctly the scene, lines remain in the area. This is what I wrote:

void MyView::paintEvent(QPaintEvent* event) {
QGraphicsView::paintEvent(event);
QPainter painter(this->viewport());
painter.save();
QPen pen;
pen.setColor(SELECTION_BORDER_AREA_QCOLOR);
pen.setWidth(GoodiesStore::SELECTION_BORDER_AREA_W IDTH);
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!

Luc4
29th April 2010, 14:09
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.