[<< Contents] [<< Prev] [Next >>]
To make the code rather easier to follow and for me to explain, we will replace the template code provided by KDevelop with our own new code shown below. Open each file by using the File menu "Quick Open..." and replace the existing code with the new code shown below.
#include "ksimulate.h"
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kaboutdata.h>
// structure that holds command line options
static KCmdLineOptions options[] = { KCmdLineLastOption };
int main(int argc, char **argv)
{
KAboutData about("ksimulate", // appName - program name used internally
"KSimulate", // programName - displayable progame name
"0.1", // version - program version string
"Simulates radio networks", // shortDescription - what program does
KAboutData::License_GPL, // licenceType
"(C) 2004 Richard Crook", // copyrightStatement
"Example KDE application!", // text - any information
"http://www.dazzle.plus.com", // homePageAddress
"richard@dazzle.plus.com"); // bugsEmailAddress
about.addAuthor( "Richard Crook", "Author", "richard@dazzle.plus.com" );
// uses KAboutData to replace some of the arguments that would otherwise be required
KCmdLineArgs::init(argc, argv, &about);
KCmdLineArgs::addCmdLineOptions( options );
// controls and provides information to all KDE applications
KApplication app;
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
// create main window, enter main event loop, and wait until exit() is called
KSimulate *mainWin = new KSimulate();
app.setMainWidget( mainWin );
mainWin->show();
args->clear();
return app.exec();
}
|
#ifndef KSIMULATE_H
#define KSIMULATE_H
#include <kmainwindow.h>
/***********************************************************************************/
/******************* KSimulate is the main application window **********************/
/***********************************************************************************/
class KSimulate : public KMainWindow
{
Q_OBJECT
public:
KSimulate(); // constructor
};
#endif // KSIMULATE_H
|
#include "ksimulate.h"
/***********************************************************************************/
/******************* KSimulate is the main application window **********************/
/***********************************************************************************/
/******************************* constuctor ********************************/
KSimulate::KSimulate() : KMainWindow()
{
}
#include "ksimulate.moc"
|
Finally using the Automake Manager remove the "ksimulateui.rc" file as it is no longer needed. Automake Manager is opened using an icon on the far-right of KDevelop. I would recommend also removing it from disk as the file will not be used again.
The application consists of a "main" routine in main.cpp as the entry point when the application is started and a class "KSimulate" in ksimulate.h and ksimulate.cpp to be the main application window.
Hopefully the comments in the code have given you good hints to what the different bits of code do, but lets go through the code in a bit more detail.
#include "ksimulate.h" #include <kapplication.h> #include <kcmdlineargs.h> #include <kaboutdata.h> |
Including ksimulate.h provides access to our main window class, kapplication.h file provides some basic code and functionality needed by all KDE applications, kcmdlineargs.h provides a class for controlling the command-line arguments, and kaboutdata.h provides a class to store information about the application.
// structure that holds command line options
static KCmdLineOptions options[] = { KCmdLineLastOption };
|
Here we could add some application specific command-line options, but in this case we just leave the standard set.
KAboutData about("ksimulate", // appName - program name used internally
"KSimulate", // programName - displayable progame name
"0.1", // version - program version string
"Simulates radio networks", // shortDescription - what program does
KAboutData::License_GPL, // licenceType
"(C) 2004 Richard Crook", // copyrightStatement
"Example KDE application!", // text - any information
"http://www.dazzle.plus.com", // homePageAddress
"richard@dazzle.plus.com"); // bugsEmailAddress
about.addAuthor( "Richard Crook", "Author", "richard@dazzle.plus.com" );
|
Here we record some information about the application.
// uses KAboutData to replace some of the arguments that would otherwise be required KCmdLineArgs::init(argc, argv, &about); KCmdLineArgs::addCmdLineOptions( options ); |
Here we initialise the command-line arguments class and add the default set of options.
// controls and provides information to all KDE applications KApplication app; KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); |
Here we create the KApplication object that contains basic functionality needed by all KDE application and parse the command-line arguments.
KSimulate *mainWin = new KSimulate(); app.setMainWidget( mainWin ); |
Now we can create the main window for our application and attach it to the KApplication object.
mainWin->show(); |
Make the main window visible.
args->clear(); return app.exec(); |
Clear the arguments to free some memory and pass control to the KApplication object until our application is closed.
#ifndef KSIMULATE_H #define KSIMULATE_H |
Only process the contents of this header file if not already included.
#include <kmainwindow.h> |
The kmainwindow.h file provides the class KMainWindow that will be the basis of our application main window class.
class KSimulate : public KMainWindow
{
Q_OBJECT
public:
KSimulate(); // constructor
};
|
Our class is derived from KMainWindow to inherit a standard set of KDE main window functionality. Q_OBJECT is a special macro that must be included in all KDE classes to invoke the meta object compiler that is part of the Qt development library. For the moment we only have a constructor method.
#endif // KSIMULATE_H |
End part of only processing this header if not already included.
#include "ksimulate.h" |
Include the class header file.
KSimulate::KSimulate() : KMainWindow()
{
}
|
Implement a (currently empty) class constructor inheriting from KMainWindow.
#include "ksimulate.moc" |
The meta object compiler produces a moc file that we need to include. The moc file contains meta object code for the classes that use the Q_OBJECT macro. Among other things, meta object code is required for the signal/slot mechanism which we will use later.
The new code will be compiled automatically when you re-run the application "Execute Program", either from the Build menu, the KDevelop toolbar or keyboard shortcuts.
Also try running the application by typing "./ksimulate --help" in a terminal session and see what happens. You will find the application binary in your "ksimulate/debug/src" directory.
[<< Contents] [<< Prev] [Next >>]