How to add a spinbox column and combo box column in QTableWidget or QTableView?
-
@VRonin said in [How to add a spinbox column and combo box column in QTableWidget or QTableView?]:
When did you switch to the dark side @Christian-Ehrlicher ?
but you also have to reimplement paint and fill in manually a QStyleOptionComboBox
Because of this stupid limitation of the QStyledItemDelegate - I can't show the editor the whole time without custom painting and the up/down spinboxes also don't work well if the item doesn't have the focus. Maybe a task for you for Qt6 ;)
-
@Christian-Ehrlicher Fair enough, when the widget to paint becomes complex the process is a nightmare, I agree.
For a quick and dirty workaround without using
setItemWidget
, you can check https://pastebin.com/XrppLZ3m -
@VRonin said in How to add a spinbox column and combo box column in QTableWidget or QTableView?:
class SpinBoxDelegate : public QStyledItemDelegate{
Q_OBJECT
Q_DISABLE_COPY(SpinBoxDelegate)
public:
explicit SpinBoxDelegate(QObject* parent = Q_NULLPTR)
:QStyledItemDelegate(parent)
{}
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
{
Q_UNUSED(option)
Q_UNUSED(index)
return new QSpinBox(parent);
}
void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE
{
qobject_cast<QSpinBox>(editor)->setValue(index.data().toInt());
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const Q_DECL_OVERRIDE
{
model->setData(index,qobject_cast<QSpinBox>(editor)->value());
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
{
Q_UNUSED(index)
editor->setGeometry(option.rect());
}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE
{
qApp->style()->sizeFromContents(
QStyle::CT_SpinBox,
&option,
QSize(option.fontMetrics.horizontalAdvance(displayText(index.data(),option.locale)), option.fontMetrics.height())
);
}
};Alot of errors
-
@VRonin
error: use of undeclared identifier 'tableView' -
This post is deleted!
-
@davidlabib said in How to add a spinbox column and combo box column in QTableWidget or QTableView?:
@VRonin
error: use of undeclared identifier 'tableView'°_°
Did you even try to have a guess at what's wrong?! It's super easy. instead of
tableView
it should beui->tableView
or even a simplethis
will work -
@VRonin how can i make the spinbox always visible
-
You need to reimplement
paint
, fill a QStyleOptionSpinBox and use the style to paint the control