"10:" QSqlField("SS_SUMM", qlonglong, length: 8, precision: 2, generated: yes, typeID: 8)
class MoneyDelegate : public QStyledItemDelegate{ Q_OBJECTpublic: MoneyDelegate(int precision=2, QWidget *parent=0) : QStyledItemDelegate(parent), prec(precision) {} QString displayText (const QVariant & value, const QLocale & locale);private: int prec;};
QString MoneyDelegate::displayText(const QVariant &value, const QLocale &locale){ QString str=QStyledItemDelegate::displayText(value, locale); if((value.type()==QVariant::Int)|| (value.type()==QVariant::LongLong)|| (value.type()==QVariant::ULongLong)|| (value.type()==QVariant::UInt)|| (value.type()==QVariant::Double) ){ int dp=str.indexOf(locale.decimalPoint()); if(dp==-1){ //добавить decimal point str.append(locale.decimalPoint()); dp=str.length()-1; } str=str.leftJustified(prec+dp+1,'0',true); } return str;}
QList<QTableView*> list=findChildren <QTableView*>(); for(int i=0;i<list.count();i++){ if(list[i]->property(queryPropertyName.toLatin1()).isValid()){ model=new QSqlQueryModelXVis(list.at(i));............... model->setQuery(query,db->db); list.at(i)->setModel(model);............... //автоформатирование чисел под precison QHash<int, int> prec=model->precisions(); QHashIterator<int, int> it(prec); while(it.hasNext()){ it.next(); list.at(i)->setItemDelegateForColumn(it.key(),new MoneyDelegate(it.value())); }.................... } }
QString QFloatItemDelegate::displayText(const QVariant &value, const QLocale &locale) const{ switch (value.type()) { case QVariant::Int: case QVariant::UInt: case QVariant::LongLong: case QVariant::ULongLong: case QVariant::Double: case QMetaType::Float: return locale.toString(value.toReal(), 'f', prec); default: return QStyledItemDelegate::displayText(value, locale); }}
list[i]->setItemDelegateForColumn(it.key(),new MoneyDelegate(it.value(), list.at(i)));
QVariant QSqlQueryModelXVis::data(const QModelIndex &item, int role) const{ if(visColumnsHash.contains(item.column())){ diapVHash h=visColumnsHash[item.column()]; QSqlQuery q=query(); q.seek(item.row()); double d; if(dataColumns.contains(item.column())){ d=q.value(dataColumns[item.column()]).toDouble(); }else{ d=q.value(item.column()).toDouble(); } if(!h.get(d).contains(role))return QSqlQueryModel::data(item,role); else return h.get(d).data(role); } return QSqlQueryModel::data(item,role);}
QVariant QSqlQueryModelXVis::data(const QModelIndex &item, int role) const{ return QSqlQueryModel::data(item,role);}