Next Previous Table of Contents

### 4. Development with KDevelop

In this chapter you will get a general overview how you can use KDevelop and the internal dialog editor to create own applications. Thereby, all tools are described by their use during the development process. If you are a beginner in C++/GUI design and programming, you should read The KDevelop Programming Handbook, which describes some basics for program design and shows a more detailed use of KDevelop by sample projects which you can follow step-by-step to get used with the way applications are created using KDevelop. Also you should have read chapter Programs to gain a general understanding why Unix applications should make use of provided development tools; this will make things easier when you hit references to what Makefiles are etc.

### 4.1 What are KDevelop Project Applications ?

Projects created with KDevelop make it very easy for developers to use the GNU standard development tools. In opposition to the self-creation of Makefiles, which have to be written anyway because of the project's complexity, those provide not only a better way of Makefile generation but also a good and safe way to provide a fast adaption towards different systems by autoconf-generated `configure` scripts.

The distribution of your applications does not require the end-user to have anything different installed than a C++ Compiler and the development libraries, which is most often the case; but you can as well distribute binary packages of your application. In either way, the end-user of your product does not have to have KDevelop installed. For giving away your sources, we advise to include the project file of KDevelop as well, as this makes it very easy for other developers to work with your sources if they use KDevelop as well. For projects where several developers, maybe working on different places, are involved, this should the case anyway to ensure consistency of the Makefiles so you don't run into trouble. Especially on multi language applications, translators won't actually work with the source code, except in cases that require corrections for enabling translation support. Anyway, those will be thankful as KDevelop simplifies their work to a minimum by adding their language and reducing the work to concentrate on the translation.

With using KDevelop for your own projects, you would start creating a new application with the KAppWizard. There, you can also set the project type according the goals of your program. Then, you would start developing the user interface by adding widgets that are already constructed in the libraries and only have to be called by your application, or by self-constructions that can be made with the dialog editor. Next comes the extension of the user interface by changing and extending those parts that are already made by KDevelop like the statusbar, menubar and toolbars. As resources are collected in a resource file, this can be extended simply by adding new values to the already existing one; the given resources can also be used as a guideline for adding your specific ones. After implementing the functionality to methods that are either generated empty by the dialog editor or already containing code for standard user actions, you should extend the User Manual by simply editing the provided SGML index file towards describing your applications capabilities. The last step would be to generate translation files and to distribute your project to translators doing the rest to enhance internationalization.

But even for non-GUI application designers the IDE offers an easy way to create new projects; the editor and Class Browser in conjunction with KDevelop's powerful project management will reduce the time for development to the minimum that is required for implementation of functionality.

### 4.2 Short Description of KDevelop's Tools

With KDevelop you have chosen a package, that, in conjuction with the use of other tools, will provide you a prefect and flexible environment for object-orientated application development under Unix-Systems. Generally, KDevelop consists of the programming environment and the dialog editor; besides that all needed programs that can be helpful for designing applications are embedded in one or the other way.

#### Programming Environment and Dialog Editor

#### The Programming Environment

The Programming Environment is the user interface that KDevelop provides for creating and maintaining projects, implicitly using many other tools by simplifying their use. It's built-in Editor and Helpbrowser as well as the Class Browser reduce the amount of work that development processes usually burden on the programmer. The ease of use make writing applications for Unix an enjoyment on it's own and will probably result in shorter release cycles and better development improvements. From here, you're managing your project throughout all its details, keep the overview over your classes and don't have to use additional consoles for your work, as well as the integration of all user interfaces results in the fact that you only have to work with one window under X and don't have to watch for several ones spread all over your desktop.

#### The Dialog Editor

KDevelop's built-in dialog editor offers a simple way of designing user interfaces with the Qt and KDE libraries. Its simple way to do almost everything with the mouse allows a rapid construction of dialogs and main views for applications and their direct transformation into C++ code, which is directly added to the project. As the dialog editor is embedded into the rest of KDevelop's user interface, you have the same commands available than working in Programming mode. This allows e.g. construction of a dialog, generation of the code output and it's direct testing within the program by the availability of the make-commands and you still can control the Compiler output without having to switch back to the programming view. For a detailed description, see The Dialog Editor .

