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 validation in column data in Qtableview?

How to add validation in column data in Qtableview?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 482 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.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by
    #1

    Hi,
    I have requirement I'm using Qtableview (Qstandrdmodel)
    and doing some calculation e.g. when result in column 2 is -ve value then it should show with brackets like this (1.15) if not then 1.15.
    How to do this?
    As this validation i have to do for 4-5 columns.

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

      This is just manipulation of the displayed output, not validation. In any case you can use:

      class AccountingDelegate : public QStyledItemDelegate{
          Q_OBJECT
          Q_DISABLE_COPY(AccountingDelegate)
          template <class T>
          QString accountingDisplayText(const T& value, const QLocale &locale) const{
              if(value<T(0))
                  return tr("(%1)").arg(QStyledItemDelegate::displayText(-value,locale));
              return QStyledItemDelegate::displayText(value,locale);
          }
      public:
          explicit AccountingDelegate(QObject *parent = nullptr)
              : QStyledItemDelegate(parent) {}
          QString displayText(const QVariant &value, const QLocale &locale) const override{
              switch (value.userType()){
              case QMetaType::Long:
                  return accountingDisplayText(value.value<long>(),locale);
              case QMetaType::LongLong:
                  return accountingDisplayText(value.value<long long>(),locale);
              case QMetaType::Short:
                  return accountingDisplayText(value.value<short>(),locale);
              case QMetaType::Float:
                  return accountingDisplayText(value.value<float>(),locale);
              case QMetaType::Int:
                  return accountingDisplayText(value.value<int>(),locale);
              case QMetaType::Double:
                  return accountingDisplayText(value.value<double>(),locale);
              default:
                  return QStyledItemDelegate::displayText(value,locale);
              }
          }
      };
      

      Then you can use it with something like:

      AccountingDelegate *accDelegate = new AccountingDelegate(this);
      for(int column =0;column <4;++column)
          tableView->setItemDelegateForColumn(column,accDelegate);
      

      "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

      N 1 Reply Last reply
      2
      • VRoninV VRonin

        This is just manipulation of the displayed output, not validation. In any case you can use:

        class AccountingDelegate : public QStyledItemDelegate{
            Q_OBJECT
            Q_DISABLE_COPY(AccountingDelegate)
            template <class T>
            QString accountingDisplayText(const T& value, const QLocale &locale) const{
                if(value<T(0))
                    return tr("(%1)").arg(QStyledItemDelegate::displayText(-value,locale));
                return QStyledItemDelegate::displayText(value,locale);
            }
        public:
            explicit AccountingDelegate(QObject *parent = nullptr)
                : QStyledItemDelegate(parent) {}
            QString displayText(const QVariant &value, const QLocale &locale) const override{
                switch (value.userType()){
                case QMetaType::Long:
                    return accountingDisplayText(value.value<long>(),locale);
                case QMetaType::LongLong:
                    return accountingDisplayText(value.value<long long>(),locale);
                case QMetaType::Short:
                    return accountingDisplayText(value.value<short>(),locale);
                case QMetaType::Float:
                    return accountingDisplayText(value.value<float>(),locale);
                case QMetaType::Int:
                    return accountingDisplayText(value.value<int>(),locale);
                case QMetaType::Double:
                    return accountingDisplayText(value.value<double>(),locale);
                default:
                    return QStyledItemDelegate::displayText(value,locale);
                }
            }
        };
        

        Then you can use it with something like:

        AccountingDelegate *accDelegate = new AccountingDelegate(this);
        for(int column =0;column <4;++column)
            tableView->setItemDelegateForColumn(column,accDelegate);
        
        N Offline
        N Offline
        n-2204
        wrote on last edited by
        #3

        @VRonin thanks, so this will show () braces for all the data I mean not only -ve data in column

        VRoninV 1 Reply Last reply
        0
        • N n-2204

          @VRonin thanks, so this will show () braces for all the data I mean not only -ve data in column

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

          @n-2204 said in How to add validation in column data in Qtableview?:

          so this will show () braces for all the data I mean not only -ve data in column

          That's exactly what this code does. I'd direct your attention on the if(value<T(0)) condition

          "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

          • Login

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