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

    0_1536607313615_yhrkC.jpg


  • Moderators

    You should take a look at QTableWidget::setCellWidget - http://doc.qt.io/qt-5/qtablewidget.html#setCellWidget



  • not helpful any thing else


  • Moderators

    @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 setCellWidget is a Q_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?



  • @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 paint and fill in manually a QStyleOptionComboBox. 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));


  • Moderators

    @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
    0_1536923921209_bd4db172-1cbd-4764-82f0-39f847677c0d-image.png



  • It's all easy to solve.

    • qobject_cast<QSpinBox> should be qobject_cast<QSpinBox*>
    • option.rect() should be option.rect


  • @VRonin
    0_1536954591996_4322db11-0dfc-4cef-974d-ab2e61c65da5-image.png
    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
    0_1536954591996_4322db11-0dfc-4cef-974d-ab2e61c65da5-image.png
    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 be ui->tableView or even a simple this 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



  • @VRonin give me an example please


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.