#### KAppWizard and the Classtools

The KAppWizard and the Classtools provided for project generation and automatic code extension are intended to make the creation of applications as easy and safe as possible and offers a good way for beginners in Qt/KDE programming to start actually working themselves into the details of GUI application design, as results can be achieved very quickly.

#### KAppWizard

KAppWizard is intended to create full-functional application projects that are ready-to-run by using GNU-standard tools and, for GUI-programs, taking advantage of the KDE and Qt libraries. By specifying only the information that is required as a minimum, users are enabled to start working at a new project within seconds. Calling KAppWizard should be the first step to be done for creating a new application with KDevelop.

KAppWizard provides you a so-called application skeleton with all needed C++ source code files for a running application. After you specified a projectname and set the needed preferences, your new project will be generated and you have a complete application that already contains a lot of functionality (for KDE and Qt projects), without even having to type one line of C++ code yourself. When specifying the complete set of options the program generator offers, your application already contains:

- an SDI-Interface (for working with one document per application window), based on the document-view-controller model
- Menus and dialogs for opening and saving files as well as printing dialogs
- a toolbar and statusbar already providing complete functionality
- a helpsystem, containing an SGML-based user manual and statusbar help
- a complete HTML-based API (application programming interface) documentation for the current state of the project
- an installation routine though make

The KAppWizard is available by the Project-menu, entry "New...".

#### Classtools

The term "Classtools" describes a variety of functions that are available for the developer to work on a project not only by an object-orientated language but using the sources in an object-orientated way. Working with classes instead of having to watch for files offers a very flexible way to extend a project and lets the user concentrate on the objects the project contains. The tools that KDevelop offers are a Classgenerator and a Class Browser, where each one provides a lot of automated functionality.

#### Creating a new class with the Classgenerator

After generating a project with the KAppWizard your work as a developer would be to add a set of classes that match the design of the application. The typical process without using KDevelop would be to create a new header and sourcefile, add them manually to the according Makefile.am and to start writing the classes declaration and implementation. To simplify the whole process, the Classgenerator lets you do this by only specifying the new class by its name, inherited class, inheritance attribute and further information such as class documentation by it's purpose for the project. Additionally, you can also change the filenames that are preset by the generator and if the class would be Qt-signal/slot enabled by default.

The rest of the work is done automatically- you don't have to take care for Makefile.am, files and the class itself. A new "make" invocation is enough to include the new class in the compilation process. As the Class Browser updates itself after the addition of a class, the declaration and implementation is available at once and you can start working on the extension of the class. The Classgenerator is invoked by the Project-menu, entry "New Class".

#### The Class Browser

The Class Browser displays all types of objects and functions of your project in a tree left of the editing windows. A class parser scans all files for methods and classes and makes them available by graphical symbols. Over those, context-menus offer a specialized functionality to work with the sources by focusing on the classes and functions.

Selecting a class will result in opening the file that contains the declaration; on methods and functions this will show you the implementation. For a full description, see The Class Browser

### 4.3 The Development Process

The development of a new application with KDevelop can generally be divided into two major steps: first, you have to generate a program skeleton with KAppWizard, then the "rest" of the development has to be done using KDevelop's features, which only requires your abilities as a programmer. To describe the development process, we assume you want to create a Qt/KDE application, which implies using most of KDevelop's features for working with these types of project, so you have a general "HOW-TO".

#### Creating a Program Skeleton

To start creating your application, you normally would call the KAppWizard, enter the project name and generate the program. By this, the wizard automatically creates a project file for the application and inserts the KDevelop areas into the Makefile.am's which will be used later when extending the application. The project file is the file you have to load for opening the project in later sessions.

You call KAppWizard by the "Project"-menu, entry "New...". When the wizard appears, you have to specify the project type on the first page. Selecting "Next >>" brings you to the next page where you have to insert the project name, version, directory and your personal information. Page 3 offers more detailed options, page 4 and 5 allow editing file headers for header and implementation files that are inserted automatically according to your selection. If you already have file headers you want to use, you can choose those files as well. The last page contains an output window and an error message window. When selecting "Create", your application will be generated and the actions that are executed can be seen in the output window. If READY appears in the output window, the KAppWizard is finished and you can select "Exit" to return to the Programming Environment. To build the binary, choose "Make" from the Build-menu or choose the according button in the toolbar. You can as well test the functions already present by choosing "Execute" from the Build-menu.

#### Developing an Application

This section describes the development phase of applications with KDevelop and the dialog editor- and all work can be done within the IDE.

The development steps are generally to edit the source files, the compilation of the project sources followed by the Linker process binding all object files to the final binary. Then errors have to be located which either prevented a compilation or linking or are semantical bugs that can be found by a debugging session. Finally, the documentation has to be extended and translations have to be added. But as all those steps can mix which each other, it is not that easy to describe a general way how to develop your own application. Typically, the usual procedure would be to first create all visible parts of the project like the main view and the dialogs for configuring any options, then generate the source code and implement all needed connections that still have to be done like changing the menubar and toolbars, adding statusbar help for new menu entries and fill the new member functions with objects that are displaying your dialogs. Then you would compile your application and test it's capabilities, fix errors and test it again. The following sections describe how this would be done; for your particular application you may choose another way- KDevelop leaves you enough flexibility to decide what to do at what time.

#### User Interfaces

The User Interface of an application is actually what the user sees and by which he interacts with the program. Therefore a program generally has a menubar, toolbars and a statusbar as well as a main view which would be e.g. a text input window for an editor. KAppWizard generated applications already contain a complex functionality on graphical interfaces- the frame structure takes care of standard elements such as bars and buttons. When developing your application, you have to extend the given structure to give the program the user interaction capability you want to provide. One part of the work is the design of dialogs e.g. for changing values like the brush width in a paint application. This can be done easily with the dialog editor inside KDevelop. Now, how do you create those good-looking dialogs ? As the Qt-library is a GUI toolkit, it provides a base set of so-called "widgets" like labels displaying plain text, lineedits for text input and buttons for navigation and selection commands. Besides these "low-level" parts of user interfaces, standard dialogs are provided and are ready to use such as printing dialogs. The KDE-libraries then are based on the Qt-library and contain replacements for certain dialogs as well as additional widgets that can be used in your application. So, when you start designing your applications behavior towards the user, you should make yourself comfortable with the widgets provided with Qt and KDE. The online-documentation of Qt offers screenshots for most of them, so you should have a look there first. Then we advise to have a closer look at the KDE-UI library, offering other widgets. Libraries like the KHTML library contains very specialized widgets that a lot of programs make use of. It just simplifies application design like the predefined dialogs provided with Qt. For your own dialogs, those low-level widgets are the one you need. A dialog merely consists by a collection of different widgets combined together building the interface and is technically programmed by C++ code in a class that is derived from `QWidget` or a more specialized dialog class that inherits `QWidget`. The program using the widget therefore needs an object of the widget's class- this is almost all you have to understand how dialogs or views are used. The dialog editor of KDevelop now offers a nice way to simply construct dialogs and user interfaces visually instead of you having to combine dialogs with pure C++ code together- this is the hard way to create interfaces. Switch to the dialog editor by choosing the according entry in the "View"-menu (or by the toolbar button). You will see that KDevelop's face has changed but for e.g. the menubar and the toolbar are very similar. This makes it very easy to switch back and forth between the two working modes and you will feel comfortable in both after some time. There you can construct your views and dialogs as you like and set all available preferences for the items the view will contain. When you're finished, select "Generate Sources" from the Build-menu; the dialog editor and the project management will take care of the rest. You can test if everything went OK by selecting Make or Execute, this will build your application including your new sources. But don't expect that dialogs are already usable- this is a matter of implementation that is the usual way a developer works. Don't worry- this isn't too difficult, too. For more information about creating dialogs, see The Dialog Editor, examples and guidelines can also be found in The KDevelop Programming Handbook.

