Difference between revisions of "Development/Tutorials/Programming Tutorial KDE 4/Using KConfig"

Jump to: navigation, search
(Prerequisites: Fixed link)
m (Text replace - "<code cppqt n>" to "<syntaxhighlight lang="cpp-qt" line>")
Line 50: Line 50:
 
==The Code==
 
==The Code==
 
===mainwindow.h===
 
===mainwindow.h===
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#ifndef MAINWINDOW_H
 
#ifndef MAINWINDOW_H
 
#define MAINWINDOW_H
 
#define MAINWINDOW_H
Line 81: Line 81:
  
 
===mainwindow.cpp===
 
===mainwindow.cpp===
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
 
#include "mainwindow.h"
 
#include "mainwindow.h"
 
#include "tutorial4.h"
 
#include "tutorial4.h"
Line 147: Line 147:
  
 
===main.cpp===
 
===main.cpp===
<code cppqt n>
+
<syntaxhighlight lang="cpp-qt" line>
  
 
#include "mainwindow.h"
 
#include "mainwindow.h"

Revision as of 20:32, 29 June 2011

noframe
 
Warning
The subject matter that I was going to cover in this tutorial is already covered in Development/Tutorials/KConfig and so this tutorial will probably be removed.


Introtokdetutorial4.png

Abstract

Author: Fabian Korak

Author: Matt Williams(presets)

In this tutorial, we're going to be showing you how to utilise KConfig XT in an application. It is recommended that you read Development/Tutorials/Using KConfig XT before continuing in order to familiarise yourself with the framework.

Prerequisites

KConfig XT

At first we make new File called tutorial4.kcfg within the source directory of your project.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE kcfg SYSTEM "http://www.kde.org/standards/kcfg/1.0/kcfg.xsd"> <kcfg> <kcfgfile name="kjotsrc"/> <include>kglobalsettings.h</include> <group name="kjots"> <entry name="showAction" type="Bool"> <label>Whether setupAction is called</label> <default>true</default> </entry> </group> </kcfg>

This code makes a setting of the type bool, which determines whether we want to do something. Then we set the default value to be true.

Always start the value of <entry name=""> in lower case since KConfig XT sets them to lower case within the generated headers either way. If you don't this could give you some nearly untraceable errors

Now we make a new File called tutorial4.kcfgc.

File=tutorial4.kcfg ClassName=tutorial4 Singleton=true Mutators=true

This should set up your configuration for now.

The Code

mainwindow.h

  1 #ifndef MAINWINDOW_H
  2 #define MAINWINDOW_H
  3 
  4 #include <KXmlGuiWindow>
  5 #include <KTextEdit>
  6 
  7 
  8 class MainWindow : public KXmlGuiWindow
  9 {
 10 
 11 	public:
 12 		MainWindow(QWidget *parent=0);
 13 	private:
 14 		KTextEdit* textArea;
 15 		void setupActions();
 16 		void setupConfig();
 17 		void readConfig();
 18 		void saveSettings();
 19 		int m_showAction;
 20 
 21 };
 22 
 23 #endif
 24 
 25 
 26 </code>
 27 
 28 There are some new voids in here, as well as m_showAction witch determines the value stored in KConfig XT. Despite that value being a bool we can actually use an integer here, since the type is converted either way.
 29 
 30 ===mainwindow.cpp===
 31 <syntaxhighlight lang="cpp-qt" line>
 32 #include "mainwindow.h"
 33 #include "tutorial4.h"
 34 
 35 #include <KApplication>
 36 #include <KAction>
 37 #include <KLocale>
 38 #include <KActionCollection>
 39 #include <KStandardAction>
 40 #include <KConfigDialog>
 41 
 42 MainWindow::MainWindow(QWidget *parent) : KXmlGuiWindow(parent)
 43 {
 44 
 45 	textArea = new KTextEdit;
 46 	setCentralWidget(textArea);
 47 	setupConfig();
 48 }
 49 
 50 void MainWindow::setupActions()
 51 {
 52 
 53 	QAction* clearAction = actionCollection()->addAction( "clear" );
 54 	
 55 	clearAction->setText(i18n("Clear"));
 56 	clearAction->setIcon(KIcon("filenew"));
 57 	clearAction->setShortcut(Qt::CTRL+ Qt::Key_W);
 58 			
 59 	connect(clearAction, SIGNAL(triggered(bool)), textArea, SLOT(clear()));
 60 
 61 	KStandardAction::quit(kapp, SLOT(quit()), actionCollection());
 62 	
 63 	setupGUI();
 64 
 65 }
 66 
 67 void MainWindow::setupConfig()
 68 {
 69 	readConfig();	
 70 	
 71 	
 72 }
 73 
 74 void MainWindow::readConfig() {
 75 	m_showAction    = tutorial4::showAction();
 76 	if(m_showAction = (true)) 
 77 		setupActions();
 78 	
 79 }
 80 
 81 void MainWindow::saveSettings() { 
 82 	tutorial4::setShowAction(m_showAction);
 83 	tutorial4::self()->writeConfig();
 84 }
 85 
 86 
 87 
 88 </code>
 89 
 90 At first we import "tutorial4.h", which should be auto-generated at runtime by KConfig XT every time you compile this. MainWindow and SetupActions are copied from the tutorials before. The only thing new is that we now call a new void called setupConfig. This then calls readConfig. We '''could''' also call saveSettings(but only after we called readConfig), but since we don't change any values there this is more of a "proof of concept".
 91 
 92 Within readConfig we assign m_showAction the value of the setting showAction. If this returns true setupsActions makes buttons and all that. When false it doesn't.
 93 
 94 The setShowActions() in saveSettings() overwrites showAction with a value of your choice, in this case with itself. Then you need to call writeConfig() to apply the changes you made.
 95 
 96 ===main.cpp===
 97 <syntaxhighlight lang="cpp-qt" line>
 98 
 99 #include "mainwindow.h"
