Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Need to have QDockView resize automatically when QTreeView child node expanded



  • I have a QTreeView inside of a QDockView. I want the columns/headers to resize based on contents (ideally never show ellipses, require resizing, or REQUIRE a horizontal scroll bar [although it's acceptable to have a horizontal scrollbar if the user chooses to shrink the width of the QDockView]). Essentially, when a tree element is expanded, the DockView's width should expand to accommodate the contents of both columns without requiring a scrollbar, and both columns' data should be visible by default.

    This is what I currently have:

    	connect (
    		dataTreeClass.dataTree,
        		&QTreeView::expanded,
    		this,
    		&myClass::handleTreeExpansion
            );
    
    void myClass::handleTreeExpansion (QModelIndex const &index_)
    {
    	dataTreeClass.dataTree->resizeColumnToContents(0);
    	dataTreeClass.dataTree->resizeColumnToContents(1);
    }
    

    I've tried all options for QHeaderView scrollbar behaviors and gotten no luck. So much of the data gets hidden with resizing (even based on hints), and it is especially bad when the tree expands such that the vertical scrollbar becomes visible, at which point the data in the second column becomes very hidden.

    My logic was to set the width of the QDockView to the sum of the width of the QTreeView plus the width of vertical scrollbar, after resizeColumnsToContents with each signal, but the closest I've gotten is that the size of the QDockView just continuously grows larger and larger with each tree node expansion (incidentally by 30 pixels each time, which is also the width of the vertical scroll bar; so I'm sure it's a logic error, but I've been working on this seemingly simple problem for far too long).


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt are you using ?
    On what platform ?
    Can you provide a minimal compilable example that shows the state where you are currently ?



  • @SGaist Hey thank you. I'm using Qt 5.9.2 on CentOS Linux. I'm reasonably sure I'm unable to provide anything compilable, as I'm working on proprietary code, and I have to submit minimal code snippets here to maintain non-disclosure compliance.

    The code given on https://forum.qt.io/topic/69181/qtreeview-last-column-always-visible-without-scrollbars/2 seemed very close to what I wanted to do, with the exception of that OP wanting to always view the second column, whereas I want to always view both columns.

    Essentially, I have a dock on the right side of the window with a QTreeView and as the tree is expanded, I want the QDockView to expand to the left, eliminating the need (but not the option) for a horizontal scroll bar. The left-expansion of the QDockView must consider both the width of the QTreeView and the vertical scroll bar, should it be necessary that it appear.

    I appreciate your response. Thank you.


  • Lifetime Qt Champion

    I understand and no need for your application code.

    I was thinking of a minimal QTreeView (maybe QTreeWidget ?) with random data in it that reproduce the situation you have with your application.


Log in to reply