Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add validation in column data in Qtableview?



  • 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.



  • 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);
    


  • 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);
    


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



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


Log in to reply