#include "x_spinboxdelegate.h" /* A delegate that allows the user to change integer values from the model using a spin box widget. */ #include SpinBoxDelegate::SpinBoxDelegate(QObject *parent, int iFlag) : QStyledItemDelegate(parent) { m_iFlag=iFlag; } QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const { if(index.column()!=0) return 0; QSpinBox *editor = new QSpinBox(parent); editor->setFrame(false); editor->setMinimum(0); if(m_iFlag==0) editor->setMaximum(100);//亮度最大值 else if(m_iFlag==1) editor->setMaximum(15);//音量最大值 editor->setAlignment(Qt::AlignHCenter); return editor; } void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{ int value = index.model()->data(index, Qt::EditRole).toInt(); QSpinBox *spinBox = static_cast(editor); spinBox->setValue(value); } void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ QSpinBox *spinBox = static_cast(editor); spinBox->interpretText(); int value = spinBox->value(); model->setData(index, value, Qt::EditRole); } void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const { editor->setGeometry(option.rect); }