How to use QTableView with SpinBoxes?
-
Hello,
I'm desperately trying to implement SpinBoxes within a QTableView. I have a working GUI Application where I would like to integrate it inside a Tab Widget.
I followed one of the countless tutorials and it kind of works. When I double click on the table I get a SpinBox BUT it is disabled (the controls cannot be clicked) and Qt automatically fills in the minimum value specified in the SpinBoxDelegate class.
See attached gif for more details:For reference, here is the code I'm using:
SpinBoxDelegate.h:
#pragma once #include <QStyledItemDelegate> class SpinBoxDelegate : public QStyledItemDelegate { Q_OBJECT public: SpinBoxDelegate(QObject* parent = nullptr); 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; void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override; };
SpinBoxDelegate.cpp:
#include "SpinBoxDelegate.h" #include <QSpinBox> /* Delegates are often stateless. The constructor only needs to call the base class's constructor with the parent QObject as its argument. */ SpinBoxDelegate::SpinBoxDelegate(QObject* parent) : QStyledItemDelegate(parent) { } /* The createEditor() function returns an editor widget, in this case a spin box that restricts values from the model to integers from 0 to 100 inclusive. */ QWidget* SpinBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /* option */, const QModelIndex& /* index */) const { QSpinBox* editor = new QSpinBox(parent); editor->setFrame(false); editor->setMinimum(30); editor->setMaximum(-30); return editor; } /* The setEditorData() function reads data from the model, converts it to an integer value, and writes it to the editor widget. */ void SpinBoxDelegate::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); } /* The setModelData() function reads the contents of the spin box, and writes it to the model. */ void SpinBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { QSpinBox* spinBox = static_cast<QSpinBox*>(editor); /* We call interpretText() to make sure that we obtain the most up-to-date value in the spin box. */ spinBox->interpretText(); int value = spinBox->value(); model->setData(index, value, Qt::EditRole); } /* The updateEditorGeometry() function updates the editor widget's geometry using the information supplied in the style option. This is the minimum that the delegate must do in this case.*/ void SpinBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex&/* index */) const { editor->setGeometry(option.rect); }
Main application window constructor:
/* Creating and assigning a model */ pModel = new QStandardItemModel(122, 2, this); ui.tableView->setModel(pModel); /* Assigning a delegator */ pDelegate = new SpinBoxDelegate(this); ui.tableView->setItemDelegateForColumn(0, pDelegate); /* Initializing the all entries to zero */ for (int row = 0; row < pModel->rowCount(); ++row) { for (int col = 0; col < pModel->columnCount(); ++col) { QModelIndex index = pModel->index(row, col, QModelIndex()); pModel->setData(index, 0); } }
I really don't know what else to try, would be great if somebody could help me.
-
@Stonebull said in How to use QTableView with SpinBoxes?:
Do you know what goes wrong
Well, 30 > -30, but you set maximum as -30 and minimum as 30. Means: your minimum is bigger than maximum...
-
@Stonebull said in How to use QTableView with SpinBoxes?:
editor->setMinimum(30); editor->setMaximum(-30);
I would start by looking at these two lines of code....
-
@JonB thanks for your reply.
@JonB said in How to use QTableView with SpinBoxes?:
I would start by looking at these two lines of code....
I followed the approach explained on the official Qt website:
https://doc.qt.io/archives/qt-4.8/qt-itemviews-spinboxdelegate-example.htmland by this guys here (which is exactly the same as the offitial approach)
https://programmer.group/using-various-custom-delegates-in-qtableview.htmlDo you know what goes wrong and are giving me a hint or is it just an advice where you would start to search?
The code is pretty short and I cannot think of what I did wrong in this section.Best Regards,
-
@Stonebull said in How to use QTableView with SpinBoxes?:
Do you know what goes wrong
Well, 30 > -30, but you set maximum as -30 and minimum as 30. Means: your minimum is bigger than maximum...