PDA

View Full Version : QSettings with QTableWidget



mikey33
19th November 2009, 11:29
I have dealt a bunch with QSettings but it mainly revolved around saving/loading variables and stylesheets and such but I have just come across QTableWidget and I'm wondering if anyone has an idea on how to say begin saving the indexes or the tableWidget itself...

Basically I have a window which allows you to enter the name and select an icon for a new tablewidget item. From there if you click the "Add" button then the item is inserted into the table through the code...

example->tableWidget->setItem(newRow, 0, newItem);

newRow just increments from 0 to 3 since I maxed out the tableWidget at 4 items tops. All that works but I need to find a way to save/load the item via QSettings.

From reading the documentation I noticed "beginReadArray" and "beginWriteArray" and such but I'm just wondering on the opinions of the more advanced users of QT if there is an easier way of saving the items that are in the tablewidget perhaps a way as easy as saving/loading variables.

Example->Item One would be placed at (0,0), Item Two at (1,0) Item Three at (2,0) etc so I was just confused if I can save the tableWidget itself or have to somehow manually save the indexes and items.

Sorry if I confused anyone. Examples would help a ton! Thanks again! :)

high_flyer
19th November 2009, 12:05
Your items probably have some properties,like text etc.
You can save those with QSettings when you add the item to the table.
As keys you can use some kind of a convention, like "item_0_text", item_0_value" or similar, so you can save various properties of the same item.
Them when reading the settings, you can build your items again.

Another way, maybe more elegant, is to encapsulate your items, as objects, in a QVariant.
At least theoretically, you really could just do a setValue() with a QVariant object that holds your table item.

mikey33
19th November 2009, 19:31
so for instance if the user is able to select an icon for the TableWidget item and then enter the text for the item you would technically need settings to say save...



WindowSettings.setValue("itemOneText", windowGUI->tableWidget->item(0,0)->text());
WindowSettings.setValue("itemOneIcon", windowGUI->tableWidget->item(0,0)->icon());

WindowSettings.setValue("itemTwoText", windowGUI->tableWidget->item(1,0)->text());
WindowSettings.setValue("itemTwoIcon", windowGUI->tableWidget->item(1,0)->icon());


etc...

I haven't had time to really mess around with converting from QVariant back to say string or image such as the standard .toString() but I would imagine the load would be something like...



windowGui->tableWidget->item(0,0)->setText(WindowSettings.value("itemOneText")).toString();


I'm sure the above code is definitely not syntactically correct I'm just throwing ideas down before hand on a break between my classes!

then again if what I understand of what your saying from encapsulate then I'm able to just save the TableWidget index location or item as an object itself which the object itself say the item at index(0,0) will be saved with both the text, icon per say it would potentially save the actually index location box of the tablewidget no matter if an item was inserted into that location or not, if any short simple examples could demonstrate that then it would be awesome as it would say a ton of time and less code, maybe that might work, any ideas from anyone? :confused:

high_flyer
20th November 2009, 14:29
For the first approach, your example it correct in principal.
The only thing is that instead of an icon object, you would save the image file location or resource name.

For the second approach, have a look at the QVariant docs, where it talks about userTypes and QVariant::fromValue(value).