Deploying Qt Quick Applications that rely on Qt Mobility on Windows…

Whew! Some things seem to take forever for me to figure out!

Here’s the scenario: we’re working on a Qt Quick (QML and C++) application at the lab to run on Microsoft Windows. We’d like to deploy the app to a bunch of study participants to use for a week or so. It’s obviously not practical for me to go around and set everybody up with Qt, Qt Mobility, and build our app. (Especially not this app; we’re using the Qt 4.8 prerelease at this point!). So we need to build a Windows installer that includes the Qt DLLs, our app and its QML, and the Qt Mobility plugins we’re using. We’re using Qt Mobility in our QML — so we need to include the Qt Mobility plugins for Qt Quick, not just the Qt Mobility libraries.

Packaging a Qt application for Windows is straightforward — just follow these instructions for packaging as a shared library. But what about the Qt Quick plugins for Qt Mobility we need?

Well, Qt Quick supports modules for that, as described here. In fact, if your application required QtWebKit’s Qt Quick plugin, you’d create a QtWebKit directory, stick a qmldir file with the line

plugin QtWebKit

copy qtwebkitplugin.dll into the directory you made and Bob’s your uncle!

We use QtMultimediaKit, so at first I created a QtMultimediaKit directory, created a qmldir file in that directory, and copied QtMultimediaKit1.dll from my Qt Mobility build output directory to the QtMultimediaKit directory I’d created.

No luck — in fact, the app just launches and shows nothing. (To add insult to injury, my app uses Qt’s support for Open GL, so not only did I not get the black screen characteristic of a QML error, but I got junk in the application window and no errors on the console.)

Turns out that QtMultimediaKit1.dll goes in the same directory as the application, and the declarative plugin file is somewhere else! If you look in the plugins directory, you’ll see a declarative directory, and the actual Qt Quick declarative plugin you’re looking for is in there! In my case, I needed to copy plugins/declarative/multimedia/release/declarative_multimedia.dll to a new directory QtMultimediaKit, create a single qmldir file that read

plugin declarative_multimedia

and put that adjacent to my application executable.

So, the resulting files and directories for me look something like:

imageformats/qgif4.dll
imageformats/qjpeg.dll
QtMultimediaKit/declarative_multimedia.dll
QtMultimediaKit/qmldir
msvcr90.dll
QtCore4.dll
QtDeclarative4.dll
QtGui4.dll
QtMultimedia4.dll
QtMultimediaKit1.dll
QtNetwork4.dll
QtOpenGL4.dll
QtScript4.dll
QtSql4,dll
QtXml4.dll
QtXmlPatterns4.dll
myapplication.exe

Now off to learn how to make a Windows installer. I think I’ll try Inno Setup.

Nokia N9 PR 1.1 and new Qt SDK…

The N9 PR 1.1 is out, with lots of great updates. Your N9 should see the update automatically; if like me you’re in a hurry:

  1. Open Settings
  2. Choose Applications
  3. Choose Manage Applications
  4. Press Updates
  5. Press the refresh button at the top of the display.

Budget about a half hour for the update; it downloads the update over WiFi or the cell connection, and then re-flashes your device. While it’s updating, your phone won’t do anything else (although I had a calendar alarm pop up during the update process!) I followed the suggestion and made a back-up first, although it looks like user data preservation works correctly; I didn’t lose anything after the update.

To go along with this, be sure to check out the new Qt SDK! See here for the links and news about what’s in it. I’ve been using Qt Mobility 1.2 on the desktop for a while, so it’s nice to have an integrated build with all the bits and pieces.

Happy hacking!

Enabling WiFi Hotspot on Nokia N9s in the United States…

I’m going to forget this the first time I have to reset my Nokia N9.

As it turns out, the WiFi Hotspot mode on the N9 doesn’t fully meet FCC SAR regulations, so it’s disabled in the United States (launching it generates a brief error and then the app exits). While I appreciate Nokia’s respect for regulations, I don’t plan on holding my N9 close to soft tissues while in hotspot mode, and dang it, I need hotspot mode occasionally.

Fortunately, brighter folks than me have found a hack; here’s a link to a post by salyavin at talk.maemo.org. It involves adding a new startup script that touches a configuration file on boot to enable the application. You’ll need to download the script from the link, install developer mode if you haven’t, and copy the script to the correct location.

