Digits after the decimal QTableView delegate

2019-07-19 07:42发布

问题:

I need a specified number of digits after the decimal point for the items of QTableView, so I wrote a simple delegate.

class TableItemDelegate : public QStyledItemDelegate
{

   Q_OBJECT

public:

   TableItemDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {}

   QString displayText(const QVariant & value, const QLocale & locale)
   {
     QString str = QString::number(value.toDouble(), 'f', 8);
     return str;
   }
};

But it doesn`t work, constructor called, but not the displayText() function.

TableItemDelegate *decDelegate = new TableItemDelegate(tableView);
tableView->setItemDelegate(decDelegate);

What I`m doing wrong?

回答1:

Your method isn't called because you forgot the const specifier at the end of the function signature:

QString displayText(const QVariant & value, const QLocale & locale ) const