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

Custom table model save only six characters on float



  • Hello, i have my own class CustomTableModel based on QAbstractTableModel

    When i try to save some numbers, for exameple 111111.11 he save only 111111

    bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (index.isValid() && role == Qt::EditRole)
        {
            Columns[index.row()][index.column()] = value.toFloat();
            qDebug()<<value.toString()<<value.toFloat();
            emit(dataChanged(index, index));
            return true;
        }
        return false;
    }
    

    If i made Columns type QVector<QVector<QString>> Columns, all works correct, but i don't wanna work with string and convertations. Can i save correct float values without losing decimal places?
    Any suggestions?


  • Lifetime Qt Champion

    @zloi_templar said in Custom table model save only six characters on float:

    When i try to save some numbers, for exameple 111111.11 he save only 111111

    This is wrong - it only displays 6 digits I would guess. It's just a problem on how it is displayed - how did you check?



  • @Christian-Ehrlicher
    i save values on db, it's save only 6 digits

    query.addBindValue(view->model()->data(view->model()->index(i, 0 , QModelIndex()), Qt::EditRole).toDouble());
    

    if i tried

    QVariant CustomTableModel::data(const QModelIndex &index, int role) const
    {
    if ((role == Qt::EditRole) && !std::isnan(Columns[index.row()][index.column()]))
            return QString::number(Columns[index.row()][index.column()], 'f', 6);
    }
    

    he print something like this:
    qDebug()<<value.toString()<<QString::number(value.toFloat(), 'f', 6);
    "123456.123456" "123456.125000"

    but numbers with 7 and more letters before dot it save like:
    "12345678.12345" "12345678.000000"



  • @zloi_templar
    I don't understand:

    • Why are you "saving" (whatever you mean by that) a number as a string? You're asking for trouble, save it as a number.

    • Is using toFloat() a good idea? floats have limited precision, at least use toDouble()/doubles?

    • Even then you will lose accuracy. The only "safe" way to store accurate floating point numbers in a database is via its decimal type support.


Log in to reply