PDA

View Full Version : Problem with painting QTable cell



codebehind
27th April 2007, 13:00
Hello!

I try to paint special cell (dynamicaly of course) and i get this error for each cell i execute paint on it:
"QGVector::remove: Index -7 out of range"
and the cell does not change color and it is whitout any data.


void UAutomat::PopulateVrednosti(int nRow)
{
tblRezultat->setText(nRow,0,QString("%1").arg(_ucenje->GetPovKaz(),0,'f',4));
for(int i=0;i<_ucenje->_igralec.GetMaxAkc();i++)
{
if (i== _ucenje->GetAkcija())
{
MyItem it(tblRezultat,QTableItem::Never,QString("%1 \%").arg(_ucenje->_igralec.GetVerN(i)*100,0,'f',2));

if (_ucenje->GetKazen()) it.SetBkgColor(Qt::red);
else it.SetBkgColor(Qt::green);

QColorGroup cg = colorGroup();
QPainter p(tblRezultat);
it.paint(&p,cg,tblRezultat->cellGeometry(nRow,i),false);
}
else
tblRezultat->setText(nRow,i+1,QString("%1 \%").arg(_ucenje->_igralec.GetVerN(i)*100,0,'f',2));
}

tblRezultat->showRow(nRow);
}

tblRezultat is a pointer to QTable

MyItem code is here (http://lists.trolltech.com/qt-interest/2003-02/msg00270.html)

high_flyer
27th April 2007, 13:30
"QGVector::remove: Index -7 out of range"
I didn't read your code very carefuly, but it looks ok.
I'd say the above error is the problem.
It looks like you have a problem with your indexes.
Are you sure that nRow is a positive int, and that it is in the table row index range?

codebehind
27th April 2007, 14:10
yes i'm sure!
otherwise others cells will be empty to, right?
i printed out index value and it is ok.

high_flyer
27th April 2007, 15:05
otherwise others cells will be empty to, right?
Not necessarily - only those who have bad indexes.
Never the less, you most certainly have an index problem indicated by the error you posted.
And the fact this error comes only when the cell painting is not working, suggests that there is a connection.
Could you show the code whre you use QGVecotr::remove()?

codebehind
27th April 2007, 16:42
Could you show the code whre you use QGVecotr::remove()?
I don't use this function anywhere, nor the QGVector.

jacek
27th April 2007, 17:47
Does this message disappear when you comment out line 15. (the one with "it.paint(...)")?

Also what is that MyItem class and when do you call PopulateVrednosti()?

high_flyer
27th April 2007, 17:49
Well, could you show the loop in which PopulateVrednosti() is called?

codebehind
28th April 2007, 18:44
Well, could you show the loop in which PopulateVrednosti() is called?


void UAutomat::PopulateVrednosti(int nRow)
{
tblRezultat->setText(nRow,0,QString("%1").arg(_ucenje->GetPovKaz(),0,'f',4));
for(int i=0;i<_ucenje->_igralec.GetMaxAkc();i++)
{
if (i== _ucenje->GetAkcija())
{
// MyItem it(tblRezultat,QTableItem::Never,QString("%1 \%").arg(_ucenje->_igralec.GetVerN(i)*100,0,'f',2));

// if (_ucenje->GetKazen()) it.SetBkgColor(Qt::red);
// else it.SetBkgColor(Qt::green);

QColorGroup cg = colorGroup();
QPainter p(tblRezultat);
// it.paint(&p,cg,tblRezultat->cellGeometry(nRow,i),false);
// tblRezultat->setItem(nRow,i+1,&(QTableItem)it);
}
else
tblRezultat->setText(nRow,i+1,QString("%1 \%").arg(_ucenje->_igralec.GetVerN(i)*100,0,'f',2));
}

Monitor->append(QString("Vrstica: %3 Izbrana akcija: %1 %2").arg(_ucenje->GetAkcija()).arg((_ucenje->GetKazen())?
"kazen":"nagrada").arg(nRow,0,'f',0));
tblRezultat->showRow(nRow);
}


Does this message disappear when you comment out line 15. (the one with "it.paint(...)")?

no. it disappeare when i comment out the line with the initialization of the object MyItem. it s definition you can find here (http://lists.trolltech.com/qt-interest/2003-02/msg00270.html)

jacek
28th April 2007, 19:14
MyItem it(tblRezultat,QTableItem::Never, ...);
...
tblRezultat->setItem(nRow,i+1,&(QTableItem)it);
You create MyItem on the stack, pass a pointer to it to setItem() and then that MyItem is destroyed, because it goes out of scope, leaving a dangling pointer. Also there is no point in painting the item when PopulateVrednosti() is called --- QTable will repaint itself on its own.

Try:

if( i == _ucenje->GetAkcija() ) {
MyItem * it = new MyItem( 0, QTableItem::Never, QString( "%1 \%" ).arg( _ucenje->_igralec.GetVerN(i)*100, 0, 'f', 2 ) );
if( _ucenje->GetKazen() ) {
it->SetBkgColor( Qt::red );
else {
it->SetBkgColor( Qt::green );
}
tblRezultat->setItem( nRow, i+1, it );
}

If the message still appears, check if it isn't caused by "_ucenje->_igralec.GetVerN(i)".

Also, please, use [code] tags for code snippets.

codebehind
28th April 2007, 19:26
thanks. it works great!
about my posts i'll listen to your advice. I have to lear that too!