Biz & IT —

First look: Qt 4.4.0 with Phonon and WebKit

Trolltech has released the Qt 4.4. engine.

Trolltech has released the Qt 4.4.0 technical preview, a pre-release that allows software developers to begin experimenting with the new features that the company has implemented for the next version. Qt is an open-source development toolkit that simplifies cross-platform application development. Qt serves as the underlying widget system for the KDE desktop environment and is also used in some popular cross-platform commercial applications like Skype, Google Earth, and Adobe Photoshop Elements.

I compiled the technical preview release from source code and tested it on my desktop computer with Ubuntu 7.10. For testing purposes, I experimented with several of the new features, examined the included demo applications, and created a few simple programs of my own.

The 4.4.0 technical preview adds a new concurrency framework, enhanced XML support, the Phonon media framework, and an integrated HTML renderer widget based on WebKit. This release is also the first to include support for rendering widgets on a GraphicsView component—a feature that will enable Qt developers to create richer and more dynamic user interfaces.

Phonon: a sound strategy

The Phonon multimedia framework is an abstraction layer that makes it possible for developers to swap seamlessly between various underlying media libraries without having to re-implement application code. Phonon was originally developed by the open source software community for KDE 4, but has also been adopted by Trolltech for inclusion in Qt. Trolltech created several new backends for Phonon and has contributed the source code to KDE with the intention of continuing all Phonon development under the aegis of the KDE project.

Supported Phonon backends include Xine, GStreamer, Quicktime 7 on Mac OS X, and DirectShow 9 on Windows. The inclusion of Mac and Windows backends makes it possible for developers to create multimedia applications that are compatible with those operating systems without having to write custom platform-specific code. Phonon also makes it so that API changes in underlying media engines will only have to be dealt with in one place—at the toolkit level—rather than in every application. Although Phonon simplifies development of cross-platform media playback and recording applications and makes it easier to leverage new media engines as needed, the limitations of the somewhat sparse API make it unsuitable for more elaborate software like audio and video editing programs.

WebKit in Qt: a new spin on Apple's web

Phonon isn't the only new feature in Qt 4.4.0 that has its origins in the KDE community. The 4.4.0 technical preview also includes WebKit, an HTML rendering engine that Apple developed based on KDE's KHTML framework. In addition to serving as the HTML renderer in Apple's Safari web browser, WebKit is used in various contexts by Nokia, Adobe, and Google. Trolltech has integratedsupport for WebKit directly into Qt, making it easy for developers to embed WebKit renderers in their Qt applications. The toolkit provides WebKit through a QWebView widget that can be used like any other Qt widget and exposes functionality through regular Qt properties and signals. The QWebView widget is even supported by Qt Designer, Trolltech's drag-and-drop user-interface design tool.

A QWebView widget on a form in Qt Designer

Trolltech initially announced plans to include WebKit in Qt at Akademy 2007, a KDE event that took place in Glasgow. The announcement was a major factor that influenced prominent members of the KDE community to advocate adoption of WebKit in KDE's Konqueror web browser. For more insight into the broader implications of WebKit for the KDE community, check out Troy's article about the KHTML/WebKit unforking from earlier this year.

Widgets on QGraphicsView: bring the bling

One of the most visually impressive new features in the Qt 4.4.0 technical preview is support for widgets on Qt's QGraphicsView canvas. The QGraphicsView widget, which was added in Qt 4.2 to replace QCanvas, offers an object-oriented model/view system for laying out and manipulating 2D graphical items. In the technical preview, regular Qt widgets can now be added to the QGraphicsView and manipulated like any other canvas item. This feature is implemented with full support for input redirection, which means that users can still fully interact with widgets that are rendered on the canvas. This makes it possible to create sophisticated widget transition animations and rich user interfaces that incorporate graphics alongside regular controls. Widgets on the canvas can be transformed in many different ways, and support scaling, rotation, angle adjustment, color filters, and all sorts of other neat tricks.

Several simple applications are included to demonstrate various ways that developers can take advantage of this functionality. One of the demos shows an assortment of miniature dialog windows that jump out at an angle when the cursor moves over them. Another demo shows a series of buttons on a flat pane that seems to spin around and display widgets on the back in response to clicks.

Trolltech's embedded dialog QGraphicsView demo

The embedded dialog demo made me giddy with excitement, so I decided to play with the source code, which is included with the technical preview. Using the embedded dialog demo as a starting point, I had no trouble at all adapting the code to use other widgets. I wanted to test it with a widget that does particularly sophisticated rendering, so I plopped down a QWebView. Much to my surprise, it rendered pretty darn well. I can imagine using something like this to implement a Cover Flow-style tab switcher for a web browser, for instance.

A QWebView scaled and at an angle, rendered on a QGraphicsView

Adding support for widgets on the QGraphicsView was obviously an extremely challenging undertaking. The Trolltech developers seem quite proud of what they have accomplished. In a blog entry from October, developer Andreas Hanssen talks about some of the challenges that they faced when they were implementing the feature.

"Early on, I was worried about many things. Prior to 4.2, I was almost convinced that adding embedded widget support to Graphics View would be an insane operation...Looking back, I don't quite understand how we've made it...[W]e've found solutions to almost every single problem that we foresaw. And we even uncovered more problems. And solved those too. And what we got, what we have, and what we're honing, stabilizing, testing and reviewing these days, makes me emotional," wrote Hanssen. "I had the sensation that things were fitting so well together that you'd almost think we hadn't invented these APIs, but rather uncovered ancient APIs from a past civilization."

Indeed, the few remaining problems are actively being resolved. During my tests I only encountered two issues with using widgets on the QGraphicsView. The first problem is that popups (like the menu that comes out of a combo box) aren't rendered properly yet. Hanssen has actually already come up with a way to fix this problem, but the patch isn't in the technical preview. The other problem is with performance. Applications with widgets on QGraphicsViews seem to be very processor intensive. This issue is also currently being addressed. In light of the fact that this is a pre-release technical preview, bugs like this are entirely forgiveable. It's likely that these issues won't remain in the final release version of Qt 4.4.

Conclusion

There are several other noteworthy features in the Qt 4.4.0 technical preview, but obviously a complete examination is well beyond the scope of this article. More information is available at Trolltech's sneak preview page. The source code for the technical preview for Windows, Linux, and Mac OS X is available for download from Trolltech's web site.

Trolltech's demo application showcases Qt's features

GTK is my toolkit of choice for the vast majority of my Linux desktop application development, so my perspective on Qt is that of an outsider looking in at a very different kind development platform. Despite my limited familiarity with the toolkit, I'm very impressed with the new functionality in Qt 4.4, and I'm looking forward to seeing how the KDE community and other Qt users leverage the features.