标签:tableview png item column inter cas parent view setvalue
class SpinBoxDeegate1 :public QStyledItemDelegate
{
Q_OBJECT
public:
SpinBoxDeegate1(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;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
SpinBoxDeegate1::SpinBoxDeegate1(QObject* parent /*= 0*/)
{
}
QWidget* SpinBoxDeegate1::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setFrame(false);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
}
void SpinBoxDeegate1::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::EditRole).toInt();
QSpinBox *spinBox = static_cast < QSpinBox* > (editor);
spinBox->setValue(value);
}
void SpinBoxDeegate1::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, Qt::EditRole);
}
void SpinBoxDeegate1::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
main.cpp:
QTableView table;
QStandardItemModel model(4, 2);
SpinBoxDeegate1 delegate;
table.setModel(&model);
table.setItemDelegate(&delegate);
table.horizontalHeader()->setStretchLastSection(true);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 2; ++column) {
QModelIndex index = model.index(row, column, QModelIndex());
model.setData(index, QVariant((row + 1) * (column + 1)));
}
}
table.show();
标签:tableview png item column inter cas parent view setvalue
原文地址:http://www.cnblogs.com/che1234/p/7440186.html