Ejemplo de delegado de QTableview
DateDelegate m_dateDelegate;
ComboDelegate m_comboDelegate;
SpinDelegate m_spinDelegate
CheckBoxDelegate m_CheckBoxDelegate; > //tableview agregar delegado
m_modle = new QStandardItemModel(this);
setModel(m_modle);
m_modle-gt; /p>
m_modle-gt; setHeaderData(0, Qt:: Horizontal, "Nombre"); //
m_modle-gt; setHeaderData(1, Qt:: Horizontal, "Cumpleaños") ;
m_modle-gt; setHeaderData(2, Qt:: Horizontal, "Trabajo");
m_modle-gt; setHeaderData(3, Qt:: Horizontal, "Ingresos") ;
m_modle-gt; setHeaderData(4, Qt::Horizontal, "NombreHorizontal, "Ingresos");
m_modle-gt; setHeaderData(4, Qt::Horizontal, " yes1");
m_modle-gt; setHeaderData(5, Qt::Horizontal, "yes2");
this-gt; horizontalHeader()-gt; setSectionResizeMode(QHeaderView: : Stretch);
setItemDelegateForColumn(1, amp; m_ dateDelegate);
setItemDelegateForColumn(2, amp; m_comboDelegate);
setItemDelegateForColumn(3, amp; m_spinDelegate);
setItemDelegateForColumn(4, amp; m_CheckBoxDelegate); //haga doble clic o seleccione para mostrar, no usado
m_modle-gt; setItem(0, 0, new QStandardItem("Tom" ));
m_modle-gt; setItem(0, 1, nuevo QStandardItem("1977-01-05"));
m_modle-gt; setItem(0, 2, nuevo QStandardItem(" Trabajador"));
m_modle-gt; setItem(0, 3, new QStandardItem("1500"));
//m_modle-gt; indexFromItem()
establecerInde
xWidget(m_modle-gt; index(0, 5), nuevo QCheckBox(this));
//prototipo de clase
#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H
#include
#include
clase DateDelegate: público QItemDelegate
{
Q_OBJECT? p>
público:
DateDelegate(QObject *parent = 0) :?QItemDelegate(parent) {}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem & option, const QModelIndex amp; index) const
{
QDateTimeEdit *editor = new QDateTimeEdit(parent);
editor-gt; dd");
editor-gt; setCalendarPopup(true);
editor-gt; installEventFilter(const_cast(this));
devolver editor;
}
void setEditorData(QWidget *editor, const QModelIndex amp; index) const
{
QString dateStr= index.model ( )-gt; data(index).toString();
QDate fecha = QDate::fromString(dateStr, Qt::ISODate);
QDateTimeEdit *edit=static_cast( editor
edit-gt; setDate(fecha);
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex amp; index ); const
{
QDateTimeEdit *edit=static_cast(editor);
QDate fecha = editar-gt(); model-gt; setData(index, QVariant(date.toString(Qt:.ISODate));
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem & option, const QModelIndex amp; índice) const
>
{
editor-gt; setGeometry(opción.rect);
}
}; /DATEDELEGATE_H
#ifndef COMBODELEGATE_ H
#define COMBODELEGATE_H
#include lt;QComboBoxgt;
#include lt;QItemDelegategt;
clase?ComboDelegate:? público?QItemDelegate
{
Q_OBJECT
público:
ComboDelegate(QObject * padre = 0): QItemDelegate(padre) { }
QWidget *createEditor(QWidget *padre, ? const QStyleOptionViewItem y opción,
const?QModelIndex & index)?const
{
Lista QString;
lista lt; "Trabajador" lt; ";
QComboBox *editor = nuevo QComboBox(padre);
editor-gt; addItems(lista);
editor-gt; installEventFilter( const_cast( this));
editor de retorno
}
void?setEditorData(QWidget *editor, QModelIndex amp; index)? > {
QString dateStr = index.model()-gt; data(index).toString()
QComboBox *box = static_cast(editor);
<; p> int i = box-gt; findText(dateStr);box-gt; setCurrentIndex(i);
}
void? *editor, QAbstractItemModel * modelo,
const?QModelIndex & index)? const
{
QComboBox *box = static_cast(editor);
QString str = box-gt; currentText();
model-gt; setData(index, str);
}
void? (QWidget *editor,
con
st?QStyleOptionViewItem y opción,?QModelIndex e índice) const
{
editor-gt;setGeometry(option.rect);
}
p>
};
#endif // COMBODELEGATE_(QWidget *editor, amp; opción);
#endif // COMBODELEGATE_(QWidget *editor, amp); ; opción) ;
#endif H
#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H
#include
#include
clase SpinDelegate: público QItemDelegate
{
Q_OBJECT
público:
SpinDelegate(QObject * padre = 0 ): QItemDelegate(padre){}
QWidget * createEditor(QWidget *padre, const QStyleOptionViewItem y opción, const QModelIndex e índice) const
{
QSpinBox *editor = nuevo QSpinBox(padre);
editor-gt; setRange(0, 10000
editor-gt; /p> p>
editor de retorno
}
void setEditorData(QWidget *editor, const QModelIndex amp; index) const
{ p>
int valor = index.model()-gt; data(index).toInt();
QSpinBox *box = static_cast(editor
box-); gt; setValue (valor);
}
void setModelData(QWidget * editor, QAbstractItemModel *model, const QModelIndex amp; index) const
{ p>
QSpinBox *box = static_cast(editor);
? int valor = box-gt;
setData(índice, valor)
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem y opción, const QMode);
lÍndice amp; índice) const
{
editor-gt; setGeometry(option.rect); /p>
#endif // SPINDELEGATE_H
#ifndef CHECKBOXDELEGATE_H
#define CHECKBOXDELEGATE_H
#include
#include
clase CheckBoxDelegate: público QItemDelegate
{
Q_OBJECT
público:
CheckBoxDelegate(QObject *parent = 0 ): QItemDelegate(padre){}
QWidget * createEditor(QWidget *padre, const QStyleOptionViewItem y opción, const QModelIndex e índice) const
{
QCheckBox *editor = new QCheckBox(parent);
editor-gt; installEventFilter(const_cast(this));
return editor; //haga doble clic o seleccione para mostrar
}
void setEditorData(QWidget *editor, const QModelIndex amp; index) const
{
valor bool =index.model( )- gt; datos(índice).toBool();
QCheckBox *box = static_cast(editor);
box-gt; >
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex amp; index) const
{
? >
? valor bool = box-gt;
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem y opción, const QModelIndex e índice) const
{
editor-gt;
}
}
};
#endif // COMPROBAR
OXDELEGATE_H