3
votes

I have a problem with saveState/restoreState of QHeaderView. I have several QTableViews in my application. The QTableView saves and restores it's QHeaderView settings, but for some QTableViews i'd like to exclude the moved section order from beeing saved to the ini-file.

This means, saveState should save sorted column, sort indicator, column width but not if a user has moved a column.

Is there a way to not save the moved columns?

Thank you.

Regards, Mani

1

1 Answers

0
votes

There is no easy way to do this. I suggest something like next:

Use vector to store logicalIndexes of moved headers.

QVector<int> last;

Use sectionMoved signal to detect moving and store logicalIndex in vector:

connect( ui->tableView->horizontalHeader(),static_cast<void (QHeaderView::*)(int,int,int)>(&QHeaderView::sectionMoved),[=](int logicalIndex, int oldVisualIndex, int newVisualIndex)
{//with lambda
    //you can also provide shecking is current logicalIdnex already exist in vector
    last.push_back(logicalIndex);
 });

Syntax is so complicate and ugly because there is another sectionMoved in QHeaderView, so it is necessary. If you don't know new syntax, use old:

connect( ui->tableView->horizontalHeader(), SIGNAL(sectionMoved(int,int,int)), this, SLOT(yourSlot(int,int,int)));

But create yourSlot(int,int,int) and do last.push_back(logicalIndex); in this slot.

When you want saveState, hide all sections with logicalIndex which you store in vector and save it:

QByteArray array;
for(int i = 0; i < last.size(); i++)
{
    ui->tableView->horizontalHeader()->hideSection(last.at(i));
}
array = ui->tableView->horizontalHeader()->saveState();

Add CONFIG += c++11 to the pro file if you want use new syntax and lambda.