2

Maybe what I'm about to ask is so basic that i missed it somewhere, but i google all kind of things and i have not been able to find the answer. I have the following table view, feed from a SQLite table:

  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);
  ui->bodyView->show();

Further down in the program i add data to the table to be displayed. That works with no problems. My question is: How i can access individual lines to format the row, i would like to change the size of the font, format the display to show number aligned and make it boldface but only for a specific row. Thanks for the help.

Dan3460
  • 95
  • 3
  • 13
  • What do you mean by line?, you could explain me please, you could show an image of what you want. – eyllanesc Jul 27 '17 at 17:18
  • When you say line, do you mean a column, or a row, or is there a certain cell? – eyllanesc Jul 27 '17 at 18:18
  • Sorry, i meant row, i just updated the post. What i want is for example make row 6 with a bigger font, boldface and change the background, to drive the eyes of the user to that row. The same could be true for a column, or a cell. – Dan3460 Jul 27 '17 at 18:37

1 Answers1

3

Qt's proposed solution to customize the details of individual view items rendering (and editing) is via delegates. The level of customization depends on your use-case: you can do some minor adjustment like changing a font or you can draw something completely special, like what's shown in Star Delegate example.

There are basically two options to proceed with a custom delegate: either you subclass QStyledItemDelegate (or its base class QItemDelegate if you need to draw the items of Qt's datatypes somewhat specially) and change some particular details of interest to you leaving the rest to the base class or you subclass QAbstractItemDelegate to have full control over the view item's appearance and behavior.

A delegate can be set up for the view in either of three different ways:

  1. for the whole view (i.e. for all model items) via setItemDelegate method
  2. for a particular column via setItemDelegateForColumn method
  3. for a particular row via setItemDelegateForRow method

For the sake of example, here's how you can specify a slightly larger font size for the item rendering:

class CustomDelegate: public QStyledItemDelegate
{
public:
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const Q_DECL_OVERRIDE;
};

void CustomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
{
    if (!index.isValid()) {
        return;
    }

    QFont font = option.font;
    font.setPointSize(font.pointSize() + 1);

    QStyleOptionViewItem localOption(option);
    localOption.font = font;
    QStyledItemDelegate::paint(painter, localOption, index);
}
Dmitry
  • 3,063
  • 2
  • 22
  • 32