Home > front end >  QStyledItemDelegate::paint - Why do the passed QStyleOptionViewItem have a rect property of size 0x0
QStyledItemDelegate::paint - Why do the passed QStyleOptionViewItem have a rect property of size 0x0

Time:01-24

I have a QListView where I set the model to a custom QAbstractItemModel, as well as a custom QStyledItemDelegate.

ui->listView->setModel(model);
ui->listView->setItemDelegate(new Delegate(ui->listView));

I followed this great answer on how to set the delegate up properly, so I overloaded paint() and sizeHint():

void
Delegate::paint(QPainter *painter,
                const QStyleOptionViewItem &option,
                const QModelIndex &index) const
{
    std::cout << __func__ << ": " << option.rect.width() << "x" << option.rect.height() << std::endl;

    QStyleOptionViewItem opt(option);
    initStyleOption(&opt, index);

    painter->save();
    // draw background of option.rect and some text
    painter->restore();
}
QSize
Delegate::sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const
{
    std::cout << __func__ << ": " << option.rect.width() << "x" << option.rect.height() << std::endl;

    return QSize(option.rect.width(), 100);
}

When I add an item to my model, it is properly propagated to the QListView and the delegate prints the following:

sizeHint: 1551x87
sizeHint: 0x0
paint: 0x0

Every new item I add is then drawn on top of the previous items, and the backgrounds aren't drawn, since option.rect is 0x0.

I've tried changing a bunch of its size-related properties of my QListView to no avail.

So why do the passed QStyleOptionViewItem have a rect of 0x0, or generally, where do the QStyleOptionViewItem get its properties from? Qt's paint documentation and sizeHint documentation doesn't seem very clear on this.

Thanks in advance!

CodePudding user response:

The issue was found in the model instead (columnCount() being calculated wrong). So if you encounter the same issue, try checking the model implementation.

By using the default QListView delegate, nothing was printed, even though I use Qt::DisplayRole. This way, the issue was isolated to be in the model, and not the view nor the delegate.

  • Related