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 limit values in the tableView which is set to a model QStandardItemModel where each cell is set to item delegate?

How to limit values in the tableView which is set to a model QStandardItemModel where each cell is set to item delegate?

Scheduled Pinned Locked Moved General and Desktop
qtableview
12 Posts 6 Posters 4.9k Views 3 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.
  • RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by Ratzz
    #1

    I have a tableView which is set to model QStandardItemModel
    Now tableview is set to an itemdelegate( comboboxdelegate) which similar to this
    How to limit the values (only to positive values)for each cell in the tableview?

        dataTableModel = new QStandardItemModel(8,4);
        ui->tableView->setModel(dataTableModel);
        for(int i=0;i<32;i++)
        {
            ui->tableView->setColumnWidth(i,64);
            ui->tableView->setRowHeight(i,33);
        }
        ui->tableView->verticalHeader()->hide();
        ui->tableView->horizontalHeader()->hide();
        monitorFilterDelegate=new ComboBoxDelegate();
        // setting combobox delegate for the tableview
        ui->tableView->setItemDelegate(monitorFilterDelegate) ;

    --Alles ist gut.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by
      #2

      Does your ComboBoxDelegate create editable comboboxes?

      If not all user can choose from are items you filled in the combobox.

      In any case you can set validator to combobox when you create it in createEditor:

      void QComboBox::setValidator ( const QValidator * validator )

      1 Reply Last reply
      0
      • RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by Ratzz
        #3

        @alex_malyu
        Thanks for the reply
        Now if i remove the the item delegate from the tableview.
        I set the data to the model . Here each cell is user editable . user can give positive, negative and 0. I want the user to limit the values only to positive and zero.
        Can we limit the user editable values?How to limit the values to positive and zero ??

            dataTableModel = new QStandardItemModel(8,4);
            ui->tableView->setModel(dataTableModel);
            for(int i=0;i<32;i++)
            {
                ui->tableView->setColumnWidth(i,64);
                ui->tableView->setRowHeight(i,33);
            }
            ui->tableView->verticalHeader()->hide();
            ui->tableView->horizontalHeader()->hide();
            int row = 8;
            int col = 4;
            for(int i = 0; i < row ; i++)
            {
                for(int j = 0; j< col ; j++)
                {
                    QModelIndex index= dataTableModel->index(j,i);
                    dataTableModel->setData(index,0,Qt::EditRole);
                }
            }

        --Alles ist gut.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Wouldn't a QSpinBox be more suited for that ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          RatzzR 1 Reply Last reply
          0
          • A Offline
            A Offline
            alex_malyu
            wrote on last edited by
            #5

            @Ratzz said:

            Now if i remove the the item delegate from the tableview.

            Why? item delegate is the straightforward way to control editing widget.
            If you do not need it for other purpose use it to set validator, which will prevent user from entering unwanted values.
            This can be done only on the widget level. You may add control what data is set to model, but this will not prevent user from entering unwanted values, even though may prevent putting wrong values into tha model data.

            RatzzR 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Wouldn't a QSpinBox be more suited for that ?

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

              @SGaist
              Thanks for the reply.
              Yes it is suited .
              Is there any other solution using the code i have shown?
              Here is the similar code.

              --Alles ist gut.

              1 Reply Last reply
              0
              • A alex_malyu

                @Ratzz said:

                Now if i remove the the item delegate from the tableview.

                Why? item delegate is the straightforward way to control editing widget.
                If you do not need it for other purpose use it to set validator, which will prevent user from entering unwanted values.
                This can be done only on the widget level. You may add control what data is set to model, but this will not prevent user from entering unwanted values, even though may prevent putting wrong values into tha model data.

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

                @alex_malyu said:

                If you do not need it for other purpose use it to set validator

                I did not get you. Can you just elaborate?

                --Alles ist gut.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  If the spin boxes all have the same range, just set them when construction the boxes

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alex_malyu
                    wrote on last edited by
                    #9

                    @Ratzz
                    Look at the variantDelegate::createEditor example below.
                    It creates QLineEdit *lineEdit editor and sets validator on it depending on the type of data.
                    You may look for validator you need in Qt documentation or if you ok with spin box you can use QSpinBox as an editor and set range to it through interface it provides as SGaist suggested. .

                    QWidget *VariantDelegate::createEditor(QWidget parent,
                    const QStyleOptionViewItem & /
                    option */,
                    const QModelIndex &index) const
                    {
                    if (index.column() != 2)
                    return 0;

                     QVariant originalValue = index.model()->data(index, Qt::UserRole);
                     if (!isSupportedType(originalValue.type()))
                         return 0;
                    
                     QLineEdit *lineEdit = new QLineEdit(parent);
                     lineEdit->setFrame(false);
                    
                     QRegExp regExp;
                    
                     switch (originalValue.type()) {
                     case QVariant::Bool:
                         regExp = boolExp;
                         break;
                     case QVariant::ByteArray:
                         regExp = byteArrayExp;
                         break;
                     case QVariant::Char:
                         regExp = charExp;
                         break;
                     case QVariant::Color:
                         regExp = colorExp;
                         break;
                     case QVariant::Date:
                         regExp = dateExp;
                         break;
                     case QVariant::DateTime:
                         regExp = dateTimeExp;
                         break;
                     case QVariant::Double:
                         regExp = doubleExp;
                         break;
                     case QVariant::Int:
                     case QVariant::LongLong:
                         regExp = signedIntegerExp;
                         break;
                     case QVariant::Point:
                         regExp = pointExp;
                         break;
                     case QVariant::Rect:
                         regExp = rectExp;
                         break;
                     case QVariant::Size:
                         regExp = sizeExp;
                         break;
                     case QVariant::Time:
                         regExp = timeExp;
                         break;
                     case QVariant::UInt:
                     case QVariant::ULongLong:
                         regExp = unsignedIntegerExp;
                         break;
                     default:
                         ;
                     }
                    
                     if (!regExp.isEmpty()) {
                         QValidator *validator = new QRegExpValidator(regExp, lineEdit);
                         lineEdit->setValidator(validator);
                     }
                    
                     return lineEdit;
                    

                    }

                    A 1 Reply Last reply
                    0
                    • A alex_malyu

                      @Ratzz
                      Look at the variantDelegate::createEditor example below.
                      It creates QLineEdit *lineEdit editor and sets validator on it depending on the type of data.
                      You may look for validator you need in Qt documentation or if you ok with spin box you can use QSpinBox as an editor and set range to it through interface it provides as SGaist suggested. .

                      QWidget *VariantDelegate::createEditor(QWidget parent,
                      const QStyleOptionViewItem & /
                      option */,
                      const QModelIndex &index) const
                      {
                      if (index.column() != 2)
                      return 0;

                       QVariant originalValue = index.model()->data(index, Qt::UserRole);
                       if (!isSupportedType(originalValue.type()))
                           return 0;
                      
                       QLineEdit *lineEdit = new QLineEdit(parent);
                       lineEdit->setFrame(false);
                      
                       QRegExp regExp;
                      
                       switch (originalValue.type()) {
                       case QVariant::Bool:
                           regExp = boolExp;
                           break;
                       case QVariant::ByteArray:
                           regExp = byteArrayExp;
                           break;
                       case QVariant::Char:
                           regExp = charExp;
                           break;
                       case QVariant::Color:
                           regExp = colorExp;
                           break;
                       case QVariant::Date:
                           regExp = dateExp;
                           break;
                       case QVariant::DateTime:
                           regExp = dateTimeExp;
                           break;
                       case QVariant::Double:
                           regExp = doubleExp;
                           break;
                       case QVariant::Int:
                       case QVariant::LongLong:
                           regExp = signedIntegerExp;
                           break;
                       case QVariant::Point:
                           regExp = pointExp;
                           break;
                       case QVariant::Rect:
                           regExp = rectExp;
                           break;
                       case QVariant::Size:
                           regExp = sizeExp;
                           break;
                       case QVariant::Time:
                           regExp = timeExp;
                           break;
                       case QVariant::UInt:
                       case QVariant::ULongLong:
                           regExp = unsignedIntegerExp;
                           break;
                       default:
                           ;
                       }
                      
                       if (!regExp.isEmpty()) {
                           QValidator *validator = new QRegExpValidator(regExp, lineEdit);
                           lineEdit->setValidator(validator);
                       }
                      
                       return lineEdit;
                      

                      }

                      A Offline
                      A Offline
                      Abhi_oct30
                      wrote on last edited by
                      #10

                      @alex_malyu Seems you have some different validator..I want a validator like some range check for integer..How do i do that

                      mrjjM 1 Reply Last reply
                      0
                      • A Abhi_oct30

                        @alex_malyu Seems you have some different validator..I want a validator like some range check for integer..How do i do that

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Abhi_oct30
                        Hi
                        Do you mean like
                        http://doc.qt.io/qt-5/qintvalidator.html

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

                          You just need a little cheat on the standard delegate

                          class PositiveDelegate : public QStyledItemDelegate{
                          Q_OBJECT
                          Q_DISABLE_COPY(PositiveDelegate)
                          public:
                          explicit PositiveDelegate(QObject* parent=Q_NULLPTR)
                          :QStyledItemDelegate(parent)
                          {}
                          virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex &index) const{
                          QWidget* result = QStyledItemDelegate::createEditor(parent,option,index);
                          QSpinBox* intSpinRes = qobject_cast<QSpinBox*>(result);
                          if(intSpinRes ){
                          intSpinRes ->setMinimum(0);
                          return intSpinRes;
                          }
                          QDoubleSpinBox* doubSpinRes = qobject_cast<QDoubleSpinBox*>(result);
                          if(doubSpinRes ){
                          doubSpinRes ->setMinimum(0.0);
                          return doubSpinRes ;
                          }
                          return result;
                          }
                          };
                          

                          "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

                          • Login

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