Part 2: First changes

Part 2: First changes


[<< Contents] [<< Prev] [Next >>]

Replacing the template code

We can now start to replace the template code provided by KDevelop to start constructing our QSimulate application. Replace the code in main.cpp with our new code below.

main.cpp

#include <QApplication>
#include <QMainWindow>

int main(int argc, char *argv[])
{
  // create main event loop handler and parse command line arguments
  QApplication app(argc, argv);

  // create application main window & enter main event loop
  QMainWindow window;
  window.show();
  return app.exec();
}

If you now try to compile or run this new code, you will notice that the compilation fails. This is because by default KDevelop has setup QMake to only use the Qt4 Core libraries. We need to tell QMake that we want to develop a GUI application.

This is done by opening the QMake Manager that can be found on the righthand side of KDevelop, ensuring the "src" subproject is selected, and opening the QMake Configuration dialog box by clicking on the small button that looks like a spanner. In this dialog box select the "Configuration" tab, select the Qt4 Libraries "Gui" before finally pressing "OK".

The new code should now successfully compile and run. As before, if KDevelop pops up any dialog boxes saying a file has changed, simply press "Reload".

Explaining the code

The application consists of a "main" routine in main.cpp as the entry point when the application is started, and uses the Qt classes QApplication and QMainWindow to create a very basic GUI window that can be resized, moved, and closed. When the window is closed, the application ends.

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.

main.cpp

#include <QApplication>
#include <QMainWindow>

Including QApplication and QMainWindow provides access to the Qt4 classes.

  // create main event loop handler and parse command line arguments
  QApplication app(argc, argv);

Here we create the QApplication object that contains basic functionality needed by all Qt GUI applications and parses common command line arguments and sets its internal state accordingly. See the Qt documentation for more details about this.

  // create application main window & enter main event loop
  QMainWindow window;

Now we can create the main window for our application.

  window.show();

Make the main window visible.

  return app.exec();

And finally pass control to the QApplication object until our application is closed.

Compile and run

The new code will be compiled automatically when necessary when you try to re-run the application, either from the Build menu, the KDevelop toolbar or keyboard shortcut.


[<< Contents] [<< Prev] [Next >>]


Last updated 14-Apr-2008