Items overlap in QStyledItemDelegate descendant (QListView contents)



  • Good morning.

    I have a bit of a problem writing prototype application. I wrote sample item delegate, implemented its sizeHint and paint methods and it seems like paint ignores any changes to QRect variable used for painting: all the items are aligned to left and overlap. Sample code follows:

    void BlabDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
        QStyledItemDelegate::paint(painter,option,index);
        painter->save();
        QRect irect(option.rect);
        irect.setHeight(54); irect.setWidth(54);
        QApplication::style()->drawItemPixmap(painter,irect,0,amodel->get(index.data().toHash()["avatar"].toString()).scaledToHeight(54));
        irect = option.rect;
        irect.setLeft(64); irect.setBottom(option.rect.bottom()-20);
        QTextDocument doc;
        doc.setHtml(index.data().toHash()["text"].toString());
        doc.drawContents(painter,irect);
        painter->restore();
    }
    
    QSize BlabDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
        QStyleOptionViewItem iopt = option;
        iopt.rect.setHeight(64);
        return QSize(iopt.rect.width(),64);
    }
    

    Now, qDebug() states that irect actually has correct values (as expected) but QTextDocument looks like inserted under the icon when it should begin with 64px offset from left. Is there something I don't understand about writing paint method? Why does the above code not work? I would appreciate any help since I cannot find the right answer.

    EDIT: When I try to draw text as drawItemText from QStyle (and not use QTextDoc) it is all correct.

    Kind Regards,
    Artur



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.