#### Binding New Elements

After you have created your user interfaces and generated the sources, you are ready to make your application use them by objects. As described above, a usual GUI-based program contains bars and the main view; additionally the main view operates with the bars, which are part of the main application class, and the document object which it is connected to. In terms of object-oriented design, you would describe the given architecture as the "Document-View-Controller"-model. This model describes the basic functions for objects in an application towards their role within the program. The Controller represents the one coordinating the main application and provides user interaction through a menubar and additionally toolbars and a statusbar. The Document class takes the task of representing a document the user works with. Therefore, a document class should do all actions like loading files and saving them again. The view now is in the center of the application window, showing the user a part of the document visually and provides all functions to manipulate the data by the user. As the bars and toolbars are already present, your work would be to create the main view and additional dialogs by which the user can change any settings or has access to additional functions.

To build your main-view, the generated source code for your application already contains a class in the form of <YourApplication>View which inherits the `QWidget` class (this is the minimal class that handles visual interfaces in Qt and therefore in KDE as well). In general, there are three different ways how to extend the given class:

- remove the document-view structure and use one of the predefined "big" widgets already containing a lot of functionality- just remove the View-class and replace the view-object by another.
- change the inheritance of the View-class. Change the inheritance to e.g. QMultiLineEdit and your application would be an editor.
- if your main view will consist of several separated parts, create the widgets and their classes you need with the dialog editor and create objects of these classes in the view-class constructors that in combination will build the main view.

For dialogs, things are a bit different. The usual way would be to call a dialog through a menubar entry and additionally by a toolbar icon. After you constructed your dialog and generated the sources, the class is available for creating an object that will be the dialog. So you first have to look for a suitable place in the menubar to add an entry which will open the dialog when selected by the user. If the already given menus do not match your needs, create a new popup menu just like the others and insert your entry and the slot you want to call when the menuitem gets selected. Then implement the slot by creating an instance of the dialog class and call the dialog with the member functions given by the base class. Also you have to specify an ID for the menuentry. The frame applications have all IDs already given collected in a file resource.h, so you only have to add the ID and give it a new number. Then you're done- additionally you could construct another toolbar icon and add the statushelp message. Your application now offers a new functionality which is visible to the user. Now you have to add the implementation of methods that your dialog will be operating with to manipulate any values. Finally, call your make-tool or "Execute" and the changed files will be recompiled; the result of your changes can then be tested immediately.

#### The Complete Development Process

Above, we discussed the usual way how to start working on a new application using KDevelop and how to extend the user interface. Now, these are the standard steps where the IDE helps you, but KDevelop does more than providing tools for creating applications and their visual parts. The following gives a short description of the main functionality that is offered for enhancing application development.

#### Sourcecode Management

The KDevelop IDE provides a lot of methods to the programmer to achieve his goals within the shortest time. As described above, the KAppWizard and the dialog editor shorten the time you would usually need to get to the same result manually. But this didn't cover the work a programmer has to do usually: working on the implementation of his application to provide a proper execution by the end-user. Now, why is KDevelop the IDE you would want to use as a coding environment, including it's use to create even non-GUI applications ?

Implicitly, the IDE keeps track of your project in general; that means, you don't have to take care for saving changes, the Makefile generation and so on- this is providing full project-management, and KDevelop plays out all it's power here in any case of C++ application development. It is easy to understand that, after taking away the management from the programmer, he is more concentrated on working out the sourcecode. On the other hand, the code is usually spread over many different files across the project, so you can separate certain parts. But this means still working the hard way- being the developer, you still would have to struggle with creating these files and writing standard contents like file headers containing the date and the author's name as well as e.g. license terms for the code. Additionally, this requires that you have to remember where your functions, class declarations and implementations are in the project. Therefore, KDevelop contains the Classtools- a variety of actions, that allow fast work and moving the developer's focus from files to objects- classes, structures and methods. The classgenerator lets you create a new class including inheritance, attributes and documentation easily . For object-oriented work, the Class Browser brings you to the location of your objects; it doesn't matter any more where the code is actually. The Browser scans all sources automatically and rebuilds itself after additions to keep up with your work and enables you to access the new code directly. By context-menus, the browser offers even more functionality, like bringing you to the implementation or to the declaration of member functions. Then, the addition of members are done graphically by dialogs- no search for the file and the place you would have to add your entries. Finally, you can get an even more specialized view of your project's classes by the Classtool dialog, providing you trees that show the usage, contents and inheritance of classes and their objects. For more detailed information, see The Class Browser.

