Please create an account to participate in the Slashdot moderation system


Forgot your password?

C++ GUI Programming with Qt 4 148

Ravi writes "When somebody talks about the Qt Framework, the first thing that comes to mind is KDE one of the most popular Desktops which is built using the Qt library. Qt is a C++ GUI framework created by the Norwegian firm Trolltech which can be used to build applications for a variety of OSes and platforms. The latest version of Qt (Ver 4.0) contains huge improvements over its earlier versions such as advanced modal/view functionality, easy to use template containers and better Unicode support just to name a few. I have been excited about the ease with which one can build GUI applications using Qt. Trolltech provides the library in two licences - the free licence which mandates that the applications developed be released under GPL and a commercial non-free licence which allows one to develop closed source applications using Qt." Read the rest of Ravi's review.
C++ GUI Programming with Qt 4
author Jasmin Blanchette and Mark Summerfield
pages 540
publisher Prentice Hall
rating 9
reviewer Ravi
ISBN 0-13-187249-4
summary A great book to learn the latest version of Qt (Ver 4.0) to create applications that run natively in Linux/Unix, Windows and Mac OS X

I found the latest book on Qt titled "C++ GUI Programming with Qt 4" authored by Jasmin Blanchette and Mark Summerfield and brought out by Prentice Hall to be a remarkable book in that it takes the readers through the basics of creating applications using the latest version of Qt and gradually shifts to explain the more advanced concepts of GUI programming using this C++ framework.

The book, all of 540 pages is divided into three parts - the first part containing 5 chapters dealing with Basic Qt, the second part titled Intermediate Qt which contains 11 chapters and the final part titled Advanced Qt containing an additional 5 chapters.

The major work in picking up skills in a new GUI framework revolves around getting to know all the classes and their member functions. And learning Qt is no different. In the first five chapters (Part I of the book), one gets to know the rudimentary concepts behind creating GUI applications using the Qt toolkit. And remarkably, instead of boring the readers with the different Qt classes, the authors take a hands on approach and introduce the concepts via examples.

In chapters 1 through 5, the readers are introduced to rapid dialog design using the Qt Designer, introduction to the main Qt classes such as QApplication, QObject, QWidget and its child classes as well as a good understanding of the concept of Signals and Slots which form the meat behind communication between different objects in Qt. Apart from that the 5th chapter also introduces to the reader the concept of Double buffering - one which is used popularly by the programmers in eliminating flicker in the GUI interface.

The sixth chapter titled "Layout Management" falls in the second part namely "Intermediate Qt". Here one gets to know and tryout the different layout classes that are available in Qt using which one can easily design sophisticated GUI applications. This chapter also gives the reader an introduction to Multiple Document Interface by way of an example.

Event Processing forms the basis of the seventh chapter where the authors explain how to catch events such as key presses and timers. Apart from that the authors also explain the five levels at which events can be processed and filtered in Qt.

Qt has an excellent collection of classes for creating 2D and 3D graphics. And from version 4, the classes are grouped into modules which need only be plugged in on a need to use basis. The primary class which deals with the 2D graphics engine in Qt is the QPainter class. The use of this class has been explained in detail in the eighth chapter titled "2D and 3D Graphics". Other than that, one also gets to know about the OpenGL module in Qt which makes it very easy to integrate OpenGL code into Qt applications as well as the classes which implement printing.

It is really interesting to see that the entire subject of Qt is apportioned into separate chapters with each chapter dealing with a particular topic. For example, if in the 9th chapter, the authors dwell on explaining the classes which implement drag and drop support, the succeeding chapter elaborates on the Model view controller architecture which forms the basis for most item view classes which implement tables, tree views and lists.

Another thing worth noting is that each chapter is explained with a complete stand alone example which makes it easy to try out what one has learned and also get a better idea of the concepts behind the classes that are explained. So while learning say about the container classes in Qt, the reader can actually try out an example and the code is accompanied by step-by-step explanations which makes it much more simpler to grasp the concepts.

All object oriented languages contain what are known as container classes - those which are responsible for creating sorted and unsorted data arrays, vectors and lists. And C++ has a couple of them in the Standard Template Library (STL). Qt has its own wide array of container classes which sport a number of improvements over STL in that they support implicit sharing (Copy on Write) which gives the applications a significant performance boost. In the 11th chapter titled Container Classes, the authors explain the concept of containers and the Qt classes that can be used for the same.

