Qt-interest Archive, July 2007
Bugfix/Workaround: QTableView, QSqlTableModel, OnManualSubmit and the verticalHeader()-Problem
Message 1 in thread
Hi all!
If you use a QSqlTableModel with a QTableView in OnManualSubmit Mode,
you will have a repaint-Problem with the verticalHeader() if you add or
remove rows.
A simple workaround is available:
* Derive your own model class from QSqlTableModel
* Override the following functions:
virtual bool insertRows(int row, int count, const QModelIndex& parent =
QModelIndex());
virtual bool removeRows(int row, int count, const QModelIndex& parent =
QModelIndex());
* Use the following implementation:
bool YourModel::insertRows(int row, int count, const QModelIndex& parent)
{
bool r = QSqlTableModel::insertRows(row, count, parent);
if(editStrategy() == OnManualSubmit)
{
emit headerDataChanged(Qt::Vertical, row, row+count-1);
emit dataChanged(index(row, 0), index(row+count-1, columnCount()-1));
}
return r;
}
bool YourModel::removeRows(int row, int count, const QModelIndex& parent)
{
bool r = QSqlTableModel::removeRows(row, count, parent);
if(editStrategy() == OnManualSubmit)
{
emit headerDataChanged(Qt::Vertical, row, row+count-1);
emit dataChanged(index(row, 0), index(row+count-1, columnCount()-1));
}
return r;
}
Note:
The emit headerDataChanged(...) call fixes the bug.
The emit dataChanged(...) is only nessesary, if you have special
painting routine in you delegate for added or removed rows. Example:
QVariant YourModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::BackgroundRole)
{
QString headerdata = headerData(index.row(), Qt::Vertical,
Qt::DisplayRole).toString();
if(headerdata == "*")
return QColor(120, 255, 120);
if(headerdata == "!")
return QColor(255, 120, 120);
}
return QSqlTableModel::data(index, role);
}
Hope this will save you some time!
Greetings
Niklas
--
[ signature omitted ]