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 resize
Forum Updated to NodeBB v4.3 + New Features

QTreeview resize

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 9.8k 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.
  • Romain CR Offline
    Romain CR Offline
    Romain C
    wrote on last edited by
    #1

    Hi,
    I'm here because I got some troubles about a QTreeview component, and I don't know how to resolve it.

    Here are my needs:

    • QTreeview need to fit parent width
    • When parent widget is resize horizontally, each column is resize until min size of each column is reached, starting with Right column to Left. Resize is blocked after that.
    • When a column is resized, QTreeview don't have to resize content. It kept the widget parent width.

    Does it exist a natural way to do that? Or does I need to implement it?
    I preferred to avoid some tricky code in the best solution.

    Thanks!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Could you explain point 3 better please?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • Romain CR Offline
        Romain CR Offline
        Romain C
        wrote on last edited by
        #3

        To explain with differents word, If a column is resized ( increase or decrease width), I don't want this change affect QTreeview width. Is it better?

        1 Reply Last reply
        0
        • Romain CR Offline
          Romain CR Offline
          Romain C
          wrote on last edited by Romain C
          #4

          So finally,
          I tried this first:

          m_view->header()->resizeSections(QHeaderView::Interactive);
          m_view->header()->setMinimumSectionSize(50);
          m_view->header()->setStretchLastSection(true);
          m_view->header()->setCascadingSectionResizes(true);
          

          But it seems to be inneficient when resize widget. Last column will be resized, but it is not propagated to others columns.

          I tried something like these in parent item:

          void QFeaturesWithFavorites::resizeEvent(QResizeEvent* event) 
          {
          	// Column count for features
          	int featureColumnCount = m_features_view->model()->columnCount();
          	QVector<QPair<int,int>> columnsWidth(featureColumnCount);
          	QSize newSize = event->size();
          
          	// Count how many columns are visible
          	int totalWidth = 0;
          	for(int column = 0;column<featureColumnCount;column++)
          	{
          		if(!m_features_view->isColumnHidden(column))
          		{
          			columnsWidth.push_back(QPair<int,int>(column,m_features_view->columnWidth(column)));
          			totalWidth += m_features_view->columnWidth(column);
          		}
          	}
          
          	int newWidth = event->size().width();
          	//If width grows
          	if( totalWidth<newWidth )
          	{
          		int index = columnsWidth.at(columnsWidth.size()-1).first;
          		int width = columnsWidth.at(columnsWidth.size()-1).second+(newWidth-totalWidth);
          		m_features_view->setColumnWidth(index,width);
          	}
          	else if( totalWidth>newWidth )
          	{
          		for(int column = columnsWidth.size()-1; column>=0&&totalWidth>newWidth; column--)
          		{
          			int index = columnsWidth.at(column).first;
          			int width = columnsWidth.at(column).second+(newWidth-totalWidth);
          			//Compute allocation to column
          			int remain = totalWidth-newWidth;
          			int allowable = width-m_minimumColumSize;
          			int allowed = (remain>allowable)?allowable:remain;
          
          			if(allowed!=0)
          			{
          				totalWidth -= allowed;
          				m_features_view->setColumnWidth(index,width-allowed);
          			}
          		}
          		//We set the remaining, avoid go out limit
          		newSize.setWidth(totalWidth);
          	}
          
          	resize(newSize);
          }
          

          I need to make improvement
          Best regards

          mrjjM 1 Reply Last reply
          0
          • Romain CR Romain C

            So finally,
            I tried this first:

            m_view->header()->resizeSections(QHeaderView::Interactive);
            m_view->header()->setMinimumSectionSize(50);
            m_view->header()->setStretchLastSection(true);
            m_view->header()->setCascadingSectionResizes(true);
            

            But it seems to be inneficient when resize widget. Last column will be resized, but it is not propagated to others columns.

            I tried something like these in parent item:

            void QFeaturesWithFavorites::resizeEvent(QResizeEvent* event) 
            {
            	// Column count for features
            	int featureColumnCount = m_features_view->model()->columnCount();
            	QVector<QPair<int,int>> columnsWidth(featureColumnCount);
            	QSize newSize = event->size();
            
            	// Count how many columns are visible
            	int totalWidth = 0;
            	for(int column = 0;column<featureColumnCount;column++)
            	{
            		if(!m_features_view->isColumnHidden(column))
            		{
            			columnsWidth.push_back(QPair<int,int>(column,m_features_view->columnWidth(column)));
            			totalWidth += m_features_view->columnWidth(column);
            		}
            	}
            
            	int newWidth = event->size().width();
            	//If width grows
            	if( totalWidth<newWidth )
            	{
            		int index = columnsWidth.at(columnsWidth.size()-1).first;
            		int width = columnsWidth.at(columnsWidth.size()-1).second+(newWidth-totalWidth);
            		m_features_view->setColumnWidth(index,width);
            	}
            	else if( totalWidth>newWidth )
            	{
            		for(int column = columnsWidth.size()-1; column>=0&&totalWidth>newWidth; column--)
            		{
            			int index = columnsWidth.at(column).first;
            			int width = columnsWidth.at(column).second+(newWidth-totalWidth);
            			//Compute allocation to column
            			int remain = totalWidth-newWidth;
            			int allowable = width-m_minimumColumSize;
            			int allowed = (remain>allowable)?allowable:remain;
            
            			if(allowed!=0)
            			{
            				totalWidth -= allowed;
            				m_features_view->setColumnWidth(index,width-allowed);
            			}
            		}
            		//We set the remaining, avoid go out limit
            		newSize.setWidth(totalWidth);
            	}
            
            	resize(newSize);
            }
            

            I need to make improvement
            Best regards

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

            @Romain-C
            What kinds of improvements are you after ?

            • but it is not propagated to others columns.

            but you do loop them with
            for(int column = 0;column<featureColumnCount;column++)

            so you can just adjust each ?

            1 Reply Last reply
            0
            • Romain CR Offline
              Romain CR Offline
              Romain C
              wrote on last edited by
              #6

              Hi!
              WHen I'm talking of improvement, I mean a better approach to do it. In the best world, I wish a native without making tricks.

              Now I'm in front of a big problem: when I drag slowly my component, all seems fine.
              But when I'm doing a quick resize, something goes wrong.
              I think my problem came from time taken by my algoirthm and how event are catch.

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #7

                I think my problem came from time taken by my algoirthm and how event are catch.

                The problem comes from the fact that you're calling resize() inside a resizeEvent. That's highly recursive and can easily cause a hang or a stack overflow crash because it stops only when you happen to resize to the same size it had before. Don't do that.
                There's no way to do exactly what you want built-in so manual solution is the only solution.

                Romain CR 1 Reply Last reply
                3
                • Chris KawaC Chris Kawa

                  I think my problem came from time taken by my algoirthm and how event are catch.

                  The problem comes from the fact that you're calling resize() inside a resizeEvent. That's highly recursive and can easily cause a hang or a stack overflow crash because it stops only when you happen to resize to the same size it had before. Don't do that.
                  There's no way to do exactly what you want built-in so manual solution is the only solution.

                  Romain CR Offline
                  Romain CR Offline
                  Romain C
                  wrote on last edited by
                  #8

                  @Chris-Kawa My bad for resize inside resizeEvent! =)

                  There's no way to do exactly what you want built-in so manual solution is the only solution.

                  It's what I was expecting for as answer, to confirm has no way to do it in a normal way.

                  Concern the slowness, I have to rethink my concept including resize inside RE. I hope to have same quite similar performance as native interactions.

                  Thanks everyone

                  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