Forgot your password?
typodupeerror

C++ GUI Programming with Qt 4 148

Posted by samzenpus
from the read-all-about-it dept.
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 bn.com. 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:
  • Interoperability (Score:2, Insightful)

    by lkeagle (519176) on Monday August 07, 2006 @04:03PM (#15861060) Homepage
    Does the book explain any methods for dealing with interoperation with other frameworks? Gnome, perhaps?

    I've always found that the most useful books are the ones that provide direction on how to get your application to work well with others...

    P.S. F.P.
  • The free edition (Score:2, Insightful)

    by ratta (760424) on Monday August 07, 2006 @04:27PM (#15861202)
    does not require the application to be GPL, it requires it be open source (OSI), since the free edition is dually licensed GPL/QPL. The funny thing is that if Qt were QPL only, you could use almost all opensource licenses (eg. BSD) but not GPL, because the GPL require anythings you link with to be GPL compatible as well! So dual licensing is really necessary :)
  • by ardor (673957) on Monday August 07, 2006 @04:29PM (#15861213)
    Indeed, GUI development is one of the areas where managed environments shine. However, it IS possible to deal with this in C++. 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. It is a very simple concept, but works very well. I can write "new Label(mainwindow,"Here is my label",22,11);" and do not ever have to worry about a delete call or about memory leaks.

    IMO the best thing is to make the GUI purely data-driven, however. Stuff the entire layout in a XML, and the application only has to connect its event handlers to the named signals (assuming a signals-based event dispatching mechanism). AFAIK libglade does it like this. The huge advantage is that redesigning the interface no longer requires code modification. Don't like the button position? Move it down, you dont even have to recompile.
  • by Anonymous Coward on Monday August 07, 2006 @04:35PM (#15861264)
    Has anyone using the QT API noticed that its behavior when it encounters an error isn't well-defined or easily detectable by the user of the library? For instance, look at QFileInfo's size member function [trolltech.com].

    Returns the file size in bytes, or 0 if the file does not exist or if the size is 0 or if the size cannot be fetched.


    So, if it returns a zero, that could mean that there is a problem that your application may need to do something about or report, or the file does not exist, or the file exists but is zero bytes. How incredibly uninformative. But at least that member function documents its behavior it something bad happens. As I glanced through the other member functions, it became apparent that most of them do not bother to say what happens when the shit hits the fan. No return codes, no exceptions, no nothing.

    Sadly, many of the classes in the library are like this. Sure, I could write an application using QT, but I wouldn't know how to handle failures simply because QT doesn't document the behavior in those cases. This is surprising, considering that QT is considered a quality library that is worthy of use in commercial applications. Granted, most of the time, we'll only execute the "happy path", and there will be no problems with this lack of documentation. However, we shouldn't throw caution to the wind and assume everything will be OK.

    I can't help but think that this could have been avoided if QT would have embraced exceptions. Then, the API could avoid using C-like return codes and maintain its elegance but still report errors in a manner that is convenient for handling and documentation. QFileInfo::size() could be documented to throw QFileNotFoundException and QIOException, for example, making it easy for the user of the class to tell what happened. But they would have to rethink some of their code if they did this, because the naked-pointer-filled code that they have now would not be exception safe at all. I doubt that they will make the switch any time soon because everyone seems to be so happy with it the way it is today.
  • by Jerry (6400) on Monday August 07, 2006 @05:26PM (#15861655)
    I've seen you post this question on QtCentre, too.

    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.

    What's so hard about that? You've never run into tri-states before?
  • by AuMatar (183847) on Monday August 07, 2006 @05:46PM (#15861808)
    Returning a negative number for failure would have been better- you don't have to worry wether 0 means an empty file or an error, it would always mean an empty file, and < 0 would always mean an error. You could also then return different negative numbers for different error conditions. It may have a work around, but its a poor design.
  • by dbIII (701233) on Monday August 07, 2006 @09:00PM (#15863008)
    Simple - you learn on the free version. If you are good enough you can then go commercial with a completely closed application and make money - but you have to feed the people who work at troll a bit too with some of this in the form of a licencing fee. If you are not sure it is good enough or you don't want to go through the hassle of convincing people to buy your stuff you release a completely open application. There is too much crap shareware in the world already - even if you write a gem how will people find it?

    However, there may be some cut price licences out there for shareware or you probably can turn your software written on the GPL Qt into the commercial version if you have never released the software and you get a commercial licence. Perhaps look at their website - I've only looked at their GPL stuff.

Your program is sick! Shoot it and put it out of its memory.

Working...