Qt World Summit: Register Today!

Resize treeView row with delegate editor

  • I would like to resize treeview row to fit with my editor.
    From QItemDelegate, I can only set a default row height from sizeHint. How can I change the height relative to Edit or Display


  • Lifetime Qt Champion

    If you have SizeHint ( sample is for fitting to a pixmap)

    class ItemDelegate : public QItemDelegate
          QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
               const TreeItem* ti(static_cast<TreeItem*>(index.internalPointer()));
                  return ti->pixmap()->size();

    Then maybe you just need to disable
    if its on.
    Also the docs says
    "Note: If the editor size hint is bigger than the cell size hint, then the size hint of the editor will be used."
    So it sounds like it can also have a sizehint?
    Did you try that route?

  • @mrjj Thanks for your reply. But no It doesn't work.

    The problem is that QAbstractItemDelegate::createEditor() doesn't call sizeHint. The Widget editor is adjusted to the row height. I want to adjust the row height to the editor.
    In your pixmap example, the row keep the same size everytime. For me, the row must be changed when editor is called .

  • Lifetime Qt Champion

    Hmm, I was under the impression that it would when
    uniformRowHeights is off. Also it would use Editors SizeHint if cell size is smaller.
    I dont know how else its possible

    I would wait and see if VRonin would be so kind to provide input as might know
    a good way. This cant be that uncommon use case. :)

  • If I understand correctly:
    Use the options widget dimensions in sizehint(), to set the row height.

    //in sizeHint():
    QStyleOptionViewItem options = option;
        initStyleOption(&options, index);
    int scrollBarrWidth = options.widget->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
    QRect rowRect(0,0,(options.widget->rect().right()-scrollBarrWidth),options.widget->rect()..bottom());

    Then in my TreeView I reimplement resizeEvent, to shange the row every time the treeView changes it's dimensions

    void MyTreeView::resizeEvent(QResizeEvent *e)
        if(model() != Q_NULLPTR){

    I did not test the first code block, but I use the options.widget->rect().right() im my app.

  • Lifetime Qt Champion

    The first size hint being in the Editor class?

  • the sizeHing() is in my subclassed QStyledItemDelegate.

    For me, the row must be changed when editor is called .

    Sorry, answered to quickly.

  • Have You tried

    class ItemDelegate : public QItemDelegate
         void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const

Log in to reply