#### Building and Executing your Application

The KDevelop IDE is specially designed to take away all those steps that you have to do periodically like building and executing your program as well as locating errors in the sourcecode.

You're starting the build-process by:

- clicking on the symbols "Build" or "Rebuild All" in the toolbar
- or selecting "Build"/"Rebuild All" from the Build-menu.

To execute your application, choose

- the symbol "Execute" or "Debug" (starting KDbg with your program) from the toolbar
- by the according menu entries in the Build-menu.
- or by "Execute with Arguments" to start your application with additional arguments

For more information about the build-process, see Projects.

#### Searching Program Errors

As naturally errors occur either during the build-process (which are detected by the Compiler and are called syntactical errors because they result from a wrong syntax of the sources) or during the execution of the application, those have to be found and removed by the programmer. For locating errors, the developer needs the information what exactly caused its occurance. As mentioned, the Compiler is able to detect syntax errors himself, resulting in that the executable can't be build (this can also happen by the Linker when he detects "unresolved symbols"-see Linker Options). As he gives out a description of the error as detailed as possible, the error can be found and removed. During the build-process, you will see the output window pop up showing you what your make-tool and the Compiler has to say. In case of errors or warnings, just press the mouse button over the error line and the editor will open the affected file and sets the cursor to the errorline. This can also be done by the entries in the "View"-menu, "Next Error" and "Previous Error" or by the according keyboard shortcuts. You will find that this is very functional and saves a lot of time to get to the error, thus for removing the cause your knowledge as a programmer is asked.

Runtime Errors, which occur during execution and will mostly result in a segmentation fault, are sometimes hard to find. Therefore, you can enable the Compiler to add information within the binary to watch the execution by the sourcecode. A debugger then is another program that lets you do this by starting the application and letting you set breakpoints in the code where the execution stops, so you can control critical implementations and by this detect at least the line that caused the error. Finding the real cause is another task; it depends on the programmer to locate this. The gdb is a debugger that is provided with the GNU Compiler and programs like ddd or KDbg are frontends that allow an easier use. KDevelop therefore uses KDbg and lets you invoke the debugging process by the commands "Debug" in the Build-menu or by the "Debug" symbol in the toolbar. For more information about debugging applications, see Projects and The KDevelop Programming Handbook.

### 4.4 Additional Information

The topics addressed within this chapter are described im more detail within the documentation provided with the KDevelop IDE and in other documentations:

- The KDevelop Programming Handbook, covering a complete introduction into GUI application design and programming with the Qt and KDE libraries,
- this handbook, sections Overview to Projects, describing all avaliable functions within KDevelop,
- this handbook, section The Dialog Editor, addressing the use of the dialog editor to create your own widgets,
- The Online-Reference Documentation to the Qt-library, which covers examples of using the Qt GUI-toolkit as well as a class reference and screenshots for the most important ready-to-use widgets included,
- The KDE-Library Class-Reference, generated automatically by KDevelop from the KDE-Library sources, containing descriptions for all classes and widgets including example code for their use,
- on the Internet, see:
  - http://www.troll.no for information about Qt and additional third-party widgets,
  - http://www.kde.org for information about the KDE project and developer guidelines,
  - http://developer.kde.org for additional references to KDE application development.
  - on the KDevelop homepage http://www.kdevelop.org

Next Previous Table of Contents