【问题标题】:Digits after the decimal QTableView delegate小数点后的数字 QTableView 委托
【发布时间】:2012-04-08 15:54:27
【问题描述】:

QTableView的item需要指定小数点后的位数,所以写了一个简单的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);

我做错了什么?

【问题讨论】:

  • 您是否尝试过 QVariant::isValid 或返回硬编码的 QString,例如“1.0000”?
  • 为这个视图设置的模型已经填充了数据,我确定QVariants是有效的。imageshack.us/photo/my-images/21/scrul.png

标签: c++ qt delegates qtableview


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-07-30
    • 2021-05-19
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2013-01-25
    • 2021-05-02
    相关资源
    最近更新 更多