QTableWidgetItem and edit float value > 2 decimal

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

  • 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);
            // you can also set these
            return box;

  • @skebanga Many thx!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.