Использование QItemDelegate для строки или столбца в QTableView

Модератор: Модераторы разделов

chen
Сообщения: 8

Использование QItemDelegate для строки или столбца в QTableView

Сообщение chen »

Доброго времени суток!
У меня следующий вопрос: В примере Spin Box Delegate Example, который доступен из справки в классе делегата заменил QSpinBox на QCheckBox
/****************************************************************************
#include <QtGui>

#include "delegate.h"

SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}

QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QCheckBox *editor = new QCheckBox(parent);
//editor->setMinimum(0);
//editor->setMaximum(100);
editor->installEventFilter(const_cast<SpinBoxDelegate*>(this));

return editor;
}

void SpinBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
// int value = index.model()->data(index, Qt::DisplayRole).toInt();

//QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
//spinBox->setValue(value);
}

void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
//QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
//spinBox->interpretText();
//int value = spinBox->value();

//model->setData(index, value);
}

void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}


в случае если делегат устанавливается функцией tableView.setItemDelegate(&delegate); при редактировании элемента появляется CheckBox, а если установит только для 1-го столбца tableView.setItemDelegateForColumn(1, &delegate);, то никакой реакции, для всех элементов появляется редактор по умолчаниюSpinBox().
Так вот вопрос в том как сделать чтобы делегат устанавливался только для выбранного столбца?
Спасибо сказали: