Example Project: Using QApplication and QLabel

Example Project Using QApplication and QLabel

Example 3.1 shows a simple main program that creates a QApplication and a QLabel, manipulates some QStrings, and then pops up a graphical window.

This example makes use of Qt's QString class, which is a dynamic string implementation that supports the Unicode standard.[1] QString contains many helpful member functions for converting and formatting strings in different ways. This example also makes use of QTextStream, a very flexible Qt class that can provide a well-developed stream interface for reading or writing text to various objects (e.g., text files, strings, and byte arrays).

[1] http://www.unicode.org/standard/standard.html

Example 3.1. src/qapp/main.cpp


int main(int argc, char * argv[]) {
 QApplication myapp(argc, argv); <-- 1
 QWidget wid; 	 <-- 2
 qDebug() << "sizeof widget: " << sizeof(wid)
 << " sizeof qapplication: " << sizeof(myapp) ;
 QString message;
 QTextStream buf(&message); <-- 3
 buf << "A QWdget is " << sizeof(wid)
 << " bytes. 
A QObject is " << sizeof(QObject)
 << " bytes. 
A QApplication is " << sizeof(myapp)
 << " bytes.";
 qDebug() << message;
 QLabel label(message); 	 <-- 4
 label.show(); 	 <-- 5
 return myapp.exec(); 	 <-- 6

(1)All Qt GUI applications need to create one of these at the start of main().

(2)We are only creating this to see how big it is.

(3)This is a stream that allows us to "write" to the string, similar in usage to std::stringstream.

(4)Create a GUI widget with the message.

(5)Make the label visible.

(6)Enter the event loop, and wait for the user to do something. When the user exits, so does myapp.exec().

To build this app, we need a project file. A project file describes the project by listing all of the other files, as well as all of the options and file locations that are needed to build the project. Because this is a very simple application, the project file is also simple, as shown in Example 3.2.

Example 3.2. src/qapp/qapp.pro

SOURCES += main.cpp

The first line, TEMPLATE = app, indicates that qmake should start with a templated Makefile suited for building applications. If this project file were for a library, you would see TEMPLATE = lib to indicate that a Makefile library template should be used instead. A third possibility is that we might have our source code distributed among several subdirectories, each having its own project file. In such a case we might see TEMPLATE = subdirs in the project file located in the parent directory, which would cause a Makefile to be produced in the parent directory and also in each subdirectory.

Part I: Introduction to C++ and Qt 4

C++ Introduction


Introduction to Qt



Inheritance and Polymorphism

Part II: Higher-Level Programming


Introduction to Design Patterns


Generics and Containers

Qt GUI Widgets


Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments


MP3 Jukebox Assignments

An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net