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 use QTableView with SpinBoxes?
Forum Updated to NodeBB v4.3 + New Features

How to use QTableView with SpinBoxes?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 904 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Stonebull
    wrote on last edited by
    #1

    Hello,
    I'm desperately trying to implement SpinBoxes within a QTableView. I have a working GUI Application where I would like to integrate it inside a Tab Widget.
    I followed one of the countless tutorials and it kind of works. When I double click on the table I get a SpinBox BUT it is disabled (the controls cannot be clicked) and Qt automatically fills in the minimum value specified in the SpinBoxDelegate class.
    See attached gif for more details:

    QTableView.gif

    For reference, here is the code I'm using:

    SpinBoxDelegate.h:

    #pragma once
    
    #include <QStyledItemDelegate>
    
    class SpinBoxDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    
    public:
        SpinBoxDelegate(QObject* parent = nullptr);
    
        QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
            const QModelIndex& index) const override;
    
        void setEditorData(QWidget* editor, const QModelIndex& index) const override;
        void setModelData(QWidget* editor, QAbstractItemModel* model,
            const QModelIndex& index) const override;
    
        void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
            const QModelIndex& index) const override;
    };
    
    

    SpinBoxDelegate.cpp:

    #include "SpinBoxDelegate.h"
    #include <QSpinBox>
    
    /* Delegates are often stateless. The constructor only needs to call the base class's constructor with the parent QObject as its argument. */
    SpinBoxDelegate::SpinBoxDelegate(QObject* parent)
        : QStyledItemDelegate(parent)
    {
    
    }
    
    /* The createEditor() function returns an editor widget, in this case a spin box that restricts values from the model to integers from 0 to 100 inclusive. */
    QWidget* SpinBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /* option */, const QModelIndex& /* index */) const
    {
        QSpinBox* editor = new QSpinBox(parent);
        editor->setFrame(false);
        editor->setMinimum(30);
        editor->setMaximum(-30);
    
        return editor;
    }
    
    
    /* The setEditorData() function reads data from the model, converts it to an integer value, and writes it to the editor widget. */
    void SpinBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
    {
        int value = index.model()->data(index, Qt::EditRole).toInt();
    
        QSpinBox* spinBox = static_cast<QSpinBox*>(editor);
        spinBox->setValue(value);
    }
    
    /* The setModelData() function reads the contents of the spin box, and writes it to the model. */
    void SpinBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
    {
        QSpinBox* spinBox = static_cast<QSpinBox*>(editor);
    
        /* We call interpretText() to make sure that we obtain the most up-to-date value in the spin box. */
        spinBox->interpretText();
    
        int value = spinBox->value();
    
        model->setData(index, value, Qt::EditRole);
    }
    
    /* The updateEditorGeometry() function updates the editor widget's geometry using the information supplied in the style option. This is the minimum that the delegate must do in this case.*/
    void SpinBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex&/* index */) const
    {
        editor->setGeometry(option.rect);
    }
    

    Main application window constructor:

            /* Creating and assigning a model */
    	pModel = new QStandardItemModel(122, 2, this);
    	ui.tableView->setModel(pModel);
    
    	/* Assigning a delegator */
    	pDelegate = new SpinBoxDelegate(this);
    	ui.tableView->setItemDelegateForColumn(0, pDelegate);
    
    	/* Initializing the all entries to zero */
    	for (int row = 0; row < pModel->rowCount(); ++row) 
    	{
    		for (int col = 0; col < pModel->columnCount(); ++col) 
    		{
    			QModelIndex index = pModel->index(row, col, QModelIndex());
    			pModel->setData(index, 0);
    
    		}
    	}
    

    I really don't know what else to try, would be great if somebody could help me.

    JonBJ 1 Reply Last reply
    0
    • S Stonebull

      @JonB thanks for your reply.

      @JonB said in How to use QTableView with SpinBoxes?:

      I would start by looking at these two lines of code....

      I followed the approach explained on the official Qt website:
      https://doc.qt.io/archives/qt-4.8/qt-itemviews-spinboxdelegate-example.html

      and by this guys here (which is exactly the same as the offitial approach)
      https://programmer.group/using-various-custom-delegates-in-qtableview.html

      Do you know what goes wrong and are giving me a hint or is it just an advice where you would start to search?
      The code is pretty short and I cannot think of what I did wrong in this section.

      Best Regards,

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Stonebull said in How to use QTableView with SpinBoxes?:

      Do you know what goes wrong

      Well, 30 > -30, but you set maximum as -30 and minimum as 30. Means: your minimum is bigger than maximum...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • S Stonebull

        Hello,
        I'm desperately trying to implement SpinBoxes within a QTableView. I have a working GUI Application where I would like to integrate it inside a Tab Widget.
        I followed one of the countless tutorials and it kind of works. When I double click on the table I get a SpinBox BUT it is disabled (the controls cannot be clicked) and Qt automatically fills in the minimum value specified in the SpinBoxDelegate class.
        See attached gif for more details:

        QTableView.gif

        For reference, here is the code I'm using:

        SpinBoxDelegate.h:

        #pragma once
        
        #include <QStyledItemDelegate>
        
        class SpinBoxDelegate : public QStyledItemDelegate
        {
            Q_OBJECT
        
        public:
            SpinBoxDelegate(QObject* parent = nullptr);
        
            QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
                const QModelIndex& index) const override;
        
            void setEditorData(QWidget* editor, const QModelIndex& index) const override;
            void setModelData(QWidget* editor, QAbstractItemModel* model,
                const QModelIndex& index) const override;
        
            void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
                const QModelIndex& index) const override;
        };
        
        

        SpinBoxDelegate.cpp:

        #include "SpinBoxDelegate.h"
        #include <QSpinBox>
        
        /* Delegates are often stateless. The constructor only needs to call the base class's constructor with the parent QObject as its argument. */
        SpinBoxDelegate::SpinBoxDelegate(QObject* parent)
            : QStyledItemDelegate(parent)
        {
        
        }
        
        /* The createEditor() function returns an editor widget, in this case a spin box that restricts values from the model to integers from 0 to 100 inclusive. */
        QWidget* SpinBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /* option */, const QModelIndex& /* index */) const
        {
            QSpinBox* editor = new QSpinBox(parent);
            editor->setFrame(false);
            editor->setMinimum(30);
            editor->setMaximum(-30);
        
            return editor;
        }
        
        
        /* The setEditorData() function reads data from the model, converts it to an integer value, and writes it to the editor widget. */
        void SpinBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
        {
            int value = index.model()->data(index, Qt::EditRole).toInt();
        
            QSpinBox* spinBox = static_cast<QSpinBox*>(editor);
            spinBox->setValue(value);
        }
        
        /* The setModelData() function reads the contents of the spin box, and writes it to the model. */
        void SpinBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
        {
            QSpinBox* spinBox = static_cast<QSpinBox*>(editor);
        
            /* We call interpretText() to make sure that we obtain the most up-to-date value in the spin box. */
            spinBox->interpretText();
        
            int value = spinBox->value();
        
            model->setData(index, value, Qt::EditRole);
        }
        
        /* The updateEditorGeometry() function updates the editor widget's geometry using the information supplied in the style option. This is the minimum that the delegate must do in this case.*/
        void SpinBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex&/* index */) const
        {
            editor->setGeometry(option.rect);
        }
        

        Main application window constructor:

                /* Creating and assigning a model */
        	pModel = new QStandardItemModel(122, 2, this);
        	ui.tableView->setModel(pModel);
        
        	/* Assigning a delegator */
        	pDelegate = new SpinBoxDelegate(this);
        	ui.tableView->setItemDelegateForColumn(0, pDelegate);
        
        	/* Initializing the all entries to zero */
        	for (int row = 0; row < pModel->rowCount(); ++row) 
        	{
        		for (int col = 0; col < pModel->columnCount(); ++col) 
        		{
        			QModelIndex index = pModel->index(row, col, QModelIndex());
        			pModel->setData(index, 0);
        
        		}
        	}
        

        I really don't know what else to try, would be great if somebody could help me.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #2

        @Stonebull said in How to use QTableView with SpinBoxes?:

        editor->setMinimum(30);
        editor->setMaximum(-30);
        

        I would start by looking at these two lines of code....

        1 Reply Last reply
        5
        • S Offline
          S Offline
          Stonebull
          wrote on last edited by
          #3

          @JonB thanks for your reply.

          @JonB said in How to use QTableView with SpinBoxes?:

          I would start by looking at these two lines of code....

          I followed the approach explained on the official Qt website:
          https://doc.qt.io/archives/qt-4.8/qt-itemviews-spinboxdelegate-example.html

          and by this guys here (which is exactly the same as the offitial approach)
          https://programmer.group/using-various-custom-delegates-in-qtableview.html

          Do you know what goes wrong and are giving me a hint or is it just an advice where you would start to search?
          The code is pretty short and I cannot think of what I did wrong in this section.

          Best Regards,

          jsulmJ 1 Reply Last reply
          0
          • S Stonebull

            @JonB thanks for your reply.

            @JonB said in How to use QTableView with SpinBoxes?:

            I would start by looking at these two lines of code....

            I followed the approach explained on the official Qt website:
            https://doc.qt.io/archives/qt-4.8/qt-itemviews-spinboxdelegate-example.html

            and by this guys here (which is exactly the same as the offitial approach)
            https://programmer.group/using-various-custom-delegates-in-qtableview.html

            Do you know what goes wrong and are giving me a hint or is it just an advice where you would start to search?
            The code is pretty short and I cannot think of what I did wrong in this section.

            Best Regards,

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Stonebull said in How to use QTableView with SpinBoxes?:

            Do you know what goes wrong

            Well, 30 > -30, but you set maximum as -30 and minimum as 30. Means: your minimum is bigger than maximum...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • S Offline
              S Offline
              Stonebull
              wrote on last edited by Stonebull
              #5

              @JonB, @jsulm OMG you are right!!!! I kept looking at it withought realizing ...

              Yeah, that was it, thanks for your help!!

              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