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. QTreeView, how to get the scrollbars when resizing if we don't use ElideMode?

QTreeView, how to get the scrollbars when resizing if we don't use ElideMode?

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 4.7k Views 2 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.
  • jsulmJ jsulm

    @mbruel No, the splitter is the "layout" of the two widgets. Press somewhere in your central widget and press "Lay Out in a Grid".

    mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by
    #11

    @jsulm
    ok thanks, yes, it solves this global size issue.
    Any idea on the original issue: how to make the horizontal scrollbar of the TreeView kicks in when some of its items are not fully visible?

    1 Reply Last reply
    0
    • SGaistS SGaist

      Can you post a minimal sample code so we have the same widgets to look at ?

      mbruelM Offline
      mbruelM Offline
      mbruel
      wrote on last edited by
      #12

      @SGaist
      Any ideas for this issue?
      I've posted the code and a zip of the project.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #13

        @mbruel said in QTreeView, how to get the scrollbars when resizing if we don't use ElideMode?:

        In Designer, uncheck "headerStretchLastSection"

        mbruelM 1 Reply Last reply
        1
        • M mpergand

          @mbruel said in QTreeView, how to get the scrollbars when resizing if we don't use ElideMode?:

          In Designer, uncheck "headerStretchLastSection"

          mbruelM Offline
          mbruelM Offline
          mbruel
          wrote on last edited by
          #14

          @mpergand
          Well this make it worst. My items are all the time truncated even when there is space in the QTreeView.
          alt text

          It seems the size of the viewport is the one of the header? how could the size of the longest row? and set it to be this? I found it strange that there is not an option to do this automatically...

          1 Reply Last reply
          0
          • mbruelM Offline
            mbruelM Offline
            mbruel
            wrote on last edited by
            #15

            It seems that it is the header DefaultSectionSize that defines when the scrollbar will kicks in.
            I've tried to use the visualRect of my items but I'm getting a invalid one for all the items except the first one...

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                _ui(new Ui::MainWindow), _treeModel(new TreeModel()), _treeItemDelegate(new TreeItemDelegate())
            {
                _ui->setupUi(this);
            ....
            
                int maxWidth = 0;
                _setTreeViewMaxWith(maxWidth);
            
                _ui->treeView->header()->setDefaultSectionSize(maxWidth);
            }
            
            #include <QDebug>
            void MainWindow::_setTreeViewMaxWith(int &maxWidth, QModelIndex parent)
            {
                for(int r = 0; r < _treeModel->rowCount(parent); ++r)
                {
                    QModelIndex index = _treeModel->index(r, 0, parent);
                    int width = _ui->treeView->visualRect(index).width();
            qDebug() << "width row " << r << " : "   << width;
                    if (width > maxWidth)
                        maxWidth = width;
            
                    if( _treeModel->hasChildren(index) )
                        _setTreeViewMaxWith(maxWidth, index);
                }
            }
            

            Here is the output:
            width row 0 : 180
            width row 0 : 0
            width row 0 : 0
            width row 1 : 0
            width row 2 : 0
            width row 3 : 0
            width row 4 : 0
            width row 1 : 0

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #16
              _ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
              

              Work well with elideMode ...
              Infinite loop detected :)

              Well, the only solution seems to resize the column yourself, you can memorize the larger item in TreeItemDelegate::sizeHint
              and set the column width accordingly.

              mbruelM 2 Replies Last reply
              1
              • M mpergand
                _ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
                

                Work well with elideMode ...
                Infinite loop detected :)

                Well, the only solution seems to resize the column yourself, you can memorize the larger item in TreeItemDelegate::sizeHint
                and set the column width accordingly.

                mbruelM Offline
                mbruelM Offline
                mbruel
                wrote on last edited by
                #17

                @mpergand
                Cool, thanks, good advise :)
                You're right, instead of resizing the header, I can just resize the first column.
                What I've done is that my Delegate emit a signal in SizeHint when the size max increase. My MainWindow catch the signal and resize the column accordingly taking the indent of the index in consideration...
                This works but I'm quite surprised that we've to do it manually and that the functionality is not offered by the QTreeView directly.

                Here is my code for the people interested

                QSize TreeItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
                {
                    QStyleOptionViewItem myOption = option;
                    myOption.textElideMode = Qt::ElideNone;
                
                    QStandardItem *item = static_cast<const TreeModel*>(index.model())->itemFromIndex(index);
                    if (static_cast<TreeItem*>(item)->getType() == TreeItemType::TreeTypeItem)
                    {
                        myOption.font.setBold(true);
                        myOption.decorationSize = QSize(24,24);
                    }
                
                    QSize size = QStyledItemDelegate::sizeHint(myOption, index);
                    if (size.width() > _maxWidth)
                    {
                        _maxWidth = size.width();
                        emit maxWidthIncreased(_maxWidth, index);
                    }
                
                    if (item)
                    {
                        if (static_cast<TreeItem*>(item)->getType() == TreeItemType::TreeTypeItem)
                            size.setHeight(26);
                        else
                            size.setHeight(18);
                    }
                
                    return size;
                }
                

                and in MainWindow:

                void MainWindow::handleTreeViewMaxWidthChanged(int maxWidth, QModelIndex index)
                {
                    int indent = _ui->treeView->indentation();
                    while (index.parent().isValid())
                    {
                        indent += _ui->treeView->indentation();
                        index = index.parent();
                    }
                
                    qDebug() << "Max width from Delegate: " << maxWidth << ", indent: " << indent;
                    _ui->treeView->setColumnWidth(0, maxWidth+indent);
                }
                

                PS: for this to work, TreeItemDelegate::_maxWidth is mutable and the signal is const

                class TreeItemDelegate : public QStyledItemDelegate
                {
                    Q_OBJECT
                ...
                signals:
                    void maxWidthIncreased(int maxSize, QModelIndex index) const;
                
                private:
                    mutable int _maxWidth;
                };
                
                1 Reply Last reply
                0
                • M mpergand
                  _ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
                  

                  Work well with elideMode ...
                  Infinite loop detected :)

                  Well, the only solution seems to resize the column yourself, you can memorize the larger item in TreeItemDelegate::sizeHint
                  and set the column width accordingly.

                  mbruelM Offline
                  mbruelM Offline
                  mbruel
                  wrote on last edited by
                  #18

                  @mpergand said in QTreeView, how to get the scrollbars when resizing if we don't use ElideMode?:

                  Infinite loop detected :)

                  where would you see an infinite loop?

                  1 Reply Last reply
                  0
                  • mbruelM Offline
                    mbruelM Offline
                    mbruel
                    wrote on last edited by
                    #19

                    Following another this other thread about customizing delegates with the display of the icons, here is a more efficient version of this one:

                    QSize TreeItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
                    {
                        QSize size = QStyledItemDelegate::sizeHint(option, index);
                        if (size.width() > _maxWidth)
                        {
                            _maxWidth = size.width();
                            emit maxWidthIncreased(_maxWidth, index);
                        }
                        return size;
                    }
                    
                    void TreeItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
                    {
                        QStyledItemDelegate::initStyleOption(option, index);
                        option->textElideMode = Qt::ElideNone;
                    
                        TreeItem *item = nullptr;
                        if (_useProxy)
                            item = static_cast<const TreeProxyModel*>(index.model())->itemFromIndex(index);
                        else
                            item = static_cast<const TreeModel*>(index.model())->itemFromIndex(index);
                    
                        if (item && static_cast<TreeItem*>(item)->isRootItem())
                        {
                    //        option->palette.setColor(QPalette::Normal, QPalette::Text,Qt::blue);
                            option->font.setBold(true);
                            option->decorationSize = QSize(24,24);
                        }
                    }
                    

                    No need to override the paint method but instead the protected initStyleOption

                    1 Reply Last reply
                    1
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #20

                      Thanks for the feedback !

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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