[Interest] QTableView column format
Andre Somers
andre at familiesomers.nl
Sat Jan 25 11:54:07 CET 2014
Muhammad Bashir Al-Noimi schreef op 25-1-2014 12:13:
> 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?!
Your delegate is dealing with the editor, but not with the display. Try
reimplementing the displayText method. Also, check if the data type you
get from the database really is numerical. SQLite can be weird with data
types in my experience.
André
> 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);
> }
> ---
>
More information about the Interest
mailing list