Now my iPhone can stay safely locked in my desk drawer on weekends when I go cafe-hopping!

Edit: Another link with the same information is

Are your Qt signals not disconnecting?

This was probably obvious to everyone except me…
disconnect(object)
is not the same as
disconnect(object, 0, 0, 0)

This makes perfect sense when you look at the overloaded function definitions for disconnect, one of which reads:
bool QObject::disconnect ( const QObject * receiver, const char * method = 0 ).

So if you write disconnect(object), you’re really disconnecting all signals received by object, not all signals sent by object.

This tripped me up in a big way today; I was switching the slot a signal was connected to at run time as part of a simple state machine, and the state machine’s various slots were all being invoked! Not a nice situation, but easily solved by using the correct variant of disconnect.

A Peek at Server Logs

So, I’ll happily admit to anyone that asks that I’m terrible about self-promotion; if anything, this blog is an extension of my books: writing about what matters to me for you, hoping to find a match. I don’t pick topics because they sell, or because I think they have wide appeal; I’ve always been drawn to writing about problems that I’ve encountered that I think others may encounter as well.

That’s why I found this chart from StatPress so interesting tonight — it’s a look at the most commonly read entries on this blog.

Blog Topics By Request
Blog Topics By Request

I love Lisp — no denying it. And I’ve spent some time plinking away at using Lisp on various machines; my time spent coercing CLIM to work on Mac OS X is clear testimony to that fact. Unfortunately, I’ve never been able to turn my affection for Lisp into a paying concern; in fact, I’ve barely been able to integrate it with my day job (this paper is as close as it gets, folks!) because my primary career focus is mobile, not any of the problems Lisp typically targets.

Unfortunately, given the amount of time I have for coding, Qt wins out over Lisp and CLIM, or even plain Lisp; the day job is currently focused on research on mobile UIs at Nokia, and Qt (or Windows Phone 7, which I’m just getting started with on my own time) is where it’s at.

I’ll admit some frustration, though — I’d like to be of more use to the community that finds these pages useful, especially because I think it’d be more fun sometimes than just quickly knocking out a few pages about problems I’ve solved on the day job for other Qt developers to refer to. So, open questions to all you people who got here looking for Lisp on Mac OS X: what else can I help you with? Where are your pain points? And what the heck are you using Lisp on Mac OS X for, anyway?

Yet more Qt tips…

Two more for you!

Shared Data
Like Qt’s copy-on-write shared data semantics? Want to do the same for your own class? It’s easier than you think. Simply inherit from QSharedData, which provides the necessary reference-counting semantics. See here for more info.

Running a Slot on a Different Thread
Sometimes you want to schedule a single operation on a different thread; you can do this using QObject::moveToThread to move an object’s thread affinity to a different thread, and then trigger the slot as a queued connection. See here for a discussion of how Qt’s threads and Qt’s object system cooperate.

More Qt tips…

Following on the heels of last week’s post, here are a few more thoughts gleaned from the experience of attending last year’s Qt Developer Days. These are all old news, especially if you’ve been doing Qt for a while, but putting them here may be useful to you, and clears out a page of thoughts in my engineering notes!

QImage vs. QPixmap
Remember that QImage is the general-purpose class for image manipulation whose data lives in main memory, while QPixmap objects are usually backed by the GPU. This has some ramifications for your application:

  • QImage is optimized for I/O and direct pixel access.
  • QPixmap is optimized for direct screen display.
  • On constrained platforms like mobile devices, there may be relatively little GPU RAM available, so you should consider using QPixmap objects sparingly.

Regardless of which you choose, both classes use Qt’s copy-on-write (COW) semantics, so if you have multiple instances of the same image, you pay a small price penalty—basically the cost of the object overhead, not the memory footprint of the image itself.

QStandardItemModel and Performance
Qt’s support for model-view-controller is great, and one of the handiest things about it is that the QStandardItemModel class provides everything you need in a model to get going. It is, however, not necessarily the most efficient way to do so.

If you have your own model already—say you’re porting an existing application, or working with a library with its own data representation—it really is a good idea to implement your own model class from QAbstractItemModel. I’m not bad-mouthing QStandardItemModel, but it’s based on Qt’s collection classes, and odds are that you know better ways to store your data (especially if there’s a lot of it!) than Qt does.

