My First Qt App

From qtnode

Jump to: navigation, search

This article describes the steps to creating your first Qt application. When complete, you should have a running application in your target environment ready for the grunt work.

This instruction set was written for the Qt-4.x platform.


Contents

Creating Project Directory

The first step is to create a project directory tree. On this topic opinions vary. Here is the typical project tree I use:

I usually create a project directory to contain all the various project files unrelated to the application, and then place a src directory under that where all the source files are maintained. Under the src directory I place a ui directory for the user interface files.


Design a UI file

Open designer. Create a 'New Form' and make it a 'Main Window' type of window. You can place whatever widgets in it you like and you can do that now or later. Save the form into your ./projects/qt/first/src/ui directory - call it "MainWindow.ui"


Create a .pro file

Create a project file called first/src/first.pro containing the following:

TEMPLATE       = app
TARGET         = first
SOURCES        = first.cpp
HEADERS        = first.h
FORMS          = ui/MainWindow.ui
MOC_DIR        = .moc/release-shared
OBJECTS_DIR    = .obj/release-shared
UI_DIR         = ui

For a more detailed explanation on the .pro file refer to the Trolltech qmake manual.


Create a first.h file

Create a file called first/src/first.h containing the following:

/**
 ** @mainpage first - This is my first Qt application
 **
 ** This application is my first skeleton.  It is enough to get a running executable
 **  to build my app around.  The comments I'm typing now are compatible with the
 **  doxygen documentation system, and will appear in my apps documentation set.
 **
 ** @author My Name Here
 **
 */
 
#ifndef FIRST_H
#define FIRST_H
 
/**
 ** Define application globals and constants here...
 **
 */
#define ORG_DOMAIN "mydomain.com"
#define APP_NAME   "first"
 
/*
** Include any stock Qt files here...
**
*/
#include <QApplication>
#include <QtGui>
#include <QMainWindow>
#include <QDebug>
 
/*
** Include any application specific headers here.
**
*/
#include "ui/ui_MainWindow.h"
 
/*
** Forward-declare any required classes here.  We do not include the
**  header files for these classes here because it just increases the
**  compile times unnecessarily.  This method is considered more
**  practical - these are typical.
**
*/
class QAction;
class QAction;
class QListWidget;
class QMenu;
class QTextEdit;
 
class QSqlDatabase;
class QSqlRelationalTableModel;
class QSqlRelationalDelegate;
 
/**
 ** The Main Window is the main user interface screen for the
 **  application.  It is implemented as a MIDI type window.
 **
 */
class firstMainWindow : public QMainWindow, private Ui::MainWindow
{
  Q_OBJECT
 
public:
  firstMainWindow();
 
};
 
#endif // #ifndef FIRST_H


Create a first.cpp file

Create a file called first/src/first.cpp containing the following:

/// first.cpp
 
#include "first.h"
 
/**
 ** The constructor initializes the environment for the first application.
 **
 */
firstMainWindow::firstMainWindow()
{
  /*
  ** Build the ui component
  **
  */
  setupUi(this);
 
} // endfirstMainWindow::firstMainWindow()
 
 
int main(int argc, char **argv)
{
  /*
  ** Create the Qt application object.
  **
  */
  QApplication app(argc,argv);
 
  /*
  ** Set the organization and application names for QSettings
  **  savage and restoration.
  **
  */
  app.setOrganizationDomain( ORG_DOMAIN );
  app.setApplicationName(    APP_NAME   );
 
  firstMainWindow win;
  win.show();
 
  return app.exec();
 
} // endint main(int argc, char **argv)


Generate the Makefile

The Makefile is automatically generated from the "first.pro" file that you created earlier. Any time you change the contents of this file you need to re-run the following command:

qmake first.pro

Simple, eh? Now you have a "Makefile" with all the rules and include paths and whatnot in your /first/src directory.


Compile the application

Once the Makefile is built, building the application is simple:

make .or. gmake .or. mingw32-make .or. whatever-make

Can't get much easier than that.


Working on the application

Periodically, depending on what you're editing, you may need to clean up your environment and regenerate like from scratch. Here are a couple of commands to keep in your pocket in the event you are editing something and the changes are just not showing up in your running executable:

qmake first.pro && make clean && make (substitute 'make' for the make you're using)

This has the effect of rebuilding the Make file (qmake first.pro), cleaning the environment (make clean) and finally recompiling everything (make). Give this a try if things are just not behaving correctly.


What's Next?

What's next is to add some widgets to your main form and get to work! I have some real-simple code for implementing a delayed-initialization thing whereby the main window is allowed to be fully constructed and painted to the screen, and then a one-shot timer is fired to execute some code that executes only after the main window is on the screen... thus allowing that code to interact more intelligently with the user. That'll be in our next tutorial...

What about Documentation?

Yes, I did mention the doxygen documentation system. This is a something I came across in my travels through linux and kdevelop. Doxygen is an application that scans all your source code in your project and pulls documentation out of it and dumps it into a series of html files for easy viewing and caroozing. This link [doxygen-howto] might be a place to begin reading.

Anyhow, I began using some of the doxygen tokens in the source files to quickly capture my notes into the doxygen files, and I'll discuss that process in another tutorial as well.

Personal tools