Hello all,
In a project. I have a tablefield with a QSqlTableModel. Depending on the columns in the select I have different in a the tablefield column.
The editor for that column can be a QLineEdit or a QTimeEdit.
This worked well on the desktop but now I've ported the to the N900.
What I see on the N900 is a thin line in the editor that supose to be my data. The editor accepts input correctly. But I only see this thin line.
Question what am I doing wrong?
Code:class MyDelegate : public QStyledItemDelegate { Q_OBJECT public: explicit MyDelegate(QObject *parent = 0); QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; }; MyDelegate::MyDelegate(QObject *parent) : QStyledItemDelegate(parent) { } QWidget* MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { const MyModel* coModel = qobject_cast<const MyModel*> (index.model()); if(coModel == 0) { return QStyledItemDelegate::createEditor(parent, option, index); } // only columns 1 & 2 are editable. 0 is not editable switch(index.column()) { case 1: // the other table { MyModel* oModel = const_cast<MyModel*>(coModel); QComboBox* oBox(new QComboBox(parent)); oBox->setModel(oModel->otherTable()); oBox->setModelColumn(otherTableModel::NAME); return oBox; } break; case 2: // the choice fields { QWidget* oEditor(0); if(coModel->lastColumnName() == "Texts") { oEditor = new QLineEdit(parent); } else { oEditor = new QTimeEdit(parent); oEditor->setDisplayFormat("hh:mm:ss"); } return oEditor; } break; } return QStyledItemDelegate::createEditor(parent, option, index); } void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { const MyModel* coModel = qobject_cast<const MyModel*> (index.model()); if(coModel == 0) { QStyledItemDelegate::setEditorData(editor, index); return; } switch(index.column()) { case 1: // the other table { QComboBox* oBox = qobject_cast<QComboBox*>(editor); if(oBox == 0) { return; } oBox->setCurrentIndex(coModel->data(index, Qt::EditRole).toInt()); break; } case 2: // the choice fields { QTimeEdit* oTimeEdit = qobject_cast<QTimeEdit*>(editor); if(oTimeEdit != 0) { // In Qt::DisplayRole the time is a QString oTimeEdit->setTime(coModel->data(index, Qt::EditRole).toTime()); return; } QLineEdit* oLineEdit = qobject_cast<QLineEdit*>(editor); if(oLineEdit != 0) { // In Qt::DisplayRole the text is the same oLineEdit->setText(coModel->data(index, Qt::EditRole).toInt()); return; } } break; default: QStyledItemDelegate::setEditorData(editor, index); } } void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { MyModel* oModel = qobject_cast<MyModel*> (model); if(oModel == 0) { QStyledItemDelegate::setModelData(editor, model, index); return; } switch(index.column()) { case 1: // the other table { QComboBox* oBox = qobject_cast<QComboBox*>(editor); if(oBox == 0) { return; } int iTableId(oModel->otherTable()->id(oBox->currentIndex())); oModel->setData(index, iTableId, Qt::EditRole); break; } case 2: // the choice fields { QTimeEdit* oTimeEdit = qobject_cast<QTimeEdit*>(editor); if(oTimeEdit != 0) { oModel->setData(index, oTimeEdit->time(), Qt::EditRole); } QLineEdit* oLineEdit = qobject_cast<QLineEdit*>(editor); if(oLineEdit != 0) { oModel->setData(index, oLineEdit->text(), Qt::EditRole); } } break; default: QStyledItemDelegate::setEditorData(editor, index); break; } }

Reply With Quote