100 
101 
102 #include <KApplication>
103 #include <KAboutData>
104 #include <KCmdLineArgs>
105  
106 int main (int argc, char *argv[])
107 {
108 	KAboutData aboutData( "tutorial4", "tutorial4",
109 			      "0.4", "KMessageBox popup",
110 	 KAboutData::License_GPL, "(c) 2007" );
111 	KCmdLineArgs::init( argc, argv, &aboutData );
112 	
113 	KApplication app;
114 	
115 	MainWindow* window = new MainWindow();
116 	window->show();
117 	
118 	return app.exec();
119 
120 }
121 </code>
122 
123 No news in here
124 
125 ===tutorial4ui.rc===
126 <code xml n>
127 <?xml version="1.0" encoding="UTF-8"?>
128 <gui name="tutorial4"
129      version="1"
130      xmlns="http://www.kde.org/standards/kxmlgui/1.0"
131      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
132      xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
133                          http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd" >
134 
135   <MenuBar>
136     <Menu name="file" >
137       <text>&amp;File</text>
138       <Action name="clear" />
139     </Menu>
140   </MenuBar>
141 
142   <ToolBar name="mainToolBar" >
143     <text>Main Toolbar</text>
144     <Action name="clear" />
145   </ToolBar>
146 
147 </gui>
148 
149 </code>
150 
151 ==CMake==
152 ===CMakeLists.txt===
153 <code ini n>
154 PROJECT(tutorial4)
155 
156 FIND_PACKAGE(KDE4 REQUIRED)
157 INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
158 
159 SET(tutorial4_SRCS 
160   main.cpp
161   mainwindow.cpp
162 )
163 
164 KDE4_ADD_KCFG_FILES(tutorial4_SRCS settings.kcfgc)
165 
166 KDE4_ADD_EXECUTABLE(tutorial4 ${tutorial4_SRCS})
167 
168 TARGET_LINK_LIBRARIES( tutorial4 ${KDE4_KDEUI_LIBS})
169 
170 install( TARGETS tutorial4 DESTINATION ${BIN_INSTALL_DIR})
171 install( FILES tutorial4ui.rc DESTINATION  ${DATA_INSTALL_DIR}/tutorial4)
172 install( FILES tutorial4.kcfg  DESTINATION  ${KCFG_INSTALL_DIR} )
173 </code>
174 
175 ==Moving On==
176 
177 For a better understanding of the XML used in KConfig XT you could try 
178 [[Development/Tutorials/Programming_Tutorial_KDE_4/How_to_write_an_XML_parser|to write an Xml Parser]]