【问题标题】:QTableView, setting a cell's font and background colourQTableView,设置单元格的字体和背景颜色
【发布时间】:2016-02-23 10:55:03
【问题描述】:

我正在使用 QTableView 和 QStandardItemModel 并且我正在尝试用保持黑色的字体为行着色。

我正在使用我的委托类的绘制方法:

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

这根本不起作用,它使每个单元格中的文本透明。我在这里做错了什么?

[编辑] 我也使用过painter->fillRect(option.rect, brush);,但它使单元格背景和文本颜色相同。

【问题讨论】:

  • 您不需要使用委托。只需尝试使用 Qt::FontRoleQt::BackgroundColorRole 角色的 QStandardItem::setData() 函数。
  • 它不会使文本透明它不会被绘制,因为您的实现什么都不做。你的班级Delegate 继承了有用的东西吗?
  • 我添加了一个drawDisplay() 函数和fillRect(),它似乎可以做我想做的事情,绘制背景并将文本保持黑色
  • 再次:您根本没有绘制任何文本,委托方法应该这样做!最好的解决方案是使用现有的实现并仅更改一些数据!再说一遍:Delegate 继承了什么?

标签: c++ qt delegates qtableview qpainter


【解决方案1】:

您的Delegate 应该继承QStyledItemDelegate

您的绘画事件可能应该如下所示:

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

【讨论】:

    【解决方案2】:

    正如vahancho所建议的,你可以使用QStandardItem::setData()函数:

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

    或者QStandardItem::setBackground()QStandardItem::setForeground() 函数:

    QStandardItem item;
    item.setBackground(QColor(Qt::green));
    item.setForeground(QColor(Qt::red));
    

    【讨论】:

    • 完美。在简单的情况下确实不需要委托! THX
    【解决方案3】:

    这对我有用:

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

    然后在 main() 中将委托分配给 tableview,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-21
      • 2017-02-08
      • 2016-12-18
      • 1970-01-01
      • 2015-06-02
      • 2012-06-06
      • 2019-10-21
      • 2021-10-19
      相关资源
      最近更新 更多