Custom item delegate sizeHint() does not set item height in QTreeView



  • Hi,
    Does anyone know what I am doing wrong?
    I've implemented the sizeHing() function in my custom delegate, as I found in few posts on this and other forums, and still I cannot set the items height.
    It seems like QStyleOptionViewItem doesn't have the correct width for the item.
    I have word wraping implemented in column nr 2, and when I open my app not all text is visible (some is draged down, like under the item in the next row).

    QSize MyItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if(index.column() == 2){
    
        QStyleOptionViewItem options = option;
    
        initStyleOption(&options, index);
    
        QTextDocument textDoc;  
        QString contactName = index.data(Qt::DisplayRole).toString();
        QString contactPhone = index.data(Qt::UserRole).toString();
        QString contactInfo = index.data(Qt::UserRole+1).toString();
    
        QTextCursor textCursor(&textDoc);
        QImage stanPoint = index.data(Qt::UserRole+2).value<QImage>();/*(contactStan.toImage());*/
    
        bool wczytajfote = !stanPoint.isNull();
    
        textCursor.insertHtml(contactName);
        if(wczytajfote){
        textCursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
        textCursor.insertImage(stanPoint);
        textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
        }
        textCursor.insertHtml(contactPhone);
        textCursor.insertHtml(contactInfo);
    
            textDoc.setTextWidth(options.rect.width());
    
        return QSize(textDoc.idealWidth(), textDoc.size().height());
    
        } else return QSize(30,30);
    }
    

    If I need to provide more code, please let me know.

    The app GUI looks like this : Click for the link to the image

    I don't know if it's important, but I have this columns header set to resize to fit the free space of the tree view (all other columns are set to static size of QSize(30, 30)


Log in to reply
 

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