PDA

View Full Version : QItemDelegate painting widget in cell 0,0 regardless of selected cell



Bodger
17th July 2011, 13:54
My first post so apologies if I have fractured any rules!!

I have created a custom delegate from QItemDelegate, i have reimplemented the virtual functions to createEditor() to create the widget, setEditorData() and set the setModalData().

It all works well for cell 0,0 but when trying to edit other cells, 0,1 for example, the editing widget is painted into cell 0,0 Once the edit (completed in cell 0,0) is complete the correct value is entered into cell 0,1.

If someone can explain why the widget is always painted to cell 0,0 or provide a link to something that explains my problem I would be very grateful.


QWidget *tDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{

QComboBox *editor = new QComboBox(parent);
QStringList tempUnits;
tempUnits << "Celcius" << "Fahrenhiet" << "Rankin" << "Delisle" << "Newton" << "Reaumur" << "Romer" << "Kelvin";

for (int i=0; i<tempUnits.size();i++)
{
editor->addItem(tempUnits.at(i));

}

editor->setCurrentIndex(0);
return editor;


}


void tDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{


QString value = index.model()->data(index,Qt::EditRole).toString();
QComboBox *combo = qobject_cast<QComboBox*>(editor);
Q_ASSERT(combo);
combo->setCurrentIndex(combo->findText(value));


}

ChrisW67
17th July 2011, 23:53
Have you implemented an override for QStyledItemDelegate::updateEditorGeometry() ? It need not be complicated, but the default does nothing and might be leading to the symptoms you describe.

Also, you can construct the combo box from the QStringList directly and dispense with the explicit loop.

BTW: You have spelt "Celsius" and "Fahrenheit" incorrectly.

Bodger
18th July 2011, 00:25
Chris,

Your programming skills are far better than my spelling!!!

I had indeed created the updateEditorGeometry() method but not populated it, deleting said method has fixed the problem.

Thanks very much for your assistance, much appreciated.