Home · Examples 


Qt Jambi Tutorial 2 - Calling it Quits

[Previous: Qt Jambi Tutorial 1 - Hello World!][Qt Jambi Tutorial][Next: Qt Jambi Tutorial 3 - Family Values]

Code:



Having created a window in Chapter 1, we will now go on to make the application quit properly when the user tells it to.

We will also use a font that is more exciting than the default one. We give the entire source code of the application:

public class Quit
{
    public static void main(String args[])
    {
        QApplication.initialize(args);


        QPushButton quit = new QPushButton("Quit");
 
        quit.resize(80, 40);
 
        quit.setFont(new QFont("Times", 18, QFont.Weight.Bold.value()));



        quit.clicked.connect(QApplication.instance(), "quit()");


        quit.setWindowTitle("Calling It Quits");
        quit.show();
        QApplication.exec();
    }
}

Line by Line Walkthrough

        QPushButton quit = new QPushButton("Quit");
This time, the button says Quit and that's exactly what the program will do when the user clicks the button.
        quit.resize(80, 40);
We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used
QFontMetrics to set right size, or let QPushButton choose a reasonable default.
        quit.setFont(new QFont("Times", 18, QFont.Weight.Bold.value()));
Here we choose a new font for the button, an 18-point bold font from the Times family. It is also possible to change the default font for the entire application, using QApplication::setFont(). We fetch the value from the Weight enum as the weight of the font is given as an int in the constructor.
        quit.clicked.connect(QApplication.instance(), "quit()");
We connect the clicked signal to the quit() slot in QApplication (QApplication.instance() returns the application's unique QApplication instance). clicked is an instance of the Signal0 class and quit() is a regular method in QApplication that quits the application. When connect() is invoked a one-way connection between the two QtJambiObjects is established. After the slot is connected to the signal the quit() method is invoked when a method on the signal is invoked; this is called emitting the signal. In this case, the application will exit when the user clicks on the button with the mouse.

Every Qt Jambi object can have both signals (to send messages) and slots (to receive messages). All widgets are Qt Jambi objects, since they inherit QWidget, which indirectly inherits QtJambiObject.

This signal and slot mechanism is perhaps the most central feature of Qt Jambi. The Signals and Slots documentation describes this topic in detail.

Running the Application

When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.

See Chapter 1 for how to compile and run the application.

Exercises

Try to resize the window. Press the button to close the application.

Are there any other signals in QPushButton you can connect to quit? [Hint: The QPushButton inherits most of its functionality from QAbstractButton.]


Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Jambi 4.5.2_01