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. TableView "setData" decimal
Qt 6.11 is out! See what's new in the release blog

TableView "setData" decimal

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 531 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.
  • N Offline
    N Offline
    NotYourFan
    wrote on last edited by
    #1

    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

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

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

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

      VRoninV 1 Reply Last reply
      0
      • N Offline
        N Offline
        NotYourFan
        wrote on last edited by
        #3

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

        switch(role)
        {
        case Qt::DisplayRole:
        return actualMath.toString();
        ..
        ..
        
        
        }
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

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

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

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

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

          • Login

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