The next two chapters deal with writing and reading data from files as well as databases. And in each case, the concepts are explained with the aid of examples which makes the narration much more interesting to follow.

Qt has good support for Networking as well as XML which is evident from the 14th and 15th chapters which elaborate on the classes responsible for these. Even though these two short chapters do not cover all the networking or XMl concepts, I found them to impart a good idea of the use of a couple of important Qt classes related to networking and XML.

The 17th chapter titled "Internationalization" falls in the Advanced section of the book. I found this chapter to be an eye opener in that, with Qt's robust use of Unicode, it is possible to create applications which support a wide variety of non-english languages.

But one of the very important concept of multi-threading is explained in the next chapter titled what else "Multithreading" where the authors teach how to create threads easily in Qt.

If one looks at any KDE application, it will be evident how the parent application can be extended by use of plugins. A plugin is a dynamic library which implements a particular interface to provide extra functionality to the user. In the 19th chapter, one gets to know how to create dynamic libraries using Qt which can then be plugged into the parent application.

In the penultimate chapter, the authors explain all the platform specific features and tweaks such as using ActiveX in Windows and handling session management in X11. There are also four pages - which I believe to contain the shortest chapter I have ever come across and where a couple of classes which can be used in programming for embedded devices such as mobile phones are listed. By including this tiny chapter, perhaps, the authors were sending a signal to the developer community that Qt is fully ready for use in embedded development.

From start to finish, I found the book immensely enjoyable. The style of narration of the authors which interleaves the example code with the explanation is ideally suited to pick up skills in using any toolkit without getting bored, especially since the bulk of the learning involves acquiring a fine understanding of the classes the toolkit provides. As a bonus the book also provides an appendix which gives an introduction to C++ for programmers who use Java or C#.

The single CD accompanying the book contains Qt library in the source form for the Linux platform as well as for Windows and Mac platforms. I did not have any problems in compiling the source and installing the library in Linux using the instructions provided in the book. And within a little more than an hour (the time taken to compile the source code), I was able to start coding and compiling the examples given in the book. All in all, I find this book to be an invaluable guide in picking up skills in programming in the latest version of Qt (4.0).

Ravi Kumar likes to share his experiences in programming and in using Linux through his blog on Linux."

You can purchase C++ GUI Programming with Qt 4 from Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
This discussion has been archived. No new comments can be posted.

C++ GUI Programming with Qt 4

