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. QTableWidgetItem and edit float value > 2 decimal
QtWS25 Last Chance

QTableWidgetItem and edit float value > 2 decimal

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetiteqstyleditemdele
3 Posts 2 Posters 3.2k 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.
  • cfdevC Offline
    cfdevC Offline
    cfdev
    wrote on last edited by
    #1

    Hi everyone!
    By default the display and edit mode is 2 decimal precision on tableWidget. I need to 3 decimal.

    I use setData function of QTableWidgetItem like this:

    // quantity is qreal
    item_QUANTITY->setData(Qt::DisplayRole, m_ilist.quantity.at(i));
    

    -> To show with 3 decimal, I use QStyledItemDelegate class and setItemDelegateForColumn of tableWidget. It's ok!

    But how to edit with 3 decimal on tableWidget ?

    S 1 Reply Last reply
    0
    • cfdevC cfdev

      Hi everyone!
      By default the display and edit mode is 2 decimal precision on tableWidget. I need to 3 decimal.

      I use setData function of QTableWidgetItem like this:

      // quantity is qreal
      item_QUANTITY->setData(Qt::DisplayRole, m_ilist.quantity.at(i));
      

      -> To show with 3 decimal, I use QStyledItemDelegate class and setItemDelegateForColumn of tableWidget. It's ok!

      But how to edit with 3 decimal on tableWidget ?

      S Offline
      S Offline
      skebanga
      wrote on last edited by skebanga
      #2

      You'll need to override QStyledItemDelegate::createEditor and return a QDoubleSpinBox where you've called setDecimals(3);

      Something along these lines:

      class ItemDelegate : public QStyledItemDelegate
      {
          QWidget* ItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& style, const QModelIndex& index) const
          {
              QDoubleSpinBox* box = new QDoubleSpinBox(parent);
      
              box->setDecimals(3);
      
              // you can also set these
              box->setSingleStep(0.01);
              box->setMinimum(0);
              box->setMaximum(1000);
      
              return box;
          }
      };
      
      cfdevC 1 Reply Last reply
      2
      • S skebanga

        You'll need to override QStyledItemDelegate::createEditor and return a QDoubleSpinBox where you've called setDecimals(3);

        Something along these lines:

        class ItemDelegate : public QStyledItemDelegate
        {
            QWidget* ItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& style, const QModelIndex& index) const
            {
                QDoubleSpinBox* box = new QDoubleSpinBox(parent);
        
                box->setDecimals(3);
        
                // you can also set these
                box->setSingleStep(0.01);
                box->setMinimum(0);
                box->setMaximum(1000);
        
                return box;
            }
        };
        
        cfdevC Offline
        cfdevC Offline
        cfdev
        wrote on last edited by
        #3

        @skebanga Many thx!

        1 Reply Last reply
        0

        • Login

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