Exclude moved column order from QHeaderView saveState / restoreState
I have a problem with saveState / restoreState QHeaderView . I have several QTableViews in my application. QTableView saves and restores QHeaderView settings, but for some QTableViews I would like to exclude the order of the moved sections from being saved to the ini file.
This means that saveState should keep the sorted column, sort indicator, column width, but not if the user has moved the column.
Is there a way to not save the moved columns?
Thank.
Regards, Mani
source to share
There is no easy way to do this. I suggest something like the following:
Use a vector to store the logicalIndexes
moved titles.
QVector<int> last;
Use a sectionMoved
signal to detect motion and save logicalIndex
to 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);
});
The syntax is so complex and ugly because in QHeaderView
there is one more
sectionMoved
, so this is necessary. If you don't know the new syntax, use the old one:
connect( ui->tableView->horizontalHeader(), SIGNAL(sectionMoved(int,int,int)), this, SLOT(yourSlot(int,int,int)));
But create yourSlot(int,int,int)
and make last.push_back(logicalIndex);
in this slot.
If you want saveState
, hide all the sections with logicalIndex
that you keep in the 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 pro file if you want to use new syntax and lambda.
source to share