PDA

View Full Version : Delegate's editor won't launch in a custom inserted column



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. :)

JoZCaVaLLo
13th May 2011, 08:26
Did you try with QLineEdit???



QWidget * CMyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QVariant value = index.data(Qt::EditRole);
QLineEdit *lineEdit = new QLineEdit(parent);
lineEdit->setText(value.toString());
return lineEdit;
}


And also... the additional Column should be also in your underlying model!!!

MarekR22
13th May 2011, 11:21
If it is never called this means that your model is not editable! Do you return proper flags for: QAbstractItemModel::flags?

Added after 9 minutes:


Did you try with QLineEdit???



QWidget * CMyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QVariant value = index.data(Qt::EditRole);
QLineEdit *lineEdit = new QLineEdit(parent);
lineEdit->setText(value.toString());
return lineEdit;
}


And also... the additional Column should be also in your underlying model!!!
Take a look on documentation! There are other virtual methods designed to set value of editing widget and setting value for data model.
See QAbstractItemDelegate::setEditorData and QAbstractItemDelegate::setModelData.

JoZCaVaLLo
13th May 2011, 12:55
I don't think so: he has problem on createEditor... not the other functions...

I now saw that you used

tv->setItemDelegateForColumn(2, new CMyDelegate()); // Set our delegate to custom column

but if you have the additional column on the model, why don't you use the standard delegate???
According to doc you have to beware on which delegate will be used:

Note: If a delegate has been assigned to both a row and a column, the row delegate will take precedence and manage the intersecting cell index.

for simplicity I would use the standard itemDelegate

tv->setItemDelegate(new CMyDelegate());

QWidget* CMyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.column() == 2) // Our column
{
return new QPushButton("test");
}
}

wysota
13th May 2011, 13:55
Does "paints just fine" mean the code in paint() is executed or paint() is empty and regardless of that the content gets drawn?

zeroknowledge
13th May 2011, 16:21
If it is never called this means that your model is not editable! Do you return proper flags for: QAbstractItemModel::flags?


You made my day! Thanks a lot. :)

I read the docs several times but somehow managed to completely miss flags(). I've already implemented setEditorData and setModelData (just omitted them from my code). I'll try to be move observant while perusing the docs next time.