Tobias Koenig
Qt
XML
Posted by Tobias Koenig
 in Qt, XML
 on Friday, July 31, 2009 @ 11:08

Now it is official, last week my xml schema branch on gitorious has been merged into qt master (the upcoming Qt 4.6) by the brave Trolls Peter and Thiago.
Since February, when the internship ended and I had to go back to university, most of the changes to the branch were code cleanups, API improvements and additional documentation. So no new features so far, however there is a long TODO list on my desk that will get some attention as soon as time permits ;)

So what is all this XML Schema thingy about? Why shall I use it my applications? As you can see in the CWE/SANS TOP 25 Most Dangerous Programming Errors the first place in category Insecure Interaction Between Components is held by Improper Input Validation. Hands on, who of us checks all input data that is entered by the user in our application? While it is kind of easy to check input, typed into a QLineEdit, QTextEdit or QSpinBox, with a QValidator or by some other form of manually written source code, validating input data that come from files or via network is more complicated. These kind of data have a special format (e.g. a CSV file, an ODF document or an image) that has to be parsed and interpreted to make sure that no bogus data are pushed into the application. However for accessing these common formats there exist libraries that do the checks and validation for us and return an error in case of a violation.

During the last years the XML format has become more and more popular and the possibility to define own XML dialects (e.g. DocBook, MathML, QtUI) led to a broad adoption in the software world. If you want to parse a XML document you normally make use of a XML parser like QXmlStreamReader or QDomDocument, which do all the low-level parsing for you and throw errors if the input document is not well-formed as defined in the XML standard. Well-formed basically means that tag names, attribute names and attribute values only contain allowed characters and that tags are nested correctly in each other. However well-formedness doesn’t say anything about what tags are allowed (e.g. <html>, <resource> etc.) inside this document and which tag can contain other tags or attributes. So the classic XML parsers can’t help you to make sure that your input data are correct, you will find yourself writing code like the following

if ( element.tagName() != "resource" )
return error;

when parsing XML documents in your application.

Since this problem is as old as XML exists, there has been made many attempts to provide an easy way of validating XML documents according specific constraints. The document type definition (DTD) is an integral part of the XML standard, however its capability to define the structure of a XML document is quite limited. Other attempts are XML Schema and Relax NG, where the former is the official validation language of the W3C and the later the result of a counter-movement to XML Schema, which is seen as too complex and difficult to learn by many day-to-day XML users.

Independent on their complexity, all XML validation languages have one thing in common, they describe in an abstract way, how a valid XML document should look like. In other words they can define a grammar for a language. This grammar (a DTD or XML Schema file) is then used to validate a XML instance document and decide whether it is correct or not.

This functionality can be used with the new QXmlSchema and QXmlSchemaValidator classes in Qt 4.6 now. So instead of checking the single tag names manually, just create a XML Schema definition of the format you want to parse, pass this definition to the QXmlSchema object and then validate it against your input data via QXmlSchemaValidator. The validator will tell you then whether the input data can be processed further as they are valid, or if processing should be stopped because they are bogus. That will take away the burden of error/validity checking from you and does reduce the code size as well… however you still have to iterate over the single XML elements and parse the data into a C++ object representation to work further on the data. Can’t we simplify that somehow?

To cite a famous quote: “Yes, we can!” ;)

The grammar of the XML document that is used for validation contains all information we need to generate C++ code with the following properties:

  1. C++ classes for every type defined in the XML schema
  2. C++ code for parsing (and validating) a XML document and fill the C++ objects
  3. C++ code for writing out C++ objects to XML documents

So instead of defining your C++ data objects first and try to fill them with XML data in a second step, you could write a XML Schema definition first, which describes your XML input data, and then you let generate the matching C++ data objects, the parsing and synthesizing code automatically. That sounds really great (how much time have you wasted by parsing XML documents into C++ structures manually?) and indeed it is! ;) ‘So where is the code’ you might ask… Unfortunately there is no code written yet that could do that, however with having the XML Schema definition as internal object representation the first major step is already done. Creating C++ code from it is just a question of studiousness and time. Let’s see what the future will bring!

Harald Fernengel
Qt
 in Qt
 on Thursday, July 30, 2009 @ 08:39

Today, I’m happy to announce three new babies in the ever growing Qt family. The first one is Qt on VxWorks, a port of Qt to the VxWorks real time operating system (RTOS). Being the youngest, it requires some help to run - an X11 server must be installed to get graphical output. Also, due to the configurability of VxWorks, it’s pretty likely that it won’t even run out of the box, so be prepared for some extra nursing.

