c++ - QTableView委托(delegate)小数点后的数字

标签 c++ qt delegates qtableview

我需要QTableView的项目的小数点后指定位数,所以我写了一个简单的委托(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;
   }
};

但是它不起作用,调用了构造函数,但没有调用 displayText() 函数。

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

我做错了什么?

最佳答案

您的方法未被调用,因为您忘记了函数签名末尾的 const 说明符:

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

关于c++ - QTableView委托(delegate)小数点后的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10064149/

相关文章:

ios - 无法从委托(delegate)方法中关闭 FBfriendPickerViewController

qt - 如何将 QPixmap 对象保存到文件?

c++ - QML 如何动态访问 ListView 项

c# - 什么时候在 C# 中使用委托(delegate)?

c++ - unordered_map 构造函数错误(equal_to 模板函数)

c++ - Qt Creator 找不到库文件

ios - 如何在 iPad 的 UISplitViewController 中将数据从细节传递到主控

c++ - 自动,错误 : map iterator has no member named ‘first`

c++ - 我在 c++ 中遇到错误 'PTHREAD_START_ROUTINE' 未在此范围内声明

c++ - 如何 boost::serialize std/boost::optional?