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

    0_1483961581007_upload-458fd723-c251-4399-95ae-b83fac492b44


  • Qt Champions 2016

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

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

    Then maybe you just need to disable
    http://doc.qt.io/qt-5/qtreeview.html#uniformRowHeights-prop
    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 .


  • Qt Champions 2016

    @dridk2
    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){
            model()->layoutChanged();
        }
        QTreeView::resizeEvent(e);
    } 
    

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


  • Qt Champions 2016

    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 .

    Right...
    Sorry, answered to quickly.



  • Have You tried

    class ItemDelegate : public QItemDelegate
    {
      public:
         void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    
         editor->setGeometry(option.widget->rect());
    }
    };
    

Log in to reply
 

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