PDA

View Full Version : Draw grid in QListView



woodtluk
22nd September 2010, 13:02
Hi there

I'd like to draw lines between items in my list.

I try to do it in my delegate (derived from QStyledItemDelegate):



void MembersListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(!index.isValid())
return;

painter->save();
painter->setRenderHint(QPainter::Antialiasing);

QStyleOptionViewItemV4 opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);

// ...

// seperation lines
QPen linePen(Qt::gray);
painter->setPen(linePen);
painter->drawLine(0, opt.rect.height(), opt.rect.width(), opt.rect.height());

painter->restore();
}


If I do so the line is only painted under the first item. The paint method should be called for each index and paint the line on bottom of the item.

Isn't there a ready made function for the lines between items like in QTableView::setShowGrid(bool show)?

Thaks!
Luke

nish
22nd September 2010, 13:43
opt.rect.height(), opt.rect.width(), opt.rect.height()
will always return the same numbers.. u need opt.rect.x() opt.rect.y()

woodtluk
22nd September 2010, 14:54
thanks!

I used some functions of QRect:

painter->drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight());