Hello World with Qtopia (Part 4 – Application Code)

Posted by – June 3, 2008

In my previous articles about Qtopia application development I showed the directory structure for a Qtopia project, the contents of basic Qtopia project file (helloworld.pro) and how to create the application interface with user interface files (helloworld.ui), in this article I’ll talk about the classes to be used in our helloworld application and how we can integrate the user interface files in our applications.

Every Qtopia application requires a initialization code like we have in the code block below:

main.cpp

1
2
3
4
5
6
7
8
9
10
11
/*
We must include the header file below since it contains
the HelloWorld class definition.
*/

#include "helloworld.h"
#include <qtopia/qtopiaapplication.h>

//Set the HelloWorld class as the main class of helloworld application.
QTOPIA_ADD_APPLICATION("helloworld", HelloWorld)
//Add initialization code to this application
QTOPIA_MAIN

In the example above we are using to macros QTOPIA_ADD_APPLICATION and QTOPIA_MAIN, both are only available if qtopia_main is set in CONFIG variable of application project file (helloworld.pro) as described in this article.


Now we must create the header file containg the definition of HelloWorld class, take a look in the code below:

helloworld.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef HELLOWORLD_H
#define HELLOWORLD_H
/*
The header file below is generated during build process,
it contains the definition of Ui_HelloWorldBase class.
*/

#include "ui_helloworld.h"

/*
HelloWorldBase inherits from QWidget and Ui_HelloWorldBase class,
Ui_HelloWorldBase class is generated from helloworld.ui file during
build process.
*/

class HelloWorldBase : public QWidget, public Ui_HelloWorldBase
{
public:
    HelloWorldBase( QWidget *parent = 0, Qt::WFlags f = 0 );
    virtual ~HelloWorldBase();
};

class HelloWorld : public HelloWorldBase
{
    Q_OBJECT
public:
    HelloWorld( QWidget *parent = 0, Qt::WFlags f = 0 );
    virtual ~HelloWorld();

private slots:
    void sayHello();
};

#endif // HELLOWORLD_H

Once the HelloWorld class is defined, we must code the main class of our application, we will setup the main window by loading the user interface definition from helloworld.ui file and then set some event handlers as we can see in the code below:

helloworld.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//The main header file of this application.
#include "helloworld.h"
/*
We must include the header file below since we will
use a messagebox in this application.
*/

#include <qmessagebox.h>

/*
This is the constructor of our widget created using QT Designer
user interfaces files.

HelloWorldBase is a custom QWidget, it contains the same child
widgets declared in helloworld.ui, we will bind all child widgets
declared in user interface file to the child widgets declared in
this class.
*/

HelloWorldBase::HelloWorldBase( QWidget *parent, Qt::WFlags f )
    : QWidget( parent, f )
{
    /*
    The method below must be called to bind the child widgets defined
    in helloworld.ui to child widget instances in one of base classes
    (Ui_HelloWorldBase) of our main class.
    */

    setupUi( this );
}

//The class destructor
HelloWorldBase::~HelloWorldBase()
{
}

/*
 *This is the main class of our application, it inherits from
 *HelloWorldBase, the main widget of this application.
 */

HelloWorld::HelloWorld( QWidget *parent, Qt::WFlags f )
    : HelloWorldBase( parent, f )
{
    /*
    The code bellow will set the method sayHello as signal
    handler (event handler) for the clicked signal assined to
    the button in the window.
    */

    connect(btnSay, SIGNAL(clicked()), this, SLOT(sayHello()));
}

/*  
 *  Destroys the object and frees any allocated resources
 */

HelloWorld::~HelloWorld()
{
    // no need to delete child widgets, Qt does it all for us
}

/*
 *  The method below will act as event handler for a button click.
 */

void HelloWorld::sayHello()
{
    //Display a information messagebox showing the contents typed
    //in the input box.
    QMessageBox::information(this, "Say hello to:", txtName->text(), QMessageBox::Ok); 
    //Close the aplication after close the messagebox.
    close();
}

In the next article I will show how to build, package, install and run the application on emulator.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>