Update() doesn't start paintevent until the window is resized
-
Hi there!
I have a function in which i call update(), I want it to redraw the things in my qGraphicsView. Now, the thing is that it doesn't redraw anything unless the window is resized. It somehow puts on the stack all the calls of update() and starts them when I resize the screen. Why is that?
Thanks for your help! -
Do you filter update events somewhere? Update events should paint your widget in the next iteration of the event-loop, so they don't "get on the stack"...
but without some code we can just guess what you are doing wrong... -
Here's the part of code responsible for it :
@void View::mousePressEvent(QMouseEvent event)
{
int xPosition = (int)(event->x()/(sceneRect().width()/7));
int yPosition = (int)(event->y()/(sceneRect().height()/7));
int position = xPosition + yPosition7;
findPossibleMoves(position);
}@@void View::findPossibleMoves(int position)
{
highlights->clear();
if(position % 7 != 6 && isEmpty(position+1))
highlights->push_back(position+1);
if(position % 7 != 0 && isEmpty(position-1))
highlights->push_back(position-1);
if(position > 6 && isEmpty(position-7))
highlights->push_back(position-7);
if(position < 42 && isEmpty(position+7))
highlights->push_back(position+7);
update();
}@ -
you need to post more code (eventFilters, paintEvent handler, initializations,...) ... the posted code is obviously correct.
There must be something else which prevents the delivery of paintEvents, etc. -
@ void View::paintEvent(QPaintEvent event)
{
QPainter painter(viewport());
drawBackground(&painter, sceneRect());
drawTiles();
}@
@
void View::drawBackground(QPainter painter, const QRectF &rect)
{
for(int i = 0; i < 49; i++){
QRect square(sceneRect().width()/7(i%7), sceneRect().height()/7(i/7), sceneRect().width()/7, sceneRect().height()/7);
drawBoard(painter, square, i, false);
}
for(int i = 0; i < highlights->size(); i++){
int position = highlights->at(i);
QRect square(sceneRect().width()/7*(position%7), sceneRect().height()/7*(position/7), sceneRect().width()/7, sceneRect().height()/7);
drawBoard(painter, square, position, true);
}
}@
@
void View::drawTiles()
{
for(int i = 0; i < 14; i++){
tiles[i]->draw();
}
}@
@
void View::drawBoard(QPainter painter, QRect &rect, int a, bool highlighted)
{
QColor lines;
if(highlighted)
lines = Qt::yellow;
else
lines = QColor(0, 205, 0);
QBrush brush(lines);
QPen pen(brush, sceneRect().width()/100);
QColor fill1(188, 238, 104);
QColor fill2(110, 139, 61);
painter->setPen(pen);
painter->drawRect(rect);
if(a%2 == 0)
painter->fillRect(rect, fill1);
else
painter->fillRect(rect, fill2);
}@
@
void View::resizeEvent(QResizeEvent event)
{
this->setSceneRect(0,0,this->frameSize().width(),this->frameSize().height());
}@ ,
where View derives from QGraphicsViewso it works like this :
mousePressEvent() -> finds possible moves & puts them in highlights -> calls update()
update is supposed to redraw but it doesnt unless i resize the window
by the looks of it, it doesnt even put the update() calls on stack, it just doesnt redraw anything. its window resize that calls the paintevent
EDIT:
hooray, found this post : http://stackoverflow.com/questions/2274512/qt4-update-or-repaint-fails-to-trigger-paintevent
did viewport()->update() and it works. what is the reason? -
you shouldn't use the paintEvent() like you did!
you are starting a painter not on the widget itself but on a complete other widget (viewport widget). I think this will result in a warning in the console?! And all following paint calls will fail...
Additionally you override QGraphicsView::drawBackground() which is virtual and gets called by Qt on resize of the viewport. This is why it works on resizing and not when calling your update() on the QGraphicsView widget.
You should completely remove your current paintEvent override and as you already noticed use viewport->update().