Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStyledItemDelegate with QListView grows, but doesn't shrink
Qt 6.11 is out! See what's new in the release blog

QStyledItemDelegate with QListView grows, but doesn't shrink

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 700 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dpaulat
    wrote on last edited by dpaulat
    #1

    I have a class derived from QStyledItemDelegate that I am using to display HTML in a QListView. In order to bind my item delegate to the QListView, I use the following:

    ui->myListView->setItemDelegate(new HTMLDelegate(this));
    

    The QListView has defaults set, with the exception of the resizeMode property, which I have changed from Fixed to Adjust.

    From the sizeHint method, the option.rect property only reflects growth, not shrinking, which causes my resulting undesired behavior:

    QSize HTMLDelegate::sizeHint(const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
    {
        QStyleOptionViewItemV4 options = option;
        initStyleOption(&options, index);
    
        QTextDocument doc;
        doc.setHtml(index.data().toString());
        doc.setTextWidth(option.rect.width());
        return QSize(doc.idealWidth(), doc.size().height());
    }
    

    I can manually call a reset() on the QListView, and that will properly resize my items, but it also deselects any selected items. Sure, I could re-select the item, but I don't really see it as an elegant solution.

    Any ideas around my problem?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved