PDA

View Full Version : Error format a cell in QTableView



estanisgeyer
3rd September 2008, 23:30
Hi guys,

I have a QTableView with four columns. I reimplement the virtual method data( ) to align text the cells. So far, so good, but I also want to replace the cells where the value is 0 (zero) for a dash ("-"). While trying to do this, my application compiles correctly but gives an error of segmentation fault when I open the window.
See the code:



QVariant TableViewQryModel::data(const QModelIndex &idx, int role) const
{
QVariant v = QSqlQueryModel::data(idx, role);

if (!idx.isValid())
return QVariant( );

if (role == Qt::DisplayRole && idx.column() > 0 &&
index(idx.row(), idx.column(), idx.parent()).data().toString() == "0")
{
return QVariant("-");
}
else if (role == Qt::TextAlignmentRole)
{
return int(Qt::AlignCenter | Qt::AlignVCenter);
}

return (v);
}


Thanks,

Marcelo E. Geyer

jacek
4th September 2008, 02:00
if (role == Qt::DisplayRole && idx.column() > 0 &&
index(idx.row(), idx.column(), idx.parent()).data().toString() == "0")
This causes an infinite recursion.

Use:

if( role == Qt::DisplayRole && idx.column() > 0 && QSqlQueryModel::data( idx ).toString() == "0" ) {
...
or, since you call data() in the first line:


if( role == Qt::DisplayRole && idx.column() > 0 && v.toString() == "0" ) {
...

estanisgeyer
4th September 2008, 15:06
Ok, it's works!
I do not understand where this happens recursion, could you explain?

Thanks,
Marcelo E. Geyer

Ginsengelf
4th September 2008, 15:22
Hi,
you call index() with the row, column and parent of the index you are testing, ergo get the same index, and then call data() (the method you are already in), which will calls index() with the row, column and parent of the index you are testing, ... until infinity.

Ginsengelf