Page 1 of 2 12 LastLast
Results 1 to 20 of 24

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

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

    Default Need to find a way to save user inputs.

    So I am making a kind of large app,which will be used by many people.
    In a seperate widget(class) I have put a tablewidget which is empty.
    User can use this tool to store data and save it.

    What I mean by "save", is that once some text is entered,it will be there next time the user opens the program.
    Then he might edit all the fields,add/remove colums/rows etc..

    The key part here is to make the fields saveable

    I have no idea how to do this..maybe there is some text file,so that I save all the data there,then read from it..or something else?

    Please help.

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

    And how does this differ from this thread?
    http://www.qtcentre.org/threads/4520...idget-for-this
    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
    Jan 2011
    Posts
    128
    Thanks
    2

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

    Tables are different than lineedits.

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

    Are they...?
    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.


  5. #5
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

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

    Well,for the Lineedit I do: set.setValue("text1", ui->le1->text()); then ui->le1->setText(set.value("text1").toString());
    While the Tablewidget doesn't have those functions( text(),setText() )..

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    QTableWidget has lines and columns that can hold QTableWidgetItems which items have setText member function.

  7. #7
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

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

    Well,this is what I have so far:
    Qt Code:
    1. void Victims::save()
    2. {
    3. QSettings set("vic","tims");
    4. set.beginGroup("setting");
    5.  
    6. int grows = ui->tw->rowCount();
    7. int gcols = ui->tw->columnCount();
    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. tmp = widgetItem(i,j)->text(); //tmp is a string.
    16. if ( tmp.isEmpty() )
    17. break;
    18. }
    19. set.setValue("victims", tmp); //tmp will be the string that will be put into set.value(),correct?
    20. }
    21. set.endGroup();
    22. }
    To copy to clipboard, switch view to plain text mode 

    However it gives me an error: widgetItem cannot be used as a function :/
    Am I on the right way?

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

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

    widgetItem is already a pointer to the item at i,j index so use it without (i,j), just: widgetItem->setText("blabla");

    LE: don't forget to check for null, because, if i remember correctly, if the indexes are out-of range that item function returns null

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

    I suggest you come up with a universal solution that will work regardless of the item you want to store/restore. Provide classes or functions that will be called depending on the item/widget type you need to store and have some infrastructure that will call those classes/functions. You basically need something similar to QMainWindow::saveState() and QMainWindow::restoreState().
    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.


  10. #10
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

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

    Right now I cannot put a simple table into settings,when you tell me to make a universal functionality..
    I suppose I'd better do it seperatly and if I have time I will think of what u said.

    So,my thought was to do this:
    Loop through the rowcount() and create as many arrays of string.
    Then use that nested loop(intthe code I pasted) and after each row,store the text into one array.
    Then in restore,I would re-loop(the table) to put the arrays as text..
    Is that ok?

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

    Quote Originally Posted by "BumbleBee" View Post
    Right now I cannot put a simple table into settings,when you tell me to make a universal functionality..
    You are considering the two as different problems whereas it is the same problem, it's just a matter of having a single solution doing all you want today and all you might want tomorrow or having a tightly coupled solution which works for what you want today but will require the same amount of work for each of the things you'll want tomorrow and the day after.
    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.


  12. #12
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

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

    Hmm..I can't really come up with an idea that would work for any widget.
    Can you give me a hint?

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

    I would probably have some kind of handler interface that I would then implement for each type of class I'd want to serialize and I'd have some class for managing the whole process that would take the result from all the implementations and store them in some kind of storage (like QSettings or QIODevice). We actually have that kind of mechanism in our software with the following interface:
    Qt Code:
    1. #ifndef ISTATE_H
    2. #define ISTATE_H
    3.  
    4. #include <QObject>
    5.  
    6. class IState : public QObject {
    7. Q_OBJECT
    8. public:
    9. IState(QObject *parent = 0) : QObject(parent){}
    10. virtual ~IState(){}
    11. virtual QByteArray saveState() const = 0;
    12. virtual void restoreState(const QByteArray &ba) = 0;
    13. virtual QObject *object() const = 0;
    14. };
    15.  
    16. #endif // ISTATE_H
    To copy to clipboard, switch view to plain text mode 

    We call saveState() for each of the objects monitored and transfer the resulting blob over the wire to a backend we have.
    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.


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

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

    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.


  16. #16
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

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

    Quote Originally Posted by wysota View Post
    I would probably have some kind of handler interface that I would then implement for each type of class I'd want to serialize and I'd have some class for managing the whole process that would take the result from all the implementations and store them in some kind of storage (like QSettings or QIODevice). We actually have that kind of mechanism in our software with the following interface:
    Qt Code:
    1. #ifndef ISTATE_H
    2. #define ISTATE_H
    3.  
    4. #include <QObject>
    5.  
    6. class IState : public QObject {
    7. Q_OBJECT
    8. public:
    9. IState(QObject *parent = 0) : QObject(parent){}
    10. virtual ~IState(){}
    11. virtual QByteArray saveState() const = 0;
    12. virtual void restoreState(const QByteArray &ba) = 0;
    13. virtual QObject *object() const = 0;
    14. };
    15.  
    16. #endif // ISTATE_H
    To copy to clipboard, switch view to plain text mode 

    We call saveState() for each of the objects monitored and transfer the resulting blob over the wire to a backend we have.
    So.The qbytearray works like a generic string type that stores any kind of data and then puts it where needed,right?

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

    Not a string type. Just a block of data. The IState subclass is responsible for interpretting it and applying to the object it operates on (be it a table widget or a line edit).
    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.


  18. #18
    Join Date
    Jan 2011
    Posts
    128
    Thanks
    2

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

    But still,to put a table into a ba,I need to loop though the table right?

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

    Yes. That's what a subclass of IState would be responsible for in my case.
    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.


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

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.