Search code examples
qtheadersizeqtreeview

QTreeView middle column fill width space instead of last column


How can do this:

middle column fill space

I already tried this way:

view->header()->setResizeMode(INDEX_COLUMN_SKU, QHeaderView::Interactive);
view->header()->setResizeMode(INDEX_COLUMN_NAME, QHeaderView::Stretch);
view->header()->setResizeMode(INDEX_COLUMN_QUANTITY, QHeaderView::Interactive);
view->header()->setResizeMode(INDEX_COLUMN_PRICE, QHeaderView::Interactive);

but does not work.


Solution

  • Did you remember to view->header()->setStretchLastSection(false)?

    Here are some examples:

    Default Behavior

    Default behavior

    Just disabling stretch on the last column:

    treeView->header()->setStretchLastSection(false);
    

    Stretch_Disabled

    Both attributes combined:

    treeView->header()->setStretchLastSection(false);
    treeView->header()->setResizeMode(1, QHeaderView::Stretch);   
    

    From QT5 onwards:

    treeView->header()->setStretchLastSection(false);
    treeView->header()->setSectionResizeMode(1, QHeaderView::Stretch); //! qt5 api change 
    

    Stretch_Disabled+Resizable_Second_Col