Results 1 to 20 of 24

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2011
    Posts
    18
    Thanked 6 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60 Maemo/MeeGo

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

    A very simple solution is this Prefs class - it uses a file, not QSettings.
    Qt Code:
    1. /*
    2.  * ApplicationPrefs.cpp
    3.  *
    4.  * Created on: Dec 31, 2009
    5.  * Author: TAMHAN
    6.  */
    7.  
    8. #include "ApplicationPrefs.h"
    9. #include <qDebug>
    10.  
    11. ApplicationPrefs::ApplicationPrefs()
    12. {
    13. // TODO Auto-generated constructor stub
    14. myPasswordChangedFlag=false;
    15. }
    16.  
    17. ApplicationPrefs::~ApplicationPrefs()
    18. {
    19. // TODO Auto-generated destructor stub
    20. //CANNOT SAVE HERE, OR THE RED KEY WILL PREVENT DATA SAVAGE
    21. }
    22.  
    23. void ApplicationPrefs::initPrefs()
    24. {
    25. QFile prefFile(QDir::currentPath() + "/prefs.tmgn");
    26. if(!prefFile.open(QIODevice::ReadOnly))
    27. {
    28. defaultPrefs();
    29. return;
    30. }
    31. QDataStream in(&prefFile);
    32. quint32 version;
    33. in >> version; // not needed for this v
    34. if(version==1)
    35. {
    36. in >> myLongIntCache;
    37. int sampleVal;
    38. in >> sampleVal;
    39. myProtectionType=(QProtectionType)sampleVal;
    40.  
    41. //Init new values from V2
    42. mySelfLockFlag=false;
    43. mySelfLockTime=60;
    44. myHideEmptyFieldsFlag=false;
    45. mySelfDestructIsArmedFlag=false;
    46. mySelfDestructPassword="";
    47. myMustUpdateSchemesFlag=true;
    48. myOldVersion=1;
    49. goto terminateInit;
    50. }
    51. in >> myLongIntCache;
    52. int sampleVal;
    53. in >> sampleVal;
    54. myProtectionType=(QProtectionType)sampleVal;
    55. //all below is new for V2
    56. in >> mySelfLockFlag;
    57. in >> mySelfLockTime;
    58. in >> myHideEmptyFieldsFlag;
    59. in >> mySelfDestructIsArmedFlag;
    60. in >> mySelfDestructPassword;
    61. in >> myMustUpdateSchemesFlag;
    62. myMustUpdateSchemesFlag=false;
    63. if(version==2)
    64. {//update for Nokia only
    65. myMustUpdateSchemesFlag=true;
    66. }
    67. terminateInit:
    68. prefFile.close();
    69. }
    70.  
    71. void ApplicationPrefs::savePrefs()
    72. {
    73. QFile prefFile(QDir::currentPath() +"/prefs.tmgn");
    74. if(!prefFile.open(QIODevice::WriteOnly))
    75. {
    76. defaultPrefs();
    77. return;
    78. }
    79. QDataStream out(&prefFile);
    80. quint32 version=3;
    81. out << version; // not needed for this v
    82. out << myLongIntCache;
    83. int sampleVal=myProtectionType;
    84. out << sampleVal;
    85. //all below is new for V2
    86. out << mySelfLockFlag;
    87. out << mySelfLockTime;
    88. out << myHideEmptyFieldsFlag;
    89. out << mySelfDestructIsArmedFlag;
    90. out << mySelfDestructPassword;
    91. out << myMustUpdateSchemesFlag;
    92. prefFile.close();
    93. }
    94.  
    95. void ApplicationPrefs::defaultPrefs()
    96. {
    97. myLongIntCache=1; //0 is reserved as magic number for XFER to other box
    98.  
    99. mySelfLockFlag=false;
    100. mySelfLockTime=60;
    101. myHideEmptyFieldsFlag=false;
    102. mySelfDestructIsArmedFlag=false;
    103. mySelfDestructPassword="";
    104.  
    105. myMustUpdateSchemesFlag=true;
    106. myOldVersion=0;
    107. }
    108.  
    109. bool ApplicationPrefs::prefsExist()
    110. {
    111. QFile prefFile(QDir::currentPath() +"/prefs.tmgn");
    112. if(prefFile.exists())
    113. {
    114. return true;
    115. }
    116. else
    117. {
    118. return false;
    119. }
    120. }
    121.  
    122. quint64 ApplicationPrefs::getUniqueID()
    123. {
    124. quint32 retval=myLongIntCache;
    125. myLongIntCache++;
    126. savePrefs();
    127. return retval;
    128. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.

    But it doesn't help you in storing settings for different types of objects, you still need to put everything in your ApplicationPrefs class and this class has to know every object it is to work with.
    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. #3
    Join Date
    Oct 2011
    Posts
    18
    Thanked 6 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60 Maemo/MeeGo

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

    Quote Originally Posted by wysota View Post
    But it doesn't help you in storing settings for different types of objects, you still need to put everything in your ApplicationPrefs class and this class has to know every object it is to work with.
    If this is the issue, I would proceed to trying XML. But the core issue always remains: the reading routine needs to know what it will read in.

  4. #4
    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?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


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

  7. #7
    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, 10:32
  2. Save log file to "all user\application data\"
    By stella1016 in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2011, 16:01
  3. Replies: 1
    Last Post: 13th June 2011, 13:06
  4. How to find the location of the user?
    By nikhilqt in forum Qt Programming
    Replies: 7
    Last Post: 14th December 2010, 18: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, 12: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.