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. Custom table model save only six characters on float
Forum Updated to NodeBB v4.3 + New Features

Custom table model save only six characters on float

Scheduled Pinned Locked Moved Unsolved General and Desktop
decimalqtablemodel
4 Posts 3 Posters 529 Views
  • 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.
  • Z Offline
    Z Offline
    zloi_templar
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Z 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

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

        Z Offline
        Z Offline
        zloi_templar
        wrote on last edited by zloi_templar
        #3

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

        JonBJ 1 Reply Last reply
        0
        • Z zloi_templar

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

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

          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