![]() |
Home · Examples |
[Address Book Tutorial][Next: Address Book 2 - Adding Addresses]
The first part of this tutorial covers the design of the basic graphical user interface (GUI) we use for the Address Book application.
The first step to creating a GUI program is to design the user interface. In this chapter, our goal is to set up the labels and input fields needed to implement a basic address book application. The figure below is a screenshot of our expected output.
We start by defining AddressBook as a QWidget subclass and declaring a constructor. We also use the Q_OBJECT macro to indicate that the class uses internationalization and Qt's signals and slots features, even if we do not use all of these features at this stage.
class AddressBook : public QWidget { Q_OBJECT public: AddressBook(QWidget *parent = 0); private: QLineEdit *nameLine; QTextEdit *addressText; };The class holds declarations of nameLine and addressText, the private instances of QLineEdit and QTextEdit mentioned earlier. You will see, in the coming chapters, that data stored in nameLine and addressText is needed for many of the address book's functions.
We do not need to include declarations of the QLabel objects we will use because we will not need to reference them once they have been created. The way Qt tracks the ownership of objects is explained in the next section.
The Q_OBJECT macro itself implements some of the more advanced features of Qt. For now, it is useful to think of the Q_OBJECT macro as a shortcut which allows us to use the tr() and connect() functions.
We have now completed the addressbook.h file and we move on to implement the corresponding addressbook.cpp file.Implementing the AddressBook Class
The constructor of AddressBook accepts a QWidget parameter, parent. By convention, we pass this parameter to the base class's constructor. This concept of ownership, where a parent can have one or more children, is useful for grouping widgets in Qt. For example, if you delete a parent, all of its children will be deleted as well.
The following code example is written in c++.
AddressBook::AddressBook(QWidget *parent) : QWidget(parent) { QLabel *nameLabel = new QLabel(tr("Name:")); nameLine = new QLineEdit; QLabel *addressLabel = new QLabel(tr("Address:")); addressText = new QTextEdit;Within this constructor, we declare and instantiate two local QLabel objects, nameLabel and addressLabel, as well as instantiate nameLine and addressText. The tr() function returns a translated version of the string, if there is one available; otherwise, it returns the string itself. Think of this function as an <insert translation here> marker to mark QString objects for translation. You will notice, in the coming chapters as well as in the Qt Examples, that we include it whenever we use a translatable string.
When programming with Qt, it is useful to know how layouts work. Qt provides three main layout classes: QHBoxLayout, QVBoxLayout and QGridLayout to handle the positioning of widgets.
QGridLayout *mainLayout = new QGridLayout; mainLayout->addWidget(nameLabel, 0, 0); mainLayout->addWidget(nameLine, 0, 1); mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop); mainLayout->addWidget(addressText, 1, 1);Notice that addressLabel is positioned using Qt::AlignTop as an additional argument. This is to make sure it is not vertically centered in cell (1,0). For a basic overview on Qt Layouts, refer to the Layout Classes document.
In order to install the layout object onto the widget, we have to invoke the widget's setLayout() function:
The following code example is written in c++.
setLayout(mainLayout); setWindowTitle(tr("Simple Address Book")); }Lastly, we set the widget's title to "Simple Address Book".
int main(int argc, char *argv[]) { QApplication app(argc, argv); AddressBook *addressBook = new AddressBook; addressBook->show(); return app.exec(); }We construct a new AddressBook widget on the heap using the new keyword and invoke its show() function to display it. However, the widget will not be shown until the application's event loop is started. We start the event loop by calling the application's exec() function; the result returned by this function is used as the return value from the main() function.
Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt Jambi 4.5.2_01 |