zeroknowledge
13th May 2011, 01:45
Hola,
spent whole night trying to figure out this problem. No luck.
I'm using a QSqlRelationalTableModel-derived class to handle a database table with a QTableView. However, I have to insert one custom column to the model for displaying additional info.
In app's constructor:
static CMyTableModel model; // Derived from QSqlRelationalTableModel
model.setTable("BookTable"); // 2 fields: ID and Name
model.select(); // Populate from database
model.insertColumn(2); // Insert column for additional data
I set up the QTableView:
QTableView* tv = ui.tableview;
tv->setModel(&model); // Set model
tv->hideColumn(0); // Hide ID
tv->setItemDelegateForColumn(2, new CMyDelegate()); // Set our delegate to custom column
tv->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
I have overriden data to handle the additional column:
QVariant CMyTableModel::data(const QModelIndex &item, int role) const
{
if(item.column() == 2) // Our column
{
if(role == Qt::DisplayRole)
{
return QString(...);
}
if(role == Qt::EditRole)
{
return QString(...);
}
}
// Default
return QSqlRelationalTableModel::data(item, role);
}
And here's the delegate's most important methods:
void CMyDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
// !!! PAINTS JUST FINE
}
QWidget * CMyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
return new QPushButton("test"); // !!! NEVER CALLED
}
Whatever I try, I just can't get the inserted column to enter edit mode. What's missing? Would REALLY appreciate some help here. Thanks. :)
spent whole night trying to figure out this problem. No luck.
I'm using a QSqlRelationalTableModel-derived class to handle a database table with a QTableView. However, I have to insert one custom column to the model for displaying additional info.
In app's constructor:
static CMyTableModel model; // Derived from QSqlRelationalTableModel
model.setTable("BookTable"); // 2 fields: ID and Name
model.select(); // Populate from database
model.insertColumn(2); // Insert column for additional data
I set up the QTableView:
QTableView* tv = ui.tableview;
tv->setModel(&model); // Set model
tv->hideColumn(0); // Hide ID
tv->setItemDelegateForColumn(2, new CMyDelegate()); // Set our delegate to custom column
tv->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
I have overriden data to handle the additional column:
QVariant CMyTableModel::data(const QModelIndex &item, int role) const
{
if(item.column() == 2) // Our column
{
if(role == Qt::DisplayRole)
{
return QString(...);
}
if(role == Qt::EditRole)
{
return QString(...);
}
}
// Default
return QSqlRelationalTableModel::data(item, role);
}
And here's the delegate's most important methods:
void CMyDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
// !!! PAINTS JUST FINE
}
QWidget * CMyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
return new QPushButton("test"); // !!! NEVER CALLED
}
Whatever I try, I just can't get the inserted column to enter edit mode. What's missing? Would REALLY appreciate some help here. Thanks. :)