views:

78

answers:

1

Hello, there.

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!

+1  A: 

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 ?

Jérôme
@Jérôme Thanks for the great answer, I'll try your example as soon as possible. I made all the same things, but a little bit complex. After I get the success result, I'll mentions here about it!
mosg