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

TableView "setData" decimal



  • Hey i have a TableView.
    i have 4 inputfields and 3 "calculate Fields".

    That works all fine, but now i calculate something and i get for example:

    1834.123093843

    i want to get only the first 3 digits.
    my setData function looks like this:

    bool ParameterCalculate::setData(const QModelIndex &indexIn, const QVariant &value, int role)
    {
       row.at(indexIn.row())->getField().at(indexIn.column()).setMathCalculate(value.toDouble());
    }
    

    Any Idea how i can get only the first 2 - 4 digits?

    Thanks


  • Lifetime Qt Champion

    Return a properly adjusted value in your data() function.



  • how can i get only the first 2 Digits? In data i also have

    switch(role)
    {
    case Qt::DisplayRole:
    return actualMath.toString();
    ..
    ..
    
    
    }
    


  • @Christian-Ehrlicher said in TableView "setData" decimal:

    Return a properly adjusted value in your data() function.

    NO!!!!!!!

    data() is pure data. How it is shown on the screen is a job for the delegate

    class DecimalsDelegate : public QStyledItemDelegate {
    Q_OBJECT
    Q_DISABLE_COPY(DecimalsDelegate)
    public:
    explicit DecimalsDelegate(QObject* parent = Q_NULLPTR) 
    :QStyledItemDelegate(parent)
    ,m_numDec(3)
    {}
    unsigned int decimals() const {return m_numDec;}
    void setDecimals(unsigned int val){m_numDec=val;}
    QString  displayText(const QVariant &value, const QLocale &locale) const Q_DECL_OVERRIDE{
    switch(value.type()){
    case QMetaType::Double:
    return locale.toString(value.toDouble(),'f',m_numDec);
    case QMetaType::Float:
    return locale.toString(value.toFloat(),'f',m_numDec);
    default:
    return QStyledItemDelegate::displayText(value,locale);
    }
    }
    private:
    unsigned int m_numDec;
    };
    

    Now you can use tableView->setItemDelegate(new DecimalsDelegate(tableView));


Log in to reply