1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include "TableDelegate.h"
//test
#include <QLineEdit>
#include <iostream>
void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
//Je le laisse faire le paint
QStyledItemDelegate::paint(painter,option,index);
}
QWidget* TableDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
// Création de l'éditeur, ici un simple QLineEdit
QLineEdit *lineEdit = new QLineEdit(parent);
return lineEdit;
}
void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
//Je l'initialise avec la valeur de type QString issue du model
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
if(lineEdit){
QString myCellText = index.model()->data(index).toString();
lineEdit->setText(myCellText);
}
}
void TableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
//je renvoi la valeur au model
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
if(lineEdit){
model->setData(index,lineEdit->text());
}
}
void TableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{
Q_UNUSED(index);
editor->setGeometry(option.rect);
} |
Partager