Comments Filter:
  • Update on the link (Score:1, Informative)

    by Anonymous Coward on Monday August 07, 2006 @04:03PM (#15861055)
    Slashdot has linked to B & N here, but it seems that Amazon has it much cheaper [].
  • Update for 4.2? (Score:2, Informative)

    by Abby The Wonder Dog ( 993772 ) on Monday August 07, 2006 @04:15PM (#15861128)
    It is too bad that this book couldn't have been based on version 4.2 coming out soon. There are some major new features in the upcoming release, most importantly (IMHO), QGraphicsView, the new canvas. []

    It also looks like they'll try to squeeze in some cool SVG related stuff. []

    Man, Qt simply pwns GTK
  • by gatkinso ( 15975 ) on Monday August 07, 2006 @04:23PM (#15861166)
    Well, when I write C++ code, I rarely use malloc().
  • Re:Platform license. (Score:5, Informative)

    by ardor ( 673957 ) on Monday August 07, 2006 @04:23PM (#15861167)
    Sort of. The Windows version has the same license, but has no VisualC support - this is included in the commercial version only. The free one only supports MinGW.
  • by everphilski ( 877346 ) on Monday August 07, 2006 @04:32PM (#15861239) Journal
    It is possible ... and not too difficult instructions here []

  • by rm999 ( 775449 ) on Monday August 07, 2006 @04:42PM (#15861308)
    And your link doesn't have a referral tag (which I hate because I know those are the next popup windows of the internet).

    It's hard to write a bad review when you know you will make money if people buy it...
  • by Phisbut ( 761268 ) on Monday August 07, 2006 @05:07PM (#15861531)
    I agree that if I want to put a label in a window, I dont want to have to handle its deallocation, i.e. I want a "fire-and-forget" feature. I accomplished this in my wrapper by letting the parent deal with its children.

    Well, that is exactly what Qt does. Every time you create a widget, you pass a pointer to its parent, and then you can forget about the widget. When the parent dies, it'll kill all of its children, and then they'll kill their children too in cascade. I do tons of Qt programming (that's what we use where I work), and memory management of Qt widgets is a breeze, every widget is hierarchically created, and I just need to kill the window when I don't need it anymore to have it kill all its widgets and sub-widgets. No need for a custom-made wrapper, it's all in there already.

  • Re:Update for 4.2? (Score:1, Informative)

    by Anonymous Coward on Monday August 07, 2006 @05:56PM (#15861906)
    "Man, Qt simply pwns GTK"

    Yes, I agree, except for one thing. GTK has the nice feature of being able to switch input methods easily without having to install additional keyboard layouts or whatever. As someone who uses the transliterated Cyrillic input method frequently, I wish QT/KDE would add this.
  • Re:Qt is very nice. (Score:2, Informative)

    by Almahtar ( 991773 ) on Monday August 07, 2006 @06:04PM (#15861965) Journal
    In my senior design class my last year of college, the project was to build a Windows app that did X, Y, and Z - any extra features and polish were left to the students' imaginations. The 3 students that used Qt did substantially better than the others, and their stuff ran on Windows, Mac, and Linux. None of them reported having to change a single line of source code porting from one platform to the next. The 6 Java people didn't do as well as the .net people overall, but their stuff ran on Mac most of the time. They reported spending some time and effort to get the Mac version working. The 8 .NET people by and large got better ratings than the Java people as far as features, usability, and speed, but their stuff only ran on Windows. So while the populations weren't large enough for anything vastly conclusive, it is a tribute to Qt that the 3 students using it did noticeably better than the 14 others just on windows, but also ran on Mac and Linux.
  • by Anonymous Coward on Monday August 07, 2006 @07:47PM (#15862636)
    QFileInfo claims to (and appears to) use caching, so while "i.isFile();i.size()" looks like a race condition, it's really not, right?

    But doesn't getting all the information about a file require more than one call? stat() will get you length and such, but not the value for readLink(), no? So isn't that still a race condition?

    It's not at all obvious from the QFileInfo docs what is atomic, and when you're writing filesystem code that's kind of important.

    (I've written large programs in Qt, and yes, they've made some rather, ah, "interesting" design decisions.)
  • by bit01 ( 644603 ) on Monday August 07, 2006 @07:53PM (#15862675)

    Use QFileInfo::isFile to determine if the file exists, then QFileInfo::size returns the file size in bytes, or 0 or if the size is 0 or if the size cannot be fetched.

    This is buggy. It is a race condition. If the underlying file is deleted or renamed or is replaced by a named pipe in between those two calls the return from both calls is inconsistent.

    This sort of error is unfortunately endemic in GUI code, not just qt, and is a large part of why GUI programs, are often flakey. GUI libraries usually have crappy kitchen sink API's that encourage bad programming practices; almost requiring numerous race conditions and potential security holes unless heroic programming efforts are made to avoid them. The kitchen sink API's are an attempt to improve productivity however a lot more care needs to go into their organisation to discourage bad programming practices, particularly race conditions. In this case they should've guaranteed the caching of all of QFileInfo so that all the information about the file is atomic, making race-free programming easier. Better, they should've had a handle to the file that guarantees that whatever operations are done on the file refer to the same file, including file operations outside of QFileInfo.


    Don't be a programmer-bureaucrat; someone who substitutes marketing buzzwords and software bloat for verifiable improvements.

  • Some Pointers (Score:3, Informative)

    by Uncle_Al ( 115529 ) on Tuesday August 08, 2006 @04:52AM (#15864529)
    Almost every part of GTK+, from the colors to widget shapes, can be changed in user themes. Qt has no way to do either of these without linking in bloated external libraries.
    Well, I guess then the class QStyle [] is completely useless....and setting a "usertheme" with qt-config is something I must have dreamed then....*g*

    Until Qt 4, there was no way to load .ui files at run time.
    Ok...but what is the class QWidgetFactory [] used for then? :-)

    Qt still has no support for "recent documents", requiring the user to keep track of and build this list himself.
    Which is rather easy using QSettings []...

    Have a nice day :-)

"You must have an IQ of at least half a million." -- Popeye