Search code examples
c++qt4.8

QTreeView header


Using QTreeView to render the content of a derived implementation of QAbstractItemModel.

The current implementation shows all 4 headers in QTreeView. This works fine.

What is the preferred way to change which headers are display and the order they are displayed at runtime? In one scenario I would like to display column\header 1,2,4 and in another scenario 1,4,3.

The following works for showing and hiding but does not handles changing order:

if ( scenario1 )
{
  ui->tree->hideColumn(3)
  ui->tree->showColumn(2)
}
else if (scenario2 )
{
  ui->tree->hideColumn(2)
  ui->tree->showColumn(3)
}

Solution

  • Call the header() function to get the QHeaderView, then swapSections(1, 0) for example will swap the first and second column.