QTreeView middle column fill width space instead of last column

Sebtm picture Sebtm · Jan 24, 2012 · Viewed 12.3k times · Source

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.

Answer

synthesizerpatel picture synthesizerpatel · Jan 24, 2012

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