2

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();
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Dan3460
  • 95
  • 3
  • 13

1 Answers1

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:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks for the answer. The problem with this solution is that is affecting the whole table, i only want to affect one line with the background color. Other thing is how do you put a "," to separate the 1,000s? Is hard for me to believe that is not a "function" that can mask the display of numbers, like in Excel "#,##0.00" for example. – Dan3460 Jul 29 '17 at 11:53
  • Please do not extend the question, to be precise that row you want to be painted of a certain color, what is the criterion? – eyllanesc Jul 29 '17 at 21:22
  • Be more specific please. Because the model is different at the time of editing than the display – eyllanesc Jul 29 '17 at 21:24
  • Thanks for the answer, I see how you doing that. Was not my intention to extend the question, which is basically how to format the numbers for display so the sheet looks professional. – Dan3460 Jul 31 '17 at 00:06
  • My answer is general, you must accommodate the code for your specific case. I help you my answer? – eyllanesc Jul 31 '17 at 01:46
  • Yes it did, It just seems to me that such a powerful language does not have a easy facility to make the screen look more professional. – Dan3460 Jul 31 '17 at 02:45
  • What do you mean by more professional? Any language can do it, it all depends on the programmer. :P – eyllanesc Jul 31 '17 at 02:47
  • I do understand that depends on the programmer. To me, and this is just my opinion, when creating the structures for the table view i would have added some functionality to easily change alignments, fonts, colors, formats, etc. Don't get my wrong I love the QT framework, is just that in few occasions fund myself skipping "beautifying' something because of the extra work that entitles. – Dan3460 Aug 01 '17 at 11:03
  • For you how should the background color be elegantly changed, ie what function should there be? – eyllanesc Aug 01 '17 at 12:53
  • I would envision something like this: ui->myTableView->setColumnFormat(2,"#,###.00"); ui->myTableView->setRowFormat(10,Qt::bold); – Dan3460 Aug 01 '17 at 13:14
  • This type of functions involves storing the data in memory for each element, that is, let's say in total that data occupy b bytes, then for all the elements of the table would be used b * number_of_columns * number_of_rows, so that it would spend too much memory Unnecessarily. Qt looks for the best performance and tells the author that it will create it through a function. Also in Qt the model not only involves the data but this is only a role as there are the roles of Qt::DisplayRole, Qt::BackGroundRole, etc. – eyllanesc Aug 01 '17 at 13:22