Qt tableView 如何单独设置某一列的背景颜色,如何设置某列的属性为NoSelection

2024-12-04 00:49:34
推荐回答(1个)
回答1:

必须重写QItemDelegate的paint方法。

xxxx.h

class Delegate :publicQItemDelegate
{
Q_OBJECT
public:
Delegate(QWidget*parent =0):QItemDelegate(parent){}
void paint(QPainter*painter, constQStyleOptionViewItem&option,
constQModelIndex&index)const;
};

xxxx.cpp
table->setItemDelegate(new Delegate);

void Delegate::paint(QPainter*painter, constQStyleOptionViewItem&option,
constQModelIndex&index)const
{
if(index.column()== 2)

painter->fillRect(option.rect, option.palette.highlight());
QItemDelegate::paint(painter, option, index);
}