Qt Patches: QGraphicsView & QImageWriter

QGraphicsView

Two and a half years ago I wrote about a UX bug with QGraphicsView where it would reset the selection in the view if you tried to extend it with a rubberband selection. I have been using those changes in the version of Qt I maintain for my software.

I finally took the time to figure out how to use the gerrit code review system for submitting patches to the Qt project, and worked with several other developers to get it in shape for merging into the codebase for Qt 5.5.

Qt Library

Qt Library

The way I implemented the changes in my old post was inelegant but functional, so I cleaned it up for submission. Then, based on suggestions from Andreas Hanssen, Dominik Haumann, and Thorbjørn Martsum, took it to the next level to make it elegant. I was even forced to add tests before it would be accepted! <gasp> (Thanks again guys!)

The changes will appear in Qt 5.5, but they will not be backported to Qt4. I have, however, created patches for both if you need to use them:

Qt4 Patch: QGraphicsView_Rubberband_Select_Qt4_patch.diff

Qt5 Patch: QGraphicsView_Rubberband_Select_Qt5_patch.diff


QImageWriter

I also revisited my patch from three years ago for QImageWriter which added the ability to turn on the optimize and progressive scan switches for writing JPEGs, which I also was maintaining in my own version of Qt.

This work had input from another developer, Gunnar Sletta, and became a better commit because of it (thanks Gunnar!).

These changes will also be part of Qt 5.5 and won’t be backported to Qt4. I have created patches for both Qt4 and Qt5 if you need them:

Qt4 Patch: QImageWriter_Qt4_patch.diff

Qt5 Patch: QImageWriter_Qt5_patch.diff


I hope these changes and patches are useful to someone out there. Based on this positive experience I hope to contribute more small changes I have lying around somewhere…

Extending Selections In QGraphicsView

Overall, the Qt library has done a fantastic job of maintaining relatively simple interfaces with well-named functions that do what they say they’re going to do [unlike MFC for example] and of having objects react the way you would expect them to without much modification. I’ve used it for several cross-platform Windows/Mac OS X/Linux applications and I can usually bend it to my will fairly easily because they’ve provided the right hooks.

One exception I’ve run into recently – and an oversight that is really kind of surprising – is using rubber band selections on a QGraphicsView. What would be natural – and what most people would expect – is that if the user is holding down the “modify selection” key – Control on Windows, Command on Mac OS X – then:

  • clicking a selected object will deselect it without modifying the rest of the selection
  • clicking an unselected object will select it without modifying the rest of the selection
  • clicking and dragging will give the user a way to select several objects to add to the current selection (known as rubber band or drag selection).

QGraphicsView With Selection

QGraphicsView With Objects Selected In Red

But with QGraphicsView – by design – if you have some objects selected (as pictured above), there’s no way to extend the selection by using “rubber band selection” to add to it. Clicking the mouse clears the current selection regardless of whether the user is holding the “modify selection” key. This is a UX design bug. It is counter-intuitive and confusing for users.

QGraphicsView Selection Method

QGraphicsView Selection Method - Holding Down The Control/Command Key Resets Selection

Not only is the default behaviour confusing, there’s no way to correct it without either inheriting from QGraphicsView and writing a new rubber band selection system or hacking the Qt source. I chose to fix the problem by doing the latter. Fortunately this turned out to be easier than I originally thought it would be…

(19 Jan 2015): I submitted a patch for this which has been merged into the Qt codebase. This fix will be part of Qt 5.5.

Continue reading