6

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!

Community
  • 1
  • 1
mosg
  • 12,041
  • 12
  • 65
  • 87
  • restoreState() returns true or false to indicate if it was successful. Does yours return true? – Arnold Spence Sep 07 '10 at 13:41
  • @Arnold Spence With all my `QTableView`'s and `QTreeView`'s I got *true* value when doing restoreState()... – mosg Sep 07 '10 at 14:41
  • When, in your startup, are you calling restoreState()? Some brief searching seems to suggest that you want to wait until the app is finished starting up and initializing everything. As an experiment, you may try adding a button or menu to your app to invoke the restore after it is running and see if it works out. – Arnold Spence Sep 08 '10 at 00:41
  • @Arnold Hm, still I'm thinking that it's only `QTableView` base class problem - Qt bug. Because, I tried to restore state with simple Qt Demo Examples (with QTableViews), and still no luck. I don't want to add extra code to my project, because, I think basic Qt features (save/restore headers state) have to work right! But I will try one more example, save/restore header settings from the memory while program run .. – mosg Sep 08 '10 at 06:55
  • Memory saveState/restoreState methods worked!.. – mosg Sep 09 '10 at 13:50
  • That's good. So there is something wrong with your code for persisting to a file with QSettings? – Arnold Spence Sep 10 '10 at 01:28

1 Answers1

10

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
  • 26,567
  • 29
  • 98
  • 120
  • 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 Sep 10 '10 at 11:47
  • Making question answered, because I had promissed! %) – mosg Apr 03 '12 at 19:26