PDA

View Full Version : QWidget Settings advanced idea



MarkoSan
15th March 2008, 16:19
Hi to all!

I got an idea how to save settings for a widget, but I do no know how to code it. Here is my problem:

In my app I have several widgets (mostly QWidget) - settings pages under main settings window (also derived from QWidget). Every settings page has QLabels, QTextEdits, QComboboxes, etc ... Now, when I click save, the save slot connected to signal clicked must get contens of all textedits and other value input widgets and save them in a object, which class is subclassed from QSettings. At the end, this app must notify sister parent application that new settings has been apllied to ini file and client must reeread settings without terminating and reruning it. Is this possible?

jpn
15th March 2008, 18:00
Yes, it's possible. :)

MarkoSan
15th March 2008, 18:13
How? I do not want to write the code for me, but I just want some guidelines ...

jpn
15th March 2008, 18:24
I'd recommend taking a look at functions like QWidget::saveGeometry() and QMainWindow::saveState() and their corresponding restore() counterparts. I suggest you implement similar interface for your settings page. This way the main window doesn't have to do anything else but simply call saveState() and restoreState() for every settings page. The settings page itself would be responsible for saving and restoring anything it needs.

MarkoSan
15th March 2008, 18:48
But I do not need to save geometry of windows, but contens of QTextEdits inside settings page, ie, the contens (mainly text() method) of child objects (QTextEdit, QLabel, ...) of settings page. I've made a QSettings subclassed class named CAppSettings, which header is as follows:
#ifndef CAPPLICATIONSETTINGS_H_
#define CAPPLICATIONSETTINGS_H_

// qt includes
#include <QSettings>
#include <QList>
#include <QApplication>
#include <QObject>
#include <QVariant>

// custom includes
#include "globals.h"

typedef struct
{
QString strHub;
QString strKey;
QVariant varValue;
} settingsType;

/*!
* class responsible for whole EROSystem Settings
*/
class CApplicationSettings : public QSettings
{
Q_OBJECT

public:
CApplicationSettings(const QString &strFileName=strIniFileName,
Format format=IniFormat,
QObject *pParent=0);
~CApplicationSettings();

private slots:
void saveSettings(); // method for saving settings
void loadSettings(); // method for loading settings

private:
QList<settingsType> m_SettingsValues;
};

#endif /*CAPPLICATIONSETTINGS_H_*/

Now, how do I extract control widget (QLineEdit, QLabel) pointer from pParent? This is my first question. And then, how do I notify about new settings client application, whose flowchart depends on settings from:
private:
QList<settingsType> m_SettingsValues;

jpn
15th March 2008, 18:57
I didn't say anything about saving geometry. I just recommended implementing a similar interface to your settings pages.

MarkoSan
25th March 2008, 00:44
Ok, jpn and all other kind experts, I am back to this section of my project. I've setup an INI file and it is created upon constructor call like it should. Now, I do not know how to extract all key names for a key. For example, this is example ini file:
[GeometrySettings]
x1=100
x2=100
y1=200
y2=200
background=blue

Now, I have been reading QSettings docs for a while, but I simply do not know how to extract strings x1, x2, y1, y2 (keys) from GeometrySettings hub. For example, I need a call that will for example call:
QStringList strKeys=QSettings::keys(QString("GeometrySettings")); return strings "x1', "x2", "y1", "y2" string in string list. I cannot find that function. So, does anyone has idea how to implement it. The problem is that I write uniform QSettings subclassed class that will be used by all applications in my software project and this class does not know how many and which keys are under some settings hub. Please help!

jpn
25th March 2008, 07:47
settings.beginGroup("GeometrySettings");
qDebug() << settings.childKeys();
settings.endGroup();

mchara
27th March 2008, 06:59
Hi, if you have lots of widgets that have similar settings, maybe consider some more automatic mechanism.
We have overridden spinboxes and splitters in our application that generates keys using kind of path from mainwindow to given widget, and saves/restores own states on show/hide.
we had to write code for view hundreds of widgets once instead of view hundreds.
You could add such mechanism to textedits, labels and combos and eventually turn it on/off for right widgets - it was lot less work to do in my case.

MarkoSan
27th March 2008, 09:41
Well, I've developed separate class that handles settings and now it is used heavely. Thanks for hints ...