29

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.

Sebtm
  • 7,002
  • 8
  • 29
  • 32

1 Answers1

50

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

Stormenet
  • 25,926
  • 9
  • 53
  • 65
synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
  • Yes. I tried, but the last column always takes up too much space – Sebtm Jan 24 '12 at 11:34
  • I would suspect that would only be the case if if you set setStretchLastSection(true), otherwise if it is set false it will shrink down and let INDEX_COLUMN_NAME stretch open. If it still doesn't work, make sure it's false and you could try setting the other columns to QHeaderView::ResizeToContents – synthesizerpatel Jan 24 '12 at 11:39
  • 9
    Two quick comments: first, in Qt 5 setResizeMode has been refactored to setSectionResizeMode. Second, setStrechLastSection must be called BEFORE any other call to setSectionResizeMode, if not, it doesn't work correctly. – cbuchart Jan 09 '14 at 16:30