[Interest] QTableView column format

Muhammad Bashir Al-Noimi mbnoimi at gmail.com
Sat Jan 25 12:13:49 CET 2014


On 01/22/2014 10:24 AM, André Somers wrote:
> If you want to modify it at the 'view' end, you should use a
> QStyledItemDelegate subclass and install that on the view, or on a
> specific column of the view.
I did that exactly but unfortunately it didn't work :(

May you please tell me what's wrong?!
I'm still get "5.25446e+06" for "5254458.963"

I call the following:
---
         ui->tableView->setModel(_tableModel);
         ui->tableView->resizeColumnsToContents();

         EcsLongDigitDelegate *delegate = new EcsLongDigitDelegate;
         ui->tableView->setItemDelegateForColumn(2, delegate);
---

Database table (SQLite):
---
CREATE TABLE "names" ("id" INTEGER PRIMARY KEY  NOT NULL 
,"nam_full_name" TEXT,"nam_id" DOUBLE DEFAULT (null) );
---

Delegate class:
---
#include "ecslongdigitdelegate.h"

EcsLongDigitDelegate::EcsLongDigitDelegate(QObject *parent) :
     QStyledItemDelegate(parent)
{
}

QWidget *EcsLongDigitDelegate::createEditor(QWidget *parent, const 
QStyleOptionViewItem &option, const QModelIndex &index) const
{
     QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
     editor->setFrame(false);
     editor->setMinimum(0);
     editor->setDecimals(3);
     editor->setMaximum(999999999999);
     return editor;
}

void EcsLongDigitDelegate::setEditorData(QWidget *editor, const 
QModelIndex &index) const
{
     double value = index.model()->data(index, Qt::DisplayRole).toDouble();
     QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
     spinBox->setValue(value);
}

void EcsLongDigitDelegate::setModelData(QWidget *editor, 
QAbstractItemModel *model, const QModelIndex &index) const
{
     QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
     spinBox->interpretText();
     double value = spinBox->value();
     model->setData(index, value, Qt::DisplayRole);
}

void EcsLongDigitDelegate::updateEditorGeometry(QWidget *editor, const 
QStyleOptionViewItem &option, const QModelIndex &index) const
{
     editor->setGeometry(option.rect);
}
---

-- 
Best Regards,
Muhammad Bashir Al-Noimi




More information about the Interest mailing list