How to add a spinbox column and combo box column in QTableWidget or QTableView?
-
wrote on 10 Sept 2018, 19:22 last edited by A Former User 9 Oct 2018, 20:14
-
You should take a look at QTableWidget::setCellWidget - http://doc.qt.io/qt-5/qtablewidget.html#setCellWidget
-
wrote on 10 Sept 2018, 21:38 last edited by
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...
wrote on 11 Sept 2018, 07:27 last edited byWhen did you switch to the dark side @Christian-Ehrlicher ? The only good
setCellWidget
is aQ_DECL_DEPRECATED void setCellWidget
@davidlabib The jedi answer is to use a
QStyledItemDelegate
subclass. Would you want the Spin/Combo box to be always visible or just while you are editing? -
wrote on 11 Sept 2018, 12:20 last edited by
@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
wrote on 11 Sept 2018, 18:44 last edited by VRonin 9 Nov 2018, 18:48@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
paint
and 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 ;)
wrote on 11 Sept 2018, 19:01 last edited by@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
paint
and 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));
wrote on 14 Sept 2018, 11:18 last edited by@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())
);
}
}; -
wrote on 14 Sept 2018, 12:31 last edited by
It's all easy to solve.
qobject_cast<QSpinBox>
should beqobject_cast<QSpinBox*>
option.rect()
should beoption.rect
-
wrote on 14 Sept 2018, 19:50 last edited by
@VRonin
error: use of undeclared identifier 'tableView' -
wrote on 14 Sept 2018, 20:28 last edited by A Former UserThis post is deleted!
-
@VRonin
error: use of undeclared identifier 'tableView'wrote on 18 Sept 2018, 09:35 last edited by VRonin@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 -
wrote on 18 Nov 2018, 23:16 last edited by A Former User
@VRonin how can i make the spinbox always visible
-
wrote on 19 Nov 2018, 08:22 last edited by
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 controlwrote on 19 Nov 2018, 10:32 last edited by@VRonin give me an example please