Results 1 to 7 of 7

Thread: QSettings, custom format and class methods as read/write functions.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Location
    Berlin, Germany
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default QSettings, custom format and class methods as read/write functions.

    Hello everybody,

    I've run into one problem, but first of all I'll describe what I tried to achieve. I'm writing a multiuser client app with server-side authentication. This app has to store some user's settings on his computer. This settings need to be encrypted with a key, which is stored on the server-side and is sent after user's authentication. Every user has its own unique key.

    I came up with the following solution: I've created a wrapper class that contains a QSettings object, an encryption key, setValue(), value(), sync(), writeFile() and readFile() methods. The methods setValue(), value() and sync() are calling the corresponding QSettings's methods. writeFile() and readFile() are declared according to QSettings::WriteFunc and QSettings::ReadFunc and do writing/reading settings to/from the file. They also encrypt/decrypt the file if the encryption key is not NULL.

    The class is inside a namespace as all other app stuff.

    Now the stripped down header file:
    Qt Code:
    1. namespace CLIENT {
    2.  
    3. class Settings: public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. Settings(const QString &user, const Key &k, QObject *parent = 0);
    9. void setValue(const QString &key, const QVariant &value);
    10. void sync();
    11. QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
    12. ~Settings();
    13.  
    14. private:
    15. QSettings::Format f;
    16. Key key;
    17.  
    18. bool readFile(QIODevice &device, QSettings::SettingsMap &map);
    19. bool writeFile(QIODevice &device, const QSettings::SettingsMap &map);
    20. };
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 
    And source file:
    Qt Code:
    1. namespace CLIENT {
    2.  
    3. Settings::Settings(const QString &user, const Key &k, QObject *parent)
    4. : QObject(parent), key(k)
    5. {
    6. f = QSettings::registerFormat("settings", (QSettings::ReadFunc)&Settings::readFile, (QSettings::WriteFunc)&Settings::writeFile);
    7.  
    8. QString appname = QCoreApplication::applicationName();
    9. if (!user.isEmpty())
    10. appname.append(".").append(user);
    11.  
    12. s = new QSettings(f, QSettings::UserScope, QCoreApplication::organizationName(), appname, parent);
    13. }
    14.  
    15. void Settings::setValue(const QString &key, const QVariant &value)
    16. {
    17. s->setValue(key, value);
    18. }
    19.  
    20. void Settings::sync()
    21. {
    22. s->sync();
    23. }
    24.  
    25. QVariant Settings::value(const QString &key, const QVariant &defaultValue) const
    26. {
    27. return s->value(key, defaultValue);
    28. }
    29.  
    30. Settings::~Settings()
    31. {
    32. delete s;
    33. }
    34.  
    35. bool Settings::readFile(QIODevice &device, QSettings::SettingsMap &map)
    36. {
    37. // Reads data from device, decrypts it with the key and saves to map
    38. }
    39.  
    40. bool Settings::writeFile(QIODevice &device, const QSettings::SettingsMap &map)
    41. {
    42. #ifdef DEBUG
    43. qDebug() << Q_FUNC_INFO << endl << "Writing map:" << map;
    44. #endif // DEBUG
    45. // Saves map to QByteArray, encrypts it with the key and writes to device
    46. }
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

    Now about the problem:
    I get a segfault at line 350 in qmap.h:
    Qt Code:
    1. inline const_iterator constBegin() const { return const_iterator(e->forward[0]); }
    To copy to clipboard, switch view to plain text mode 
    which traces back to the line 43 of my code (the first access to map).

    The debugger tells me that the map is <not in scope>, the device seems to be <not in scope> too.

    Am I doing something wrong?
    Is there any way I can get my class working with QSettings or do I have to write it from scratch?

    I'm using Qt 4.6.2 (minGW).
    Last edited by leppa; 31st March 2010 at 11:44. Reason: corrected a little typo
    With best regards,
    Oleksii Serdiuk <contacts[at]oleksii[dot]name>

Similar Threads

  1. cannot access QLineEdit text in other methods of the class...
    By Leoha_Zveri in forum Qt Programming
    Replies: 2
    Last Post: 29th September 2009, 12:07
  2. Opendocument format read/write *.odt
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 17th September 2008, 00:48
  3. QSettings , read only avaiable?
    By patrik08 in forum Qt Programming
    Replies: 5
    Last Post: 18th November 2007, 15:21
  4. QSettings, my own format of config-file
    By IPFreely in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2007, 20:07
  5. Replies: 4
    Last Post: 1st February 2006, 17:17

Tags for this Thread

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.