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. Resize treeView row with delegate editor
Forum Updated to NodeBB v4.3 + New Features

Resize treeView row with delegate editor

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 4.1k Views 3 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
    dridk2
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      D 1 Reply Last reply
      1
      • mrjjM mrjj

        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?

        D Offline
        D Offline
        dridk2
        wrote on last edited by
        #3

        @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 .

        mrjjM 1 Reply Last reply
        0
        • D dridk2

          @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 .

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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. :)

          1 Reply Last reply
          0
          • michalosM Offline
            michalosM Offline
            michalos
            wrote on last edited by michalos
            #5

            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.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The first size hint being in the Editor class?

              1 Reply Last reply
              0
              • michalosM Offline
                michalosM Offline
                michalos
                wrote on last edited by michalos
                #7

                the sizeHing() is in my subclassed QStyledItemDelegate.

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

                Right...
                Sorry, answered to quickly.

                1 Reply Last reply
                0
                • michalosM Offline
                  michalosM Offline
                  michalos
                  wrote on last edited by
                  #8

                  Have You tried

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

                  • Login

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