Recurcive repaint problem
-
Hello, I'm making an app similar to paint, but having a problem with recursive repaint and QVector indexes out of range( I don't know why this is happening ). Here's how my error's look in compiler:
@ASSERT failure in QVector<T>::operator[]: "index out of range", file ....\5.2.1\mingw48_32\include/QtCore/qvector.h, line 369
QWidget::repaint: Recursive repaint detected@
Here're links to my paint.h and paint.cpp files:
https://github.com/Vyivrain/ProjectFiles/blob/master/mypaint.h
https://github.com/Vyivrain/ProjectFiles/blob/master/mypaint.cpp
Thx for answering. -
Are you using a debugger? Set break points on your paint where you access the vector elements. Check your variable window and look at the size, you are exceeding the size of the multi dimensional array somewhere in your code.
Also, you have no bound checks on your vector in your repaint which from what I can see will most likely occur before your mouse press event...therefore, your inner vector in your multi dimensional array will be empty and you will 'exceed the bounds of the array'.
include <QDebug> at the top of your .cpp file, and before you access your vector in the paint method, print a statement to the log window:
@
qDebug() << geo.size();
@Then start working backwards to figure out where the issue is coming from.
-
The app starts nicely, but when I try to press any button this stuff happens. If you want the app is on github you can download hole project there. I think the hole problem is mouseEvent and paintEvent, because f.e. rectButtonClicked, ellButtonClicked, lineButtonClicked is only assure, that only one button is active( checked ) at a time. And another methods are just for initialization.
-
Hi,
I just started the programin the debugger.
what fails is:@
void MyPaint::paintEvent(QPaintEvent *pe)
{
QPainter painter( this );
if( lineButClicked )
{
painter.drawLine( geom[index][0], geom[index][1] ); //<-- here
index++;
pointsNum = 0;
}
@geom has size 1, geom[0] has size 0, but you access elements of it.
you activate the sections inpaint with clicking the button. But the geom vector does contain an empty vector.
-
But your code hasmor problems.
You are painting on the dialog, not on the view for example.@
void MyPaint::paintEvent(QPaintEvent *pe)
{
QPainter painter( this );
painter.setPen(Qt::red);
if( lineButClicked )
{
if(geom[index].size() >= 2)
{
painter.drawLine( geom[index][0], geom[index][1] );
index++;
pointsNum = 0;
}
}
else if( ellButClicked )
{
if(geom[index].size() >= 2)
{
painter.drawRect( QRect( geom[index][0], geom[index][1] ) );
index++;
pointsNum = 0;
}
}
else if ( rectButClicked )
{
if(geom[index].size() >= 2)
{
painter.drawEllipse( QRect( geom[index][0], geom[index][1] ) );
index++;
pointsNum = 0;
}
}
}@
-
Yeah, I know, but I can't paint on the dialog, because I have a condition in mouseEvent. But the main thing is repaint. How ca I deal with it, so it will repaint only, when mouseEvent occurs and there I'll use update to launch paintEvent. And because I didn't know that repaint calls whenever it wants , it became obvious error, my bad =(.
-
You have to overwrite the paint event of the widget, where you want to paint in.
You subclassed the dialog, hence you draw in the dialog.@
void MyPaint::paintEvent(QPaintEvent *pe)
@MyPaint is the dialog...
[quote author="Vyivrain" date="1403644402"]Yeah, I know, but I can't paint on the dialog, because I have a condition in mouseEvent. But the main thing is repaint. How ca I deal with it, so it will repaint only, when mouseEvent occurs and there I'll use update to launch paintEvent. And because I didn't know that repaint calls whenever it wants , it became obvious error, my bad =(.[/quote]
Repaints can also occure, if yomeone puts a window in front of yours and away again, there are many reasons.