How to make restoreState () and saveState () work correctly with the QTableView class?

First of all, I wanted to say that my problem has already been discussed here, on SO and here . But the answers are not good ...

So here is the problem : I have a QTableView class with a simple model associated with the tableView->setModel(model); method tableView->setModel(model); . For example, I have 4-5 columns. I launched my project application and made some changes with the width of the columns. After I clicked Exit, my project application will save the data state tableView->horizontalHeader()->saveState(); with QSettings to file. And when I run my application again, it does something like this:

 tableView->horizontalHeader()->restoreState(/* data from settings ini file */); 

But nothing happens! Column widths have a standard width. They do not change with the saved values ​​!: (

Thanks!


PS: This problem does not show up in the QTreeView class. QTreeView is OK!

+4
source share
1 answer

I tried to reproduce your problem, but everything works fine for me. Here is what I did:

With Qt-Designer, I put a QTableView (called tbvTest ) in my form.

In the constructor of my form, this is what I wrote:

 Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); ui->tbvTest->setModel(new TableModel); QSettings MySetting(QSettings::IniFormat, QSettings::UserScope, "Test"); QByteArray MyArray = MySetting.value("column_width", "").toByteArray(); ui->tbvTest->horizontalHeader()->restoreState(MyArray); } 

(note that in my main.cpp I set ApplicationName , OrganizationName and OrganizationDomain )

In the destructor of my form, here is what I wrote:

 Widget::~Widget() { QByteArray MyArray = ui->tbvTest->horizontalHeader()->saveState(); QSettings MySetting(QSettings::IniFormat, QSettings::UserScope, "Test"); MySetting.setValue("column_width", MyArray); delete ui; } 

If I launch the application and change the column width, close the application and run it again, the column width will be restored correctly.

Is there anything I do from you?

+7
source

All Articles