qt/LedOK/base/x_spinboxdelegate.cpp

36 lines
1.4 KiB
C++
Raw Normal View History

2022-01-04 18:11:48 +08:00
#include "x_spinboxdelegate.h"
/*
A delegate that allows the user to change integer values from the model
using a spin box widget.
*/
#include <QSpinBox>
2022-08-25 18:37:24 +08:00
SpinBoxDelegate::SpinBoxDelegate(QObject *parent, int iFlag) : QStyledItemDelegate(parent) {
2022-01-04 18:11:48 +08:00
m_iFlag=iFlag;
}
2022-08-25 18:37:24 +08:00
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;
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
2022-01-04 18:11:48 +08:00
int value = index.model()->data(index, Qt::EditRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}
2022-08-25 18:37:24 +08:00
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
2022-01-04 18:11:48 +08:00
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
}
2022-08-25 18:37:24 +08:00
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const {
2022-01-04 18:11:48 +08:00
editor->setGeometry(option.rect);
}