Important: Please read the Qt Code of Conduct -

QTreeview resize

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


  • Could you explain point 3 better please?

  • 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?

  • So finally,
    I tried this first:


    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++)
    			totalWidth += m_features_view->columnWidth(column);
    	int newWidth = event->size().width();
    	//If width grows
    	if( totalWidth<newWidth )
    		int index =;
    		int width =;
    	else if( totalWidth>newWidth )
    		for(int column = columnsWidth.size()-1; column>=0&&totalWidth>newWidth; column--)
    			int index =;
    			int width =;
    			//Compute allocation to column
    			int remain = totalWidth-newWidth;
    			int allowable = width-m_minimumColumSize;
    			int allowed = (remain>allowable)?allowable:remain;
    				totalWidth -= allowed;
    		//We set the remaining, avoid go out limit

    I need to make improvement
    Best regards

  • Lifetime Qt Champion

    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 ?

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

  • Moderators

    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.

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

Log in to reply