QSignalMapper Example Revisited

Qt Library

Qt Library

Several years ago I wrote a short piece on QSignalMapper to give a quick example of how it can be used. Since then, Qt5 has been released and C++11 is now A Thing.

One of the cool things introduced in Qt5 is a new overload for the QObject::connect() method:

This means we can use function pointers instead of the old (char *) based method. One of the big advantages of using the new method is that it can check signal/slot connections at compile-time instead of only at run-time. If the signature of either the sender or receiver changes the code will fail to compile. (If you are using C++11 and like to write “clever” code, you can now use lambda expressions as well!) There’s a decent overview of the new method in this blog post by Olivier Goffart.

I recently started updating my aging codebase to Qt5 and C++11 and one of the things I’m changing is all my connections to use the new format. I ran into an issue almost immediately that I thought I’d document in case I can save someone some time.

Continue reading

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…

Creating An Interactive Map With Leaflet and OpenStreetMap

I’ve known for a while that the interactive map of the world showing some of my bloodstain pattern analysis software customers was kind of slow. I also knew that I was using a very outdated version of Google’s API for displaying the data. Yesterday I decided to take a look at it to see what I needed to do to fix it up.

Old Google Map

Old Google Map

My first stop was the Google page about moving to v3 of the API. Apparently I needed to get a new API key by signing up for something with my Google Account. Google has added usage limits and now tracks absolutely everything (yes, they probably did before, but now it’s more explicit). I also needed to consider whether or not I can use the maps on a commercial site.

I don’t like hurdles, and I like simplicity, so I decided to look around for alternatives. I remembered looking at OpenStreetMap (OSM) several years ago when I originally built my map and deciding it wasn’t quite good enough. I decided to check it out again to see how they were doing. My how things change!

OpenStreetMap

OpenStreetMap

Because I’m a good lazy developer (by that I mean I avoid writing things when they already exist, are well-coded, and maintainable), my first thought was to grab a WordPress plugin and to use it for my map. I tried out the OSM – OpenStreetMap plugin. While it sort-of worked for what I was trying to do, it was not as customizable as I’d like, it seemed too heavy for one map on one site, and it tied me to WordPress (which my other site hasn’t moved to yet).

My next thought was to look for a JavaScript library with a nice API to integrate and handle most of the heavy lifting. Poking around the OSM site, I ran across OpenLayers. It looked pretty powerful, and has a ton of stuff in its API. I downloaded it (10M?) and took a look but I really just wanted something simpler. Then I found Leaflet. Exactly what I needed. Small, simple, great documentation, and some really straightforward examples.

Leaflet

Leaflet

Even though I had to brush up on some JavaScript, jQuery, and JSON concepts—things I rarely use— I ended up researching possibilities and replacing my Google Map with an OpenStreetMap/Leaflet version in just a couple of hours. It’s now much faster and simpler to customize.

New OpenStreetMap/Leaflet Map

New OpenStreetMap/Leaflet Map

In this article, I’ll step through an example that shows how to:

  • set up a simple map using the Leaflet JavaScript library
  • load marker locations from a JSON file
  • change the marker icon
  • have the markers show some data when clicked

Continue reading

Converting Between cv::Mat and QImage or QPixmap

In a previous article, I outlined how to compile OpenCV for Mac OS X and to build and run a small example. In that post I used OpenCV‘s High-Level GUI (highgui) module to display a simple interface using the Qt framework. This is great if you’re writing a one-off program or are just experimenting with OpenCV, but how do you interface with your own Qt-based project if you don’t want to (or can’t) use the highgui module?

OpenCV Library

OpenCV Library

+

Qt Library

Qt Library

The first hurdle is converting between data formats so you can work with images in both OpenCV and Qt. So how do you move an image’s data between a cv::Mat and a QImage or QPixmap so you can display it in a widget?

In this article, I present some functions to handle these conversions and explain how to use them.

Continue reading

Compiling OpenCV On Mac OS X 10.6

A couple of years ago I needed to do some basic image processing and found OpenCV. OpenCV is a BSD-licensed library for digital image processing which implements several hundred computer vision algorithms. Unfortunately compiling it on the Mac was not straightforward—requiring Fink or MacPorts—and the one existing Mac framework was out of date and no longer maintained.

OpenCV Library

OpenCV Library

Fast forward to last week. I had another requirement for some image processing magic and I thought I’d check out OpenCV again. What a difference! It’s now really easy to compile and use on the Mac. I didn’t find a writeup online on how to compile on the Mac—the one on the OpenCV site is out of date—so I thought I’d write up a short summary for future reference and any Googlers out there in internet land.

Here’s how I did it…

Continue reading

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

Point Cloud Library on Mac OS X: A Build Diary

I’ve been using and contributing to various open source projects for almost 20 years. I love the fact that we can leverage each other’s work and help other developers save time instead of reinventing the wheel or fighting the same problem a thousand times over. Open source tools and libraries make what I do possible.  Whenever I use open source, I like to compile from the source so I can contribute back to the projects – even if only little patches for fixing compilation warnings, clarifying error messages, or adding to documentation.  I used to enjoy configuration and build challenges – such as tweaking and compiling my own Linux kernels – but that’s behind me now.  I just want things to work.

I’m a solo developer working on a commercial cross-platform Mac OS X/Windows application.  This means that I have to be careful how I allocate my development time since I also have research to conduct and a business to run.  Part of this is maintaining control of my development “ecosystem”.  The more moving pieces there are – tools, 3rd party libs, etc. – the more overhead, and the less time I have for actual development.  Keeping it simple means saving myself a lot of time.  It also makes it quick and easy to bring short-term contractors on board.

I started to investigate open source tools for processing point clouds for a future version of my software – I want to handle scan data from FARO and Leica scanners.  I quickly zeroed in on the Point Cloud Library which is a BSD-licensed library to read, visualize, manipulate, and process point clouds. It looks like exactly what I need and includes a bunch of capabilities I don’t understand in the least, but seem like they might be useful in the future as I learn about it. Most of the capabilities are too advanced for me to contribute much to them directly, but I thought I’d be able to contribute “around the edges”, so I wanted to build it from source.

Point Cloud Library

Point Cloud Library

This article is a blow-by-blow account of the steps I took while trying to build the Point Cloud Library (PCL) on Mac OS X 10.6 starting with the information from PCL’s Compiling from Source page. The first thing I tried to do is build and link PCL with only the mandatory dependencies. After that, I’ll want to look at the optional pieces and build them if I need them.

As I was working through it, I put together notes for myself on what I did in case I had to do it again or have a contractor do it.  As with most other things I have on this site, I thought at least one other person might benefit through the power of Google, so I decided to put the notes up here verbatim.  Knowing what I know now, I could shorten this dramatically into “Steps to Build PCL on Mac OS X” [even though I haven’t been successful yet], but I decided it might be interesting to show the action-packed sequences of what actually happened…

I hope this is taken in the spirit it’s intended – documentation of some of the problems just getting PCL to configure and build, along with what I hope are some constructive recommendations [at the end of this voluminous tome]. [Warning: I get a little rant-like in the last commentary section – it’s not directed specifically at the PCL, but software development projects in general.]

I have to point out that the PCL guys have been great, answering my initial question quickly, and they’ve been encouraging [so far – hope they still are after this!]. This doesn’t always happen in open source projects, so it’s great to see. They deserve a lot of credit for their work, which is of great value to many  developers and companies worldwide.  All the developers and supporting companies are super-awesome for making this library available as open source. Very much appreciated!

[If you want to skip the gruesome details – don’t forget they’re action-packed! – I list some bugs and recommendations near the end.]

So… here we go!

Continue reading