The second baby is a bit older - Qt on QNX can run on its own, even without an X11 server, using the Qt Window System (QWS) that up until now has only been available on GNU/Linux. The port contains an experimental io-graphics based QWS screen driver as well as experimental devi-hid based QWS keyboard and mouse drivers. Note that coexistence with QNX’s Photon environment is not possible, since QWS wants exclusive access to the screen. Here’s an extremely amateur-ish video showing Qt on QNX running on a 266 MHz PowerPC chip as well as on an Intel Atom:

Both babies are community supported (Tier3), so be prepared for messy moments. More technical details can be found in our platform notes, the official announcement can be found here.

And the third baby, you ask? She’s about to happen, we wish Robert and his wife all the best, and a special thanks to Robert who merged the ports into Qt’s master branch while waiting in the delivery ward.

Ariya Hidayat
WebKit
Graphics Dojo
Posted by Ariya Hidayat
 in WebKit, Graphics Dojo
 on Wednesday, July 29, 2009 @ 06:53

Google Maps, or any similar application, becomes very important in our digital life. Not surprisingly, it also becomes easier to integrate some map functionalities into our application. This is something that attracts the developer side of each and every one of us. The brand-new, work-in-progress Google Maps API v3, via the use of QtWebKit module, is the instant solution for embedding maps in your own beloved Qt-based application.

Since doing so would be almost trivial (you can even do it within Designer or Creator), I decided to raise the bar a bit. What if, when your press the mouse button and hold it for a while, a magnifying glass (remember Sherlock Holmes?) shows up and you can move it around? Thanks to Qt, surprisingly (again) implementing such a demo is easy, the result is less than 300 lines of code, and thus I am glad to share you how it looks like, in a static screenshot and a moving picture (watch in YouTube):



The code is checked in already to the Graphics Dojo repository, it is securely stashed under the mapzoom subdirectory. If you do not bother to digest the code, here is the trick. I keep a larger version of the map in a separate QWebPage. When the user press-and-hold the mouse button for a while, this hidden page is used to render the maps under the magnifying glass, clipped to the shape of a circle. Simple, isn’t it? Of course, you can still move the maps by dragging the mouse, just like in the normal use of Google Maps.

Now I am sure some of you are ready to throw the questions. What about Qt/S60 version? What about OpenStreetMap? Relax and be patient a bit. The combination of both will surely make a cameo, some time in the near future, in the form of another example (a successor to this one). Meanwhile, enjoy your maps, Sherlock!

cduclos
Qt
Git
Posted by cduclos
 in Qt, Git
 on Tuesday, July 28, 2009 @ 13:18

We all want to get access to new features, but we also want to get stable things. These two goals seems contradictory and in a sense they are. If you want to live in the edge you have to accept some “early adopter problems” but if you want to have only rock solid features, you need to let them mature and “gain experience”. With our new open development process people can get access to all of our newly developed features while they are still in the making, therefore gaining valuable insight on what’s coming in the next release. The problem appears when people uses that “bleeding edge” code to start developing things, how can we provide “stable bleeding edge” features?

The solution is more or less obvious, have a master branch where everything is committed and a master-stable branch that is slightly behind master but it is guaranteed to compile. It seems simple and problem free, however there is still one pending question. How “slightly behind” is master-stable from master? Up to last week, master-stable was manually updated, which led to fewer updates and not so “bleeding edge” code. From this week on we have implemented automatic update of master-stable, so now master-stable will be updated as soon as everything is ready, no human intervention required.

The system goes like this:

  1. Whenever there is a change committed to master, that change triggers a build in all of our tier 1 platforms.
  2. If the build is successful, then a build on all of our tier 2 platforms is triggered.
  3. And if that build is successful, our CI system will push changes from master to master-stable.

Voila, master-stable updates itself! So from now on you have no excuses not to use master-stable to check out what’s happening in Qt while enjoying a “stable bleeding edge” branch.

The avid reader would have noticed that it is the first time my name appears on this blog. I joined Trolltech on September 2007 and I’ve been the QA manager for Qt for the past two minor releases (4.4 and 4.5) and currently I’m busy handling Qt 4.6 :-)

Alan Alpert
Declarative UI
Posted by Alan Alpert
 in Declarative UI
 on Tuesday, July 28, 2009 @ 04:27

Here’s a video of a game we’ve written with Qt Declarative UI:

