Transcript of talk: "The Qt Sorcerer's Apprentice - Doing Things With Qt That Qt Wasn't Made For"

KDE Contributor and Developer Conference

Notes taken by: Jonathan Riddell (these are not official material)

More information on the talk can be found here

Jesper was going to do this talk but he's broken his foot. He will be watching the live screen so "hello Jesper!".

David, Jesper and Kalle have been collecting tricks with Qt for a while now.

An program by their company for computer assisted surgery, a receiver knows the position of the knife to a micrometre and the surgeon looks at the screen not the actual patient. The customer wanted something that looked completely different from what everyone else had. So a designer sent some designs as PNG files. Alpha blending and interesting tab widgets looking quite snazzy. This was to run on ancient Sun workstations without RENDER extensions. Trolls say that Qt can do alpha blending but only when RENDER is enabled. Every picture has a transparency level, if you don't have one you have to do manual alpha blending done by the client. bitblt used to copy to xserver.

Practical Qt is a book by Matthias and Jesper published by dpunkt-Verlag in English. Questions come from their own programming problems, Qt courses, qt-interest mailing list and #qt the answers have been put together into this book. These examples come from the book.

An intelligent busy cursor blocks user input during a long operation using an event filter which eats events when the cursor is busy.

Shoot a beastie, what's this widget? how big is it? What's its parent? Which signals/slots does it have? Use ShootABug and click on it to find out. Demonstration of a basic application where you could shift-click on a widget to get useful debugging information. The actual event is eaten by an event filter to stop your shift-click activating the button. Another click makes the widget color-cycle through shades of grey which lets you see how far the widget extends.

Floating point number spinbox, need to alter QSpinBox a bit but works well. Scale the input by a power of 10. A QDoubleValidator sorts out the display.

Drawing on screen is easy because everything works in menus. Printing requires you to print in centimetres. How do you print screen to a size on paper? This technique also works if printer has a high resolution, use a QPaintDeviceMetrics which lets you get the resolution (DPI) then you can convert. Margins have to be taken care of.

Tied Splitter, space split in 4 but with both horizontal and vertical splitters moving as one each. Requires two splitters to be tied. There is no API to get the movement of the splitter before it has been dropped. eventFilter helps again because you need to know when the mouse is moving. You have to wait for the widget to be moved before you get any event but the eventFilter is called before this. You need a 0 length timer to allow the move then move the tied splitter. This is a subclass of QSplitter.

Matthias Ettrich shouts from the back "don't you mess with the toolkit!".

By default a QListView will show the full text as a tool tip if it is too long to be displyed. What if you want to show a different tooltip? KListView has a method for this marked as "//FIXME". Subclass QToolTip and reimpement maybeTip then mess around with itemAt/itemRect/QHeader.

Sometimes people want to get rid of the QHeader in a listview, you can do this with header()->hide(). You don't want to delete the header. With QTable you also have to setLeftMargin()

Finally, an example of writing on a child widget. How can QPainter draw on top of a child widget. When your own widget repaints itself the child widget area is clipped out. If you remove the clipping and a window is moved over the child that will get a repaint event and won't know how to do it. Demo of painting a cross on top of a lot of button widgets. You need double buffering, painting the child widgets into the buffer pixmap. QPainter redirection is a method which says "you don't want to use this method, go away". Use that to get the child to paint to you pixmap with some logic to stop the paint event going round in circles. It all works quit well.

This talk shows that Qt is a fantastic toolkit that tries to get out of your way as much as possible. No vendor can guess all the uses for the toolkit. One criteria for the quality of a toolkit is that it can be adapted to what you want to do with it. Kalle admits to spending the first 2 years of his career writing crap Motif code and Motif gets in your way as much as possible. Why are we the coolest desktop environment? Not just because we have the coolest developers here ("woo!") but also because we have the best toolkit. The next time you meet a GTK or Java programmer ask them how to do these thing.

Ian mentions an idea of when What's This is activated the possible areas with What's This text are highlighted, would this be possible? You could use this method, another cheaper way might be to give those widgets a property that change how they are drawn.

[ Edit ]