Создал такую конструкцию на основе SpinBox. Сам делегат:
CSpinBoxDelegate::CSpinBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
//------------------------------------------------------------------------------
CSpinBoxDelegate::CSpinBoxDelegate
(
int iMinEx,
int iMaxEx,
int iStepEx,
QObject *parent
): QItemDelegate(parent)
{
iMin = iMinEx;
iMax = iMaxEx;
iStep = iStepEx;
}
//------------------------------------------------------------------------------
void CSpinBoxDelegate::SetParametrs
(
int iMinEx,
int iMaxEx,
int iStepEx
)
{
iMin = iMinEx;
iMax = iMaxEx;
iStep = iStepEx;
}
//------------------------------------------------------------------------------
QWidget* CSpinBoxDelegate::createEditor
(
QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &index
) const
{
if (index.column()==0)
return 0;
QSpinBox *pSpinBox = new QSpinBox(parent);
pSpinBox->setMinimum(iMin);
pSpinBox->setMaximum(iMax);
pSpinBox->setSingleStep(iStep);
return pSpinBox;
}
//------------------------------------------------------------------------------
void CSpinBoxDelegate::setEditorData
(
QWidget *editor,
const QModelIndex &index
) const
{
if (index.column()==0)
return;
int value = index.model()->data(index).toInt();
QSpinBox *pSpinBox = static_cast<QSpinBox*>(editor);
pSpinBox->setValue(value);
}
//------------------------------------------------------------------------------
void CSpinBoxDelegate::setModelData
(
QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index
) const
{
if (index.column()==0)
return;
QSpinBox *pSpinBox = static_cast<QSpinBox*>(editor);
pSpinBox->interpretText();
int value = pSpinBox->value();
model->setData(index, value);
}
//------------------------------------------------------------------------------
void CSpinBoxDelegate::updateEditorGeometry
(
QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index
) const
{
if (index.column()==0)
return;
editor->setGeometry(option.rect);
}
Вставил её в QTreeWidget.
QStringList lst;
lst << tr("Свойство") << tr("Значение");
QTreeWidget* tw;
tw = new QTreeWidget;
tw->setHeaderLabels(lst);
QTreeWidgetItem* pItem;
pItem->setText(0, tr("Значение"));
pItem->setFlags(Qt::ItemIsEditable|Qt::ItemIsEnabled);
CSpinBoxDelegate * sb = new CSpinBoxDelegate();
tw->setItemDelegateForRow(1, sb);
Как мне отловить изменение значения в ячейке?