This completely functional implementation of Same Game is 334 LOC, 127 lines of QML and 207 lines of simple Javascript for handling the game logic. Considering that it includes animated movement and visual effects, that seems rather impressive. But LOC is not a very well defined metric - if you really want to see just how simple Qt Declarative UI makes things then checkout the source from the kinetic repository on gitorious (the kinetic-declarativeui branch) and look for yourself . Don’t worry, those 334 lines include some comments to help explain what’s going on. This demo does have a lot of stuff packed into those lines though, including dynamic object creation and great potential for custom themes.

So have a look at the Qt Declarative UI branch if you want to play with this powerful and emerging technology - or just with the games we’ve implemented with it.

Ariya Hidayat
Graphics Dojo
S60
Posted by Ariya Hidayat
 in Graphics Dojo, S60
 on Sunday, July 26, 2009 @ 13:09

I am not a frequent flyer, but I was told that interesting travel tools, especially for mobile phones, are valuable for those who fly a lot. Conveniently having your itenaries, the ability to track the status of your flights, getting the alert as the flight time changes, and so on, everything at your fingertip, is just one wonderful aspect of modern phone technology. After all, it is the matter of connecting people.

Right after I saw Qt/S60 Tower release some time ago, flight tracking example was one that came to my mind almost immediately (there were others as well, they will show up in the coming weeks). So here it is, I present you the code. Just do a git pull in your local Graphics Dojo repository (you do have it, right? Otherwise clone from the one in gitorous) and find it in the flightinfo subdirectory. Again, we have to thank Alessandro for the improvements to this little example and especially also for the videocast (watch in YouTube) he did, so that we can enjoy Flight Info in its glory:

The code is far from perfect but should serve as a nice basis for further improvement. The way the data is parsed is quite fragile, this could use some love. And because it uses FlightView as the back-end, you can only use it for flights from and to US. There are still few more useful exercises for the readers from this example. For example, the program does not have the ability to find the flight number given the route. This is however doable because FlightView offers that functionality, you just to have the right hook. Also, how about an integration with the previous Weather Info?

Kent
Qt
Posted by Kent
 in Qt
 on Thursday, July 23, 2009 @ 07:25

Did a nifty change in QStateMachine yesterday: QStateMachine now inherits QState. QStateMachine::rootState() is gone; the state machine now is the root state. This allowed us to remove a fair amount of code and API from QStateMachine; most importantly, it makes application code nicer, as you can do new QState(machine) to create a top-level state, instead of the old new QState(machine->rootState()) (whee, that’s something we’ve wanted since day one!). Other than that, behavior and source compatibility is preserved.

An interesting question is… What should QState::machine() return when the state is a state machine? Itself?

Nope, it doesn’t. We now support embedding state machines within other state machines. If the parent of the state machine is a QState, machine() will return the machine of the parent state.

When a QStateMachine is a child state of another QState, the ancestor state machine will treat the nested machine as an atomic state in the state machine algorithm; effectively, the nested machine is treated as a black box. QStateMachine reimplements QState::onEntry() and calls start(), so that when a nested state machine is entered, it will automatically start running. The nested machine is self-contained; it maintains its own event queue and so on. Of course, you can’t have transitions from states in the nested machine to states in the parent machine; you’ll have to use e.g. the nested machine’s finished() signal to trigger a transition from the nested machine to another state in the parent machine (since the nested machine is a state of the parent machine, and not of itself). Well, that should go without saying.

OK, so we haven’t found a use case yet where you’d rather use nested state machines with separate event loops and all instead of just using normal group states within a single machine, where all states can be directly connected by transitions (maybe you can think of a scenario?). But a) it’s neat, and b) it works. As always, these qualities take presedence over any purported absence of use cases.

The documentation for QState still says that “the QState class provides a general-purpose state for QStateMachine”. Seeing as QStateMachine itself is now a QState, this begs the question: Who is really providing what for whom, to what end, and why?

Ariya Hidayat
Graphics View
Graphics Dojo
S60
Posted by Ariya Hidayat
 in Graphics View, Graphics Dojo, S60
 on Wednesday, July 22, 2009 @ 08:17

In the spirit of writing more demos for Qt/S60, I thought about having something that I will actually use on a daily basis. A weather applet came to my mind. While my previous weather example was more a visual web-scraping attempt based on QtWebKit, this time I wanted something else and hence I decided to use the unofficial Google Weather API just like what is used in Weather Globe Google gadget. The query for this Google service will give back the result in an XML format, which is quite easy to parse. Unfortunately Qt/S60 Tower release does not bundle the XML Patterns module yet, so I have to use the classic stream reader to digest the XML data.

