Not displaying 0 values for int and double columns in table view, but still using a SpinBox delegate when editing.
This commit is contained in:
@ -31,9 +31,9 @@ QWidget* ComboboxDelegate::createEditor(QWidget* parent,
|
||||
void ComboboxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
|
||||
/// Get the value via index of the Model
|
||||
const QAbstractItemModel* localModel = index.model();
|
||||
QString headerText = localModel->headerData(index.column(), Qt::Horizontal).toString();
|
||||
const QString headerText = localModel->headerData(index.column(), Qt::Horizontal).toString();
|
||||
|
||||
UserRoles role = GET_ROLE_FOR_COLUMN(index.column());
|
||||
const UserRoles role = GET_ROLE_FOR_COLUMN(index.column());
|
||||
const bool isShareType = SHARE_TYPE_ROLES.contains(role);
|
||||
const bool isBiddingType = BIDDING_TYPE_ROLES.contains(role);
|
||||
/// Put the value into the SpinBox
|
||||
|
||||
69
UIs/BeetRoundWidgets/widgets/spinboxdelegate.cpp
Normal file
69
UIs/BeetRoundWidgets/widgets/spinboxdelegate.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include "spinboxdelegate.h"
|
||||
|
||||
#include <QSpinBox>
|
||||
|
||||
#include "model/metadata.h"
|
||||
|
||||
SpinboxDelegate::SpinboxDelegate(QObject* parent)
|
||||
: QStyledItemDelegate(parent) {}
|
||||
|
||||
void SpinboxDelegate::paint(QPainter* painter,
|
||||
const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const {
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
|
||||
QSize SpinboxDelegate::sizeHint(const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const {
|
||||
return QStyledItemDelegate::sizeHint(option, index);
|
||||
}
|
||||
|
||||
QWidget* SpinboxDelegate::createEditor(QWidget* parent,
|
||||
const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const {
|
||||
const QAbstractItemModel* localModel = index.model();
|
||||
QString headerText = localModel->headerData(index.column(), Qt::Horizontal).toString();
|
||||
|
||||
const UserRoles role = GET_ROLE_FOR_COLUMN(index.column());
|
||||
const bool isInt = INT_ROLES.contains(role);
|
||||
if (isInt) {
|
||||
QSpinBox* editor = new QSpinBox(parent);
|
||||
editor->setMinimum(0);
|
||||
editor->setMaximum(23000);
|
||||
return editor;
|
||||
} else {
|
||||
QDoubleSpinBox* editor = new QDoubleSpinBox(parent);
|
||||
editor->setMinimum(0);
|
||||
editor->setMaximum(23000);
|
||||
return editor;
|
||||
}
|
||||
// return QStyledItemDelegate::createEditor(parent, option, index);
|
||||
}
|
||||
|
||||
void SpinboxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
|
||||
// Get the value via index of the Model
|
||||
const QAbstractItemModel* localModel = index.model();
|
||||
QString headerText = localModel->headerData(index.column(), Qt::Horizontal).toString();
|
||||
|
||||
const UserRoles role = GET_ROLE_FOR_COLUMN(index.column());
|
||||
const bool isInt = INT_ROLES.contains(role);
|
||||
if (isInt) {
|
||||
int value = index.model()->data(index, Qt::EditRole).toInt();
|
||||
// Put the value into the SpinBox
|
||||
QSpinBox* spinbox = static_cast<QSpinBox*>(editor);
|
||||
spinbox->setValue(value);
|
||||
} else {
|
||||
// Put the value into the SpinBox
|
||||
qreal value = index.model()->data(index, Qt::EditRole).toReal();
|
||||
QDoubleSpinBox* spinbox = static_cast<QDoubleSpinBox*>(editor);
|
||||
spinbox->setValue(value);
|
||||
}
|
||||
|
||||
// QStyledItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
|
||||
void SpinboxDelegate::setModelData(QWidget* editor,
|
||||
QAbstractItemModel* model,
|
||||
const QModelIndex& index) const {
|
||||
QStyledItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
26
UIs/BeetRoundWidgets/widgets/spinboxdelegate.h
Normal file
26
UIs/BeetRoundWidgets/widgets/spinboxdelegate.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef SPINBOXDELEGATE_H
|
||||
#define SPINBOXDELEGATE_H
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
class SpinboxDelegate : public QStyledItemDelegate {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit SpinboxDelegate(QObject* parent = nullptr);
|
||||
|
||||
/// QAbstractItemDelegate interface
|
||||
public:
|
||||
void paint(QPainter* painter,
|
||||
const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const override;
|
||||
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||
QWidget* createEditor(QWidget* parent,
|
||||
const QStyleOptionViewItem& option,
|
||||
const QModelIndex& index) const override;
|
||||
void setEditorData(QWidget* editor, const QModelIndex& index) const override;
|
||||
void setModelData(QWidget* editor,
|
||||
QAbstractItemModel* model,
|
||||
const QModelIndex& index) const override;
|
||||
};
|
||||
|
||||
#endif // SPINBOXDELEGATE_H
|
||||
Reference in New Issue
Block a user