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:
2026-02-10 11:52:35 +01:00
parent bdc8075324
commit f148ef9cba
7 changed files with 126 additions and 2 deletions

View File

@ -35,6 +35,7 @@ if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
dialogs/settingsdialog.h dialogs/settingsdialog.cpp
views/itemdetailmapper.h views/itemdetailmapper.cpp
widgets/comboboxdelegate.h widgets/comboboxdelegate.cpp
widgets/spinboxdelegate.h widgets/spinboxdelegate.cpp
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET ${TARGET_APP} APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR

View File

@ -18,6 +18,7 @@
#include "model/metadata.h"
#include "model/tablemodel.h"
#include "widgets/comboboxdelegate.h"
#include "widgets/spinboxdelegate.h"
static QStandardPaths::StandardLocation standardLocation = QStandardPaths::HomeLocation;
static QString updateTextClean = "Do you want to update the application now?";
@ -346,6 +347,15 @@ void MainWindow::setupModelViews() {
// ui->tableView->setModel(m_tableModel.get());
m_proxyModel = m_core->getSortFilterModel();
/// setting number delegates to combo boxes
SpinboxDelegate* spinboxDelegate = new SpinboxDelegate(this);
ui->tableView->setItemDelegateForColumn(0, spinboxDelegate);
ui->tableView->setItemDelegateForColumn(4, spinboxDelegate);
ui->tableView->setItemDelegateForColumn(6, spinboxDelegate);
ui->tableView->setItemDelegateForColumn(7, spinboxDelegate);
ui->tableView->setItemDelegateForColumn(8, spinboxDelegate);
/// setting type delegates to combo boxes
ComboboxDelegate* shareTypeDelegate = new ComboboxDelegate(SHARE_TYPES, this);
ComboboxDelegate* biddingTypeDelegate = new ComboboxDelegate(BIDDING_TYPES, this);
ui->tableView->setItemDelegateForColumn(3, shareTypeDelegate);

View File

@ -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

View 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);
}

View 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

View File

@ -19,6 +19,13 @@ QVariant ModelItem::data(int role) const {
case JsonObjectRole:
return toJsonObject();
break;
case MembershipNumberRole:
case ShareAmountRole:
case Bidding1Role:
case Bidding2Role:
case Bidding3Role:
return getValueButReplaceZeroValueWithEmptyString(role);
break;
default:
return m_values.value(role);
}
@ -95,3 +102,12 @@ QJsonObject ModelItem::toJsonObject() const {
}
return itemObject;
}
QVariant ModelItem::getValueButReplaceZeroValueWithEmptyString(const int role) const {
QVariant localValue = m_values.value(role, QVariant());
if (localValue == 0) {
return QVariant();
} else {
return localValue;
}
}

View File

@ -20,6 +20,8 @@ class ModelItem {
private:
ModelItemValues m_values;
QVariant getValueButReplaceZeroValueWithEmptyString(const int role) const;
};
#endif // MODELITEM_H