Here is the screenshot of this little weather tool running on the desktop. Use right-click to pop up the context menu, from which you can choose one of the predefined places. The dialog to ask the user for a custom location is left as an exercise for the readers. The beautiful icons are taken from the Tango Desktop Project with an additional set from darkobra at devianart.com. Because I want to take into account the different S60 phone resolutions, these vector-based icons are rendered at run-time thanks to our QtSvg module.


Just like in the case of both the digital clock and kinetic scrolling demos, Alessandro was (more than) willing to waste his precious time filming the following video, for our pleasure. It shows the weather info running on Nokia 5800.

The code is just one git pull away from the usual repository, see the weatherinfo subdirectory. As you might guess, it is using Graphics View, although the layout and animation are done semi-manually. This is because I want to make sure it looks good both on Portrait and Landscape Mode. If you are brave and want to experiment, convert the simple animation to use the brand-new Animation Framework slated for Qt 4.6. Still need some challenges? How about using XPath and XQuery to process the XML? Feel free to implement auto refresh and multiple cities features, too!

Last but not least: thank you, Google!

Ariya Hidayat
Graphics Dojo
S60
Posted by Ariya Hidayat
 in Graphics Dojo, S60
 on Sunday, July 19, 2009 @ 01:50

Remember the Flick Charm [1]? Seems that for many developers, it is simple and easy enough to use and so far the feedback has been positive. On the other hand, one thing that people want to do [2] is to have the flick support for widgets that do not subclass QScrollArea. I wrote this new example exactly for this purpose. It is of course a bit more complicated than Flick Charm (because we do not abuse the event filters), but basically it involves subclassing Flickable (an abstract class) and implementing two functions to set and get the scroll offset. Since Flickable does not inherit QWidget, you also need to forward the interesting mouse events there. And if you still want to receive a click event (useful for e.g. a selection list), there is an option to do so. Now you can kinetically scroll anything!

The code is already landed in the repository (under the flickable subdirectory). There are both the Qt/C++ version and PyQt one (thanks to David for the latter!). The example is a dual-color list which draws almost a thousand items. The code works just fine on Qt for desktop, however just like my last digital clock example, try to grab Qt/S60 and then compile (and launch) this flickable example on your favorite touch-based S60 phones. We spent a great deal of time to ensure that it can scroll fast on real devices. So far it has been tested on both on Nokia 5800 and Nokia N97. Again, thanks to our Video Maestro Alessandro, check the following YouTube video as the proof:

What about bouncing-on-edge feature? Stay tuned, it will be here in a few weeks :)

Footnotes:

[1] On a related note, if you are still using Flick Charm, recently I also managed to fix the scrolling bug there, so make sure you grab the latest version. Also, for those who think the charm does not work on e.g. a list view (because the list would go wild), double check again: did you set the scroll mode to ScrollPerPixel?

[2] And BTW, if you can wait, you have the alternative to use Declarative UI because then you can leverage the pan and flick feature without writing any custom code at all.

Jens
Uncategorized
Qt
Styles
Styles/QGtkStyle
Posted by Jens
 in Uncategorized, Qt, Styles, Styles/QGtkStyle
 on Friday, July 17, 2009 @ 17:03

Both GNOME and KDE has a nice little control panel that allows you to configure how you want your toolbars to appear. Pure Qt applications show only the icons by default. Unfortunately, I don’t think it is a good idea to suddenly change this behavior, but we can do the next best thing and allow you to opt-in. Starting with 4.6, you can use setToolButtonStyle(Qt::ToolButtonFollowStyle) on your mainwindow, tool bar or tool button to make your application dynamically follow desktop settings.

Here is a Qt app on GNOME with “Text below Items”:

below31.png

And this is how it looks with with “Text beside Items”:
beside3.png

There are a few things you should do before you enable this setting though. First you should allways set QAction::IconText, otherwise your annoyingly long tool tip text will suddenly appear next to your icon. However, as you can barely see in the second screenshot, doing that is not enough to properly support Qt::TextBesideIcon since putting all that text into the toolbar generally takes up a bit too much space.

To solve this problem we added QAction::setPriority(QAction::Priority). In other words, you can now mark actions as more or less important. Quite handy for dynamic layouts determining which items to collapse first. In this case,  actions with low priority will hide their label automatically when changing to this desktop setting.



© 2008 Nokia Corporation and/or its subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.
All other trademarks are property of their respective owners.