Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: Need to find a way to save user inputs.

  1. #21
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Need to find a way to save user inputs.

    Qt Code:
    1. void Victims::load()
    2. {
    3. int grows = ui->tw->rowCount();
    4. int gcols = ui->tw->columnCount();
    5.  
    6. QSettings set("victims");
    7. set.beginGroup("setting");
    8.  
    9. for(int i = 0; i < grows ;i++)
    10. {
    11.  
    12. for(int j = 0; j < gcols ;j++)
    13. {
    14. QTableWidgetItem* widgetItem = ui->tw->item(grows, gcols);
    15. QString ex = set.value("victims", str).toString();
    16. widgetItem->setText(ex);
    17. }
    18.  
    19. }
    20.  
    21. set.endGroup();
    22. }
    23.  
    24.  
    25. void Victims::save()
    26. {
    27. int grows = ui->tw->rowCount();
    28. int gcols = ui->tw->columnCount();
    29.  
    30. QSettings set("victims");
    31. set.beginGroup("setting");
    32.  
    33. for(int i = 0; i < grows ;i++)
    34. {
    35.  
    36. for(int j = 0; j < gcols ;j++)
    37. {
    38. QTableWidgetItem* widgetItem = ui->tw->item(grows, gcols);
    39. str = widgetItem->text();
    40. }
    41.  
    42. }
    43. set.setValue("victims", str);
    44. set.endGroup();
    45. }
    To copy to clipboard, switch view to plain text mode 

    Can you tell me what I did wrong here?

  2. #22
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need to find a way to save user inputs.

    You should iterate over i and j instead of grows and gcols.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #23
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Need to find a way to save user inputs.

    You can iterate through the ItemModel (instead of table widget items). Here a some what generic code, with which you can save & restore all item data roles. With a minor modifications and use of recursion this code can also be used to save TreeWidgets

    Qt Code:
    1. void Victims::load()
    2. {
    3. QAbstractItemModel* model = ui->tw->model();
    4. for(int row = 0; row < model->rowCount(); row++)
    5. {
    6. settings->beginGroup(QString("Row-%1").arg(row));
    7. for(int col = 0; col < model->columnCount(); col++)
    8. {
    9. settings->beginGroup(QString("Column-%1").arg(col));
    10. QModelIndex item = model->index(row, col);
    11. for(int role = 0; role < Qt::UserRole; role++)
    12. {
    13. model->setData(item, settings->value(QString("ItemDataRole-%1").arg(role))); // Restore from file
    14. }
    15. settings->endGroup();
    16. }
    17. settings->endGroup();
    18. }
    19. }
    20.  
    21.  
    22. void Victims::save()
    23. {
    24. QAbstractItemModel* model = ui->tw->model();
    25. for(int row = 0; row < model->rowCount(); row++)
    26. {
    27. settings->beginGroup(QString("Row-%1").arg(row));
    28. for(int col = 0; col < model->columnCount(); col++)
    29. {
    30. settings->beginGroup(QString("Column-%1").arg(col));
    31. QModelIndex item = model->index(row, col);
    32. for(int role = 0; role < Qt::UserRole; role++)
    33. {
    34. settings->setValue(QString("ItemDataRole-%1").arg(role), model->data(item, role)); // Save to file
    35. }
    36. settings->endGroup();
    37. }
    38. settings->endGroup();
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

  4. #24
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

    Default Re: Need to find a way to save user inputs.

    Quote Originally Posted by Santosh Reddy View Post
    You can iterate through the ItemModel (instead of table widget items). Here a some what generic code, with which you can save & restore all item data roles. With a minor modifications and use of recursion this code can also be used to save TreeWidgets

    Qt Code:
    1. void Victims::load()
    2. {
    3. QAbstractItemModel* model = ui->tw->model();
    4. for(int row = 0; row < model->rowCount(); row++)
    5. {
    6. settings->beginGroup(QString("Row-%1").arg(row));
    7. for(int col = 0; col < model->columnCount(); col++)
    8. {
    9. settings->beginGroup(QString("Column-%1").arg(col));
    10. QModelIndex item = model->index(row, col);
    11. for(int role = 0; role < Qt::UserRole; role++)
    12. {
    13. model->setData(item, settings->value(QString("ItemDataRole-%1").arg(role))); // Restore from file
    14. }
    15. settings->endGroup();
    16. }
    17. settings->endGroup();
    18. }
    19. }
    20.  
    21.  
    22. void Victims::save()
    23. {
    24. QAbstractItemModel* model = ui->tw->model();
    25. for(int row = 0; row < model->rowCount(); row++)
    26. {
    27. settings->beginGroup(QString("Row-%1").arg(row));
    28. for(int col = 0; col < model->columnCount(); col++)
    29. {
    30. settings->beginGroup(QString("Column-%1").arg(col));
    31. QModelIndex item = model->index(row, col);
    32. for(int role = 0; role < Qt::UserRole; role++)
    33. {
    34. settings->setValue(QString("ItemDataRole-%1").arg(role), model->data(item, role)); // Save to file
    35. }
    36. settings->endGroup();
    37. }
    38. settings->endGroup();
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 
    Thanks for putting all this code.But the save slot doesn't work..after loading,the table is empty!!!!

Similar Threads

  1. How to implement functionalities of other devices/inputs in Qt
    By qt_user in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 9th October 2011, 11:32
  2. Save log file to "all user\application data\"
    By stella1016 in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2011, 17:01
  3. Replies: 1
    Last Post: 13th June 2011, 14:06
  4. How to find the location of the user?
    By nikhilqt in forum Qt Programming
    Replies: 7
    Last Post: 14th December 2010, 19:22
  5. Restrict user from entering space in Save As File Name
    By jyoti kumar in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2007, 13:47

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.