Qt World Summit: Register Today!

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.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
        textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
        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