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

QSignalMapper Example

Qt is a great development framework. I’ve been using it for years and I’m still discovering new & improved ways to do things. For example, I had a rather clunky bit of code to put menu items in the help menu which would open up various pages on my website.

myMainWindow.h

myMainWindow.cpp

Wow. That’s chunky code. Any additional menu items required a declaration of another one line function which of course meant modifying the header and the cpp file. Messy.

Then I discovered QSignalMapper which let me tie a QString to a given QAction so I would only need one slot to handle any URL opening.

Continue reading