Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HowTo make restoreState() and saveState() work correctlly to QTableView class?

First of all, I wanted to say, that my problem was already discuss here, on SO, and here it is. But the answers are not the good ones...

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

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

But, nothing happens! The columns widths has standard width. They are not changed with my stored values! :(

Thanks!


PS: This problem not become apparent with QTreeView class. With QTreeView all is ok!

like image 765
mosg Avatar asked Sep 07 '10 08:09

mosg


1 Answers

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

With Qt-Designer, I put a QTableView (named tbvTest) on my form.

In the constructor of my form, here is what I've written :

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 the ApplicationName, OrganizationName and OrganizationDomain)

In the destructor of my form, here is what I've written :

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

    delete ui;
}

If I run the application and change the column width, quit the app and run it again, the column widths are correctly restored.

Is there something I'm doing different from you ?

like image 98
Jérôme Avatar answered Nov 08 '22 13:11

Jérôme