4

I am using QTableView and QStandardItemModel and I'm trying to colour a row with the font remaining black.

I am using my delegate class's paint method:

void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QBrush brush(Qt::red, Qt::SolidPattern);
    painter->setBackground(brush);
}

This does not work at all and it makes the text within each cell transparent. What am I doing wrong here?

[EDIT] I've used painter->fillRect(option.rect, brush); as well but it makes the cell background and text the same colour.

ethane
  • 2,329
  • 3
  • 22
  • 33
  • 3
    You don't need to use a delegate. Just try `QStandardItem::setData()` function with `Qt::FontRole` and `Qt::BackgroundColorRole` roles. – vahancho Feb 23 '16 at 11:00
  • it doesn't make text transparent it is not painted since your implementation does nothing. Does your class `Delegate` inherit something useful? – Marek R Feb 23 '16 at 11:05
  • I've added a `drawDisplay()` function in conjunction with `fillRect()` which seems to do what I want it to, paint the background and keep the text black – ethane Feb 23 '16 at 11:10
  • again: you didn't draw any text at all and delegate method should do it! The best solution is to use existing implementation and altering only some data! Again: What Delegate inherits? – Marek R Feb 23 '16 at 11:15

3 Answers3

7

Your Delegate should inherit QStyledItemDelegate.

Your paint event probably should look like this:

void Delegate::paint(QPainter *painter,
                     const QStyleOptionViewItem &option,
                     const QModelIndex &index) const
{
    QStyleOptionViewItem op(option);

    if (index.row() == 2) {
        op.font.setBold(true);
        op.palette.setColor(QPalette::Normal, QPalette::Background, Qt::black);
        op.palette.setColor(QPalette::Normal, QPalette::Foreground, Qt::white);
    }
    QStyledItemDelegate::paint(painter, op, index);
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
Marek R
  • 32,568
  • 6
  • 55
  • 140
5

As vahancho suggested, you can use the QStandardItem::setData() function:

QStandardItem item;
item.setData(QColor(Qt::green), Qt::BackgroundRole);
item.setData(QColor(Qt::red), Qt::FontRole);

Or the QStandardItem::setBackground() and QStandardItem::setForeground() functions:

QStandardItem item;
item.setBackground(QColor(Qt::green));
item.setForeground(QColor(Qt::red));
Tomas
  • 2,170
  • 10
  • 14
2

This worked for me:

class TableViewDelegateWritable : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit TableViewDelegateWritable(QObject *parent = 0)
        : QStyledItemDelegate(parent)
    {
    }

    // background color manipulation
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QColor background = QColor(135, 206, 255); // RGB value: https://www.rapidtables.com/web/color/blue-color.html
        painter->fillRect(option.rect, background);

        // Paint text
        QStyledItemDelegate::paint(painter, option, index);
    }

    // only allow digits
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    {
        QSpinBox *editor = new QSpinBox(parent);

        editor->setMinimum(-99999);
        editor->setMaximum(99999);

        return editor;
    }
};

Then in main() assign the delegate to the tableview like this:

for(int c = 0; c < ui->tableView->model()->columnCount(); c++)
{
    ui->tableView->setItemDelegateForColumn(c, new TableViewDelegateWritable(ui->tableView));
}
fruitCoder
  • 77
  • 5