PDA

View Full Version : QTableWidgetItem visual format



uygar
6th October 2007, 10:12
1. In a TableView I have doubleSpinbox delegate, like count or money. the value comes from dataBase like numeric(10,4) I mean for example a cell seems "100000.1200", and after delegating it returns like "100000.12" or "450.0000" returns "450", that because It seems less.

2. I need to show these cells (like in ms excel) like ###,###.## for example "12,452,231.24" and need to keep (also like excel) more digits. I mean the value "1.2445" must seems like "1.24" but when in calculation, it must have its original value.

here is my codes:
in the main:


...
QTableWidgetItem *prof_x_miktar = new QTableWidgetItem(Proformaindex.value(6).toString() );
...
ProformaDelegate *spin_miktar;
spin_miktar = new ProformaDelegate(tableProformaindex);


in the delegate (tableProformaindex):


QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setMaximum(4000000000);
editor->setDecimals(2);
return editor;
...

void ProformaDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
ProformaDelegate::ProformaDelegate(QObject *parent)
: QItemDelegate(parent)
{

}

QWidget *ProformaDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
...
QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
double value = index.model()->data(index, Qt::DisplayRole).toDouble();
spinBox->setValue(value);
}

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

jpn
6th October 2007, 10:19
QDoubleSpinBox removes group separators by default. You can enable them like shown in this thread: http://www.qtcentre.org/forum/f-qt-programming-2/t-qspinbox-how-to-separate-the-numbers-8868.html

aekilic
6th October 2007, 14:22
Dear All

Actually we have another problem for this one...

We are using a QTableWidget, for one column we bring a double value. After that we have made a delegate for this column for QSpinBox.

What we would like to is we would like to

1st. Bring the values to QTable like 999,999,999.00
2nd. edit them in spin, and put it back to table 888,888,888.00
3rd we would like to save them to sql as 888888888

is it clear?

jpn
6th October 2007, 16:21
What's the problem? It gets stored as a number to the database, doesn't it? No formatting will be stored.

aekilic
6th October 2007, 17:18
no formatting is going to store to database, only what we like to do is to show them like i said...

jpn
6th October 2007, 17:48
You can always reimplement your model's data() and return numbers in localized format with help of QLocale. Item views localization support was significantly improved in Qt 4.3, so you could also try playing with table views locale.