码迷,mamicode.com
首页 > 其他好文 > 详细

QT委托demo

时间:2017-08-27 14:02:49      阅读:195      评论:0      收藏:0      [点我收藏+]

标签: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();

技术分享

 

QT委托demo

标签:tableview   png   item   column   inter   cas   parent   view   setvalue   

原文地址:http://www.cnblogs.com/che1234/p/7440186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!