void QItemDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const [virtual]
paint函数的作用是:绘制要在view中显示的数据。
获取到model中的数据后,我想改变显示样式,那么可用在painter函数中进行改变。
1.
获取model中的数据
index.model()->data( index );
2.
改变数据为自己想要的形式
例如,改变字体,改变颜色等。
改变字体:
QStyleOptionViewItem so = option;//因为option参数是const类型,不能直接修改option,所以,做一个同类型的拷贝,修改so参数。
so.palette = QPalette(Qt::red);
drawDisplay(painter,so,so.rect,str);
3.
显示数据
调用QItemDelegate类的DrawDisplay()函数。
option变量,此时就用到了。因为option的字体,text对齐方式等都已经修改为想要的样式,所以,将option传递给DrawDisplay()函数。
DrawDisplay()函数的作用就是,绘制要现实的数据。
按照option中的样式进行绘制要显示的text。
void QItemDelegate::drawDisplay ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text )
Renders the item view text within the rectangle specified by rect using the given painter and style option.
作用:
绘制在view中显示的item的text,即绘制要在view中显示的文字,也就是item的内容。