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?

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.
  • RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on 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
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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

      RatzzR 1 Reply Last reply
      0
      • raven-worxR raven-worx

        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) );
        
        RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on 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.

        raven-worxR 1 Reply Last reply
        0
        • RatzzR Ratzz

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on 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

          RatzzR 1 Reply Last reply
          0
          • raven-worxR raven-worx

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

            RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by Ratzz
            #5

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

            --Alles ist gut.

            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