Results 1 to 4 of 4

Thread: QSettings with QTableWidget

  1. #1
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question QSettings with QTableWidget

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with QTableWidget

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings with QTableWidget

    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...

    Qt Code:
    1. WindowSettings.setValue("itemOneText", windowGUI->tableWidget->item(0,0)->text());
    2. WindowSettings.setValue("itemOneIcon", windowGUI->tableWidget->item(0,0)->icon());
    3.  
    4. WindowSettings.setValue("itemTwoText", windowGUI->tableWidget->item(1,0)->text());
    5. WindowSettings.setValue("itemTwoIcon", windowGUI->tableWidget->item(1,0)->icon());
    To copy to clipboard, switch view to plain text mode 

    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...

    Qt Code:
    1. windowGui->tableWidget->item(0,0)->setText(WindowSettings.value("itemOneText")).toString();
    To copy to clipboard, switch view to plain text mode 

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with QTableWidget

    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).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 7
    Last Post: 18th September 2009, 10:59
  2. QSettings and QGLWidgets
    By Rayven in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2008, 18:01
  3. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.