How to add a spinbox column and combo box column in QTableWidget or QTableView?
-
How to add a spinbox column and combo box column in QTableWidget
like this
-
You should take a look at QTableWidget::setCellWidget - http://doc.qt.io/qt-5/qtablewidget.html#setCellWidget
-
not helpful any thing else
-
@davidlabib said in How to add a spinbox column and combo box column in QTableWidget or QTableView?:
not helpful any thing else
Why not? You want a widget in a QTableWidget and that's the function you need for it...
-
@davidlabib said in How to add a spinbox column and combo box column in QTableWidget or QTableView?:
not helpful any thing else
Why not? You want a widget in a QTableWidget and that's the function you need for it...
When did you switch to the dark side @Christian-Ehrlicher ? The only good
setCellWidgetis aQ_DECL_DEPRECATED void setCellWidget@davidlabib The jedi answer is to use a
QStyledItemDelegatesubclass. Would you want the Spin/Combo box to be always visible or just while you are editing? -
@VRonin said in How to add a spinbox column and combo box column in QTableWidget or QTableView?:
Would you want the Spin/Combo box to be always visible or just while you are editing?
combobox always visible , spinbox while editing
-
@VRonin said in How to add a spinbox column and combo box column in QTableWidget or QTableView?:
Would you want the Spin/Combo box to be always visible or just while you are editing?
combobox always visible , spinbox while editing
@davidlabib said in How to add a spinbox column and combo box column in QTableWidget or QTableView?:
spinbox while editing
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()) ); } };combobox always visible
same as above but you also have to reimplement
paintand fill in manually aQStyleOptionComboBox. It's tedious but if you get stuck I'm happy to help.To use the delegate you can call something like
tableView->setItemDelegateForColumn(0,new SpinBoxDelegate(tableView)); -
@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 ;)
-
@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 -
@davidlabib said in How to add a spinbox column and combo box column in QTableWidget or QTableView?:
spinbox while editing
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()) ); } };combobox always visible
same as above but you also have to reimplement
paintand fill in manually aQStyleOptionComboBox. It's tedious but if you get stuck I'm happy to help.To use the delegate you can call something like
tableView->setItemDelegateForColumn(0,new SpinBoxDelegate(tableView));@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!
-
@VRonin

error: use of undeclared identifier 'tableView'@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
tableViewit should beui->tableViewor even a simplethiswill 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 -
You need to reimplement
paint, fill a QStyleOptionSpinBox and use the style to paint the control@VRonin give me an example please