Configuration files are read and written with the KConfig class. The default configuration file, kappnamerc, can be accessed with the KConfig object returned by the method KApplication::config(). You typically access this object with kapp->config(). (kapp is a macro defined in kapp.h that returns a pointer to the current KApplication object. A KDE application uses only one KApplication object; therefore, kapp can reliably be used from any source-code file that is part of your application.) Listings 7.8 and 7.9 show the code for KConfigDemo, a widget that demonstrates the use of KConfig.
Example 7.8. kconfigdemo.h: Class Declaration for KConfigDemo, a widget that demonstrates KConfig 1
2 1: #ifndef __KCONFIGDEMO_H__
3 2: #define __KCONFIGDEMO_H__
4 3:
5 4: #include <qlineedit.h>
6 5:
7 6: /**
8 7: * KConfigDemo
9 8: * Show how to access KDE configuration files with KConfig.
10 9: **/
11 10: class KConfigDemo : public QLineEdit
12 11: {
13 12: public:
14 13: KConfigDemo ();
15 14:
16 15: protected:
17 16: void closeEvent (QCloseEvent *qcloseevent);
18 17:
19 18: };
20 19:
21 20: #endif
22 |
KConfigDemo is a line editor widget that saves its text in a configuration file when the user closes the window, and it reloads it the next time the program is run. KConfigDemo is subclassed from QLineEdit, which does most of the work for you. You just need to add the configuration file access.
Example 7.9. kconfigdemo.cpp: Class Definition for KConfigDemo 1
2 1: #include <kapp.h>
3 2: #include <kconfig.h>
4 3:
5 4: #include "kconfigdemo.h"
6 5:
7 6: KConfigDemo::KConfigDemo () : QLineEdit (0)
8 7: {
9 8: kapp->config()->setGroup ("LineEditor");
10 9: setText (kapp->config()->readEntry ("Text", "Hello"));
11 10: }
12 11:
13 12: void
14 13: KConfigDemo::closeEvent (QCloseEvent *qcloseevent)
15 14: {
16 15: kapp->config()->setGroup ("LineEditor");
17 16: kapp->config()->writeEntry ("Text", text());
18 17: kapp->config()->sync();
19 18:
20 19: qcloseevent->accept();
21 20: } |
In the constructor, you look in the group LineEditor (line 8) for the key Text (line 9). (Note: If you had not specified a group with setGroup(), the Key, Value pair would have been written to the default, unnamed group.) readEntry() returns the value found to the right of Text= in the configuration file as a QString. In the event that the key Text does not appear at all—as is the case when the program is run for the first time—readEntry() returns Hello, the value specified as the default (line 9). You want to save the text when the user closes the window. To do this, reimplement the virtual method closeEvent(). This method is called when a close request is made, but before the window is actually closed. In this method, place the text of the widget (returned by text()) to the configuration file into the group LineEditor. The call on line 17 is very important. This call actually writes the new configuration information to disk. | KConfig caches the information in memory until the method KConfig::sync() is called, so be sure to call it before your application exits or else your settings will be lost. |
Finally, call qcloseevent->accept(), which lets Qt know that the widget is willing to grant the user's request and be closed. Listing 7.10 is for a main() function that you can use to test KConfigDemo. Figure 7.3 shows the program that is created—kconfigdemotest—running.
Example 7.10. main.cpp: A main() Function Suitable for Testing KConfigDemo 1
2 1: #include <kapp.h>
3 2:
4 3: #include "kconfigdemo.h"
5 4:
6 5: int
7 6: main (int argc, char *argv[])
8 7: {
9 8: KApplication kapplication (argc, argv, "kconfigdemotest");
10 9:
11 10: KConfigDemo *kconfigdemo = new KConfigDemo;
12 11:
13 12: kapplication.setMainWidget (kconfigdemo);
14 13:
15 14: kconfigdemo->show();
16 15: return kapplication.exec();
17 16: } |
KConfig can read and write many types of data, not just strings. For example, in the next section you read and write a QFont. The supported types are listed in the documentation (and header file) for KConfigBase. |