Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add a spinbox column and combo box column in QTableWidget or QTableView?
Forum Updated to NodeBB v4.3 + New Features

How to add a spinbox column and combo box column in QTableWidget or QTableView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 6.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    How to add a spinbox column and combo box column in QTableWidget
    like this

    0_1536607313615_yhrkC.jpg

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        not helpful any thing else

        1 Reply Last reply
        -1
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          VRoninV 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @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...

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            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?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @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

              VRoninV 1 Reply Last reply
              0
              • ? A Former User

                @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

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                @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));

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                ? 1 Reply Last reply
                1
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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 ;)

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  VRoninV 1 Reply Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    @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 ;)

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @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

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @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));

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      @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

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        It's all easy to solve.

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

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        2
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          @VRonin
                          0_1536954591996_4322db11-0dfc-4cef-974d-ab2e61c65da5-image.png
                          error: use of undeclared identifier 'tableView'

                          VRoninV 1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by A Former User
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • ? A Former User

                              @VRonin
                              0_1536954591996_4322db11-0dfc-4cef-974d-ab2e61c65da5-image.png
                              error: use of undeclared identifier 'tableView'

                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #14

                              @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

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              1 Reply Last reply
                              3
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by A Former User
                                #15

                                @VRonin how can i make the spinbox always visible

                                1 Reply Last reply
                                0
                                • VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #16

                                  You need to reimplement paint, fill a QStyleOptionSpinBox and use the style to paint the control

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  ? 1 Reply Last reply
                                  3
                                  • VRoninV VRonin

                                    You need to reimplement paint, fill a QStyleOptionSpinBox and use the style to paint the control

                                    ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by
                                    #17

                                    @VRonin give me an example please

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved