3
votes

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.

3
You don't need to use a delegate. Just try QStandardItem::setData() function with Qt::FontRole and Qt::BackgroundColorRole roles. - vahancho
it doesn't make text transparent it is not painted since your implementation does nothing. Does your class Delegate inherit something useful? - Marek R
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
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

3 Answers

7
votes

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);
}
5
votes

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));
2
votes

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));
}