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 restrict the values in the QTableview only to positive using setData?
Forum Updated to NodeBB v4.3 + New Features

How to restrict the values in the QTableview only to positive using setData?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.5k 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.
  • R Offline
    R Offline
    Ratzz
    wrote on 17 Dec 2015, 13:41 last edited by
    #1

    I have a QStandardItemModel set to tableView. I want to restrict the values to only positive
    How to restrict the values in the QTableview only to positive using setData?

        QStandardItemModel *dataTableModel = new QStandardItemModel(8,4);
        ui->tableView_datatable->setModel(dataTableModel);
        int row = 8;
        int col = 4;
        for(int i = 0; i < col ; i++)
        {
            for(int j = 0; j< row ; j++)
            {
                QModelIndex index= dataTableModel->index(i,j,QModelIndex());
                dataTableModel->setData(index,i);
            }
        }
    

    --Alles ist gut.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 17 Dec 2015, 14:07 last edited by raven-worx
      #2

      you need to create a custom item delegate (derived from QStyledItemDelegate) and override it's createEditor() method.

      Following should do what you want:

      class MyItemDelegate : public QStyledItemDelegate
      {
          Q_OBJECT
      
      public:
          ...
      
          virtual QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
          {
                  QWidget* editor = QStyledItemDelegate::createEditor(parent, option, index);
                  if( QSpinBox* spinBox = qobject_cast<QSpinBox>(editor) )
                       spinBox->setMinimum(0);
                  return editor;
          }
      };
      

      And of course finally set it on your table view widget:

      ui->tableView_datatable->setItemDelegate( new MyItemDelegate(ui->tableView_datatable) );
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      R 1 Reply Last reply 18 Dec 2015, 04:36
      0
      • R raven-worx
        17 Dec 2015, 14:07

        you need to create a custom item delegate (derived from QStyledItemDelegate) and override it's createEditor() method.

        Following should do what you want:

        class MyItemDelegate : public QStyledItemDelegate
        {
            Q_OBJECT
        
        public:
            ...
        
            virtual QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
            {
                    QWidget* editor = QStyledItemDelegate::createEditor(parent, option, index);
                    if( QSpinBox* spinBox = qobject_cast<QSpinBox>(editor) )
                         spinBox->setMinimum(0);
                    return editor;
            }
        };
        

        And of course finally set it on your table view widget:

        ui->tableView_datatable->setItemDelegate( new MyItemDelegate(ui->tableView_datatable) );
        
        R Offline
        R Offline
        Ratzz
        wrote on 18 Dec 2015, 04:36 last edited by Ratzz
        #3

        @raven-worx
        Thanks for the reply.
        I do not have spinbox set to my table. Using setdata each cell of the tableturns out to be spinbox (not sure if it is spin box). can we do it without custom item delegate?
        It works with custom ItemDelegate. but i already have a delegate in my project with combobox

        QWidget *ComboBoxDelegate::createEditor(QWidget *parent,  const QStyleOptionViewItem & option , const QModelIndex & index ) const                               
         {
            QComboBox *editor = new QComboBox(parent);
            editor->addItems(comboItems);
            return editor;
        }
        

        can we implement spinbox and combobox in single delegate and use it for two different table?

        --Alles ist gut.

        R 1 Reply Last reply 18 Dec 2015, 09:00
        0
        • R Ratzz
          18 Dec 2015, 04:36

          @raven-worx
          Thanks for the reply.
          I do not have spinbox set to my table. Using setdata each cell of the tableturns out to be spinbox (not sure if it is spin box). can we do it without custom item delegate?
          It works with custom ItemDelegate. but i already have a delegate in my project with combobox

          QWidget *ComboBoxDelegate::createEditor(QWidget *parent,  const QStyleOptionViewItem & option , const QModelIndex & index ) const                               
           {
              QComboBox *editor = new QComboBox(parent);
              editor->addItems(comboItems);
              return editor;
          }
          

          can we implement spinbox and combobox in single delegate and use it for two different table?

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 18 Dec 2015, 09:00 last edited by
          #4

          @Ratzz

          can we implement spinbox and combobox in single delegate?

          sure you can: You also get the QModelIndex passed as parameter. Use this to either check the column or to retrieve any custom data (e.g. via data item role).

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          R 1 Reply Last reply 18 Dec 2015, 09:09
          0
          • R raven-worx
            18 Dec 2015, 09:00

            @Ratzz

            can we implement spinbox and combobox in single delegate?

            sure you can: You also get the QModelIndex passed as parameter. Use this to either check the column or to retrieve any custom data (e.g. via data item role).

            R Offline
            R Offline
            Ratzz
            wrote on 18 Dec 2015, 09:09 last edited by Ratzz
            #5

            @raven-worx
            checking column of one table will effect other table?

            --Alles ist gut.

            1 Reply Last reply
            0

            1/5

            17 Dec 2015, 13:41

            • Login

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