Personally, my approach is that if I don’t have a data container and I’m only dealing with a handful of data items — say, up to a hundred or so objects with a half-dozen roles or so — I use QStandardItemModel and don’t look back. (Qt’s collection classes are, after all, quite good for general applications). But if I’ve already got a data model, or I know I’m going to be dealing with lots and lots of objects, it’s time for a custom model that leverages my knowledge of the data set I’m wrapping.

Use QtConcurrent for Parallel Computation
I’ve not tried this personally, but QtConcurrent looks really cool for doing any kind of concurrent map/reduce style programming. If you need to write code that leverages multiple cores for concurrent computation, it’s best to look here before trying to roll your own framework using QThread.

A grab bag of Qt tips…

So, last year I went to the Qt Developer conference here in S.F. It’s a great opportunity to meet up with Qt Developers from all over the world. One of the amazing things about Qt is how easy it is to get up and running and be productive; unfortunately, it’s easy to get stuck in a rut where you always do things the same way and don’t necessarily try new ways of doing the same things, or really look at performance in your code, and just generally be a fair-to-middling developer with Qt.

I attended a well-run session on Qt tips and tricks from the trenches, and ended up with two pages of longhand scrawl of good thoughts that I either hadn’t really explored in Qt, or realized I needed to learn more about. These got transcribed to a bullet list of items, some of which I’ve followed up on, and some I haven’t. Here are a few, with some of my observations about them, preserved for posterity and your use!

invokeMethod for Deferred Method Invocation
You often want to run some bit of code just after a method call; deferred initialization is a common case, where your constructor does the bare minimum to set something up, and then once everything’s up and running you want to do a bit more initialization. A single-shot timer is a good way to do this; you can use QTimer::singleShot with a short delay to trigger a slot in your class. You can also do the same thing using QMetaObject::invokeMethod, passing Qt::QueuedConnection as the connection type. When you do this, Qt sends a QEvent and the method is invoked as soon as the application enters the main event loop, which is probably what you were trying to accomplish with the timer anyway.

Implementing << and >> for QVariant Serialization
This is a “well, duh” if you’ve read Qt’s serialization documentation, but you can implement << and >> for your class using QDataStream to support serialization.

I like to take the time and do a human-readable string for QDebug::operator<< at the same time, too, so I can log complex data types to the debug console when doing printf-style debugging.

Use const References with foreach
This should go without saying, but a lot of people forget. If you're iterating across a Qt collection using foreach, don't forget you almost certainly want to use constant references to do so. Otherwise, the implementation makes a copy of each item being passed to you at the beginning of each pass through the loop, and destroys that instance at the end. All of this happens on the stack, of course, but you're still paying the constructor and destructor penalty, and if your object is large, you're thrashing your stack footprint needlessly.

I'll post a few more of these next week!

Qt for the next billion…

I could not argue that Nokia’s announcements in February about the shift in focus to Windows Phone 7 from Symbian and MeeGo has caused some grief for my latest book. While there are a slew of Nokia developers — and from the looks of sales, plenty of people wanting to target Nokia products with Qt on Symbian and MeeGo — Nokia’s announcement certainly rattled some cages. Especially here in the US, the shift has been perceived by some to be a death knell for Qt, at least in the mobile space.

As a result, I’m especially excited by Nokia’s commitment to powering applications for the next billion using Qt. While Nokia’s been clear about their continued support for Qt, it’s hard to imagine mobile development using Qt without the “mobile” part. And while Nokia’s announcement is light on details, the Symbian and MeeGo platforms remain relevant for Qt developers today, while Qt itself promises a path to the future.

I use Qt at the office every day — both as a user on the mobile and fixed devices I use, as well as a developer. Without exception, I remain pleased with its portability and flexibility; I can’t imagine doing the work I need to do on any other platform as easily.

Creating a new class of declarative item…

(It’s been too long since I’ve posted, and I suspect most people think I’ve either abandoned this again or figure I’m no longer doing Qt/QML work. Neither is true; I’ve just been busy, doing QML with Qt at the office!)

The Qt documentation covers this pretty well, but since it feels a little esoteric, it’s worth mentioning that you can create a new declarative item