QTableView formatting numbers

做~自己de王妃 提交于 2019-12-02 02:32:00

问题


I have created a delegate and i'm able to align and boldface the numbers on the table. I would like to force them to have two decimal places, for example 1.2 should show as 1.20. This is the header of the delagete:

#ifndef TOTALDELEGATE_H
#define TOTALDELEGATE_H

#include <QObject>
#include <QStyledItemDelegate>


class TotalDelegate : public QStyledItemDelegate
{
public:
  TotalDelegate();

  virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
};

#endif // TOTALDELEGATE_H

Here is the implementation:

#include "totaldelegate.h"

TotalDelegate::TotalDelegate()
{

}

void TotalDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
  if(!index.isValid()) return;
  QFont font=option.font;
  font.setBold(true);

  QStyleOptionViewItem localOption(option);
  localOption.font=font;
  localOption.displayAlignment=Qt::AlignRight;
  QStyledItemDelegate::paint(painter,localOption,index);

}

Still a little lost on how to control the alignment so it forces two decimals. Also i would like to know how to change the background color. Thanks for the help. Here is the model:

  body = new QSqlTableModel(parent,data->m_db);
  body->setTable("C"+QString::number(markTime.toSecsSinceEpoch()));
  body->select();
  ui->bodyView->setModel(body);
  ui->bodyView->sortByColumn(0,Qt::AscendingOrder);
  ui->bodyView->setColumnWidth(0,30);
  ui->bodyView->setColumnWidth(1,80);
  for(int x=2;x<ui->columns->maximum()+2;x++) ui->bodyView->setColumnWidth(x,40);
  ui->bodyView->setEditTriggers(QAbstractItemView::NoEditTriggers);
  ui->bodyView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  ui->bodyView->setAlternatingRowColors(true);

//  //  ***************  Testing  ********************
  ui->bodyView->setItemDelegateForRow(10,new TotalDelegate);

//  //  *****************Testing  ********************

  ui->bodyView->show();

回答1:


One possible solution is to create a custom QSqlTableModel class and override the QVariant QSqlTableModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const method.

In the case of setting how it will be shown we use the Qt::DisplayRole role as a filter, and in case of changing the background color we will use the Qt::BackgroundRole:

*.h

#ifndef CUSTOMSQLTABLEMODEL_H
#define CUSTOMSQLTABLEMODEL_H

#include <QSqlTableModel>

class CustomSqlTableModel : public QSqlTableModel
{
public:
    CustomSqlTableModel(QObject *parent = Q_NULLPTR, QSqlDatabase db = QSqlDatabase());

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
};

#endif // CUSTOMSQLTABLEMODEL_H

*.cpp

#include "customsqltablemodel.h"

#include <QBrush>

CustomSqlTableModel::CustomSqlTableModel(QObject *parent, QSqlDatabase db):QSqlTableModel(parent, db)
{

}

QVariant CustomSqlTableModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole){
        if(index.column()  == 4)
            return QVariant(QString::number(QSqlTableModel::data(index, role).toDouble(), 'f', 2));
    }

    if (role == Qt::BackgroundRole){
        if(index.row()  == 4)
            return QVariant(QBrush(Qt::blue));
    }
    return QSqlTableModel::data(index, role);
}

Output:



来源:https://stackoverflow.com/questions/45384202/qtableview-formatting-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!