in Code

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.

This allowed me to simplify:

myMainWindow.h

myMainWindow.cpp

Much cleaner code and it’s easier to maintain and modify. I’ve since found another couple of places in my code that benefit from QSignalMapper since it handles more than QString. It also lets you associate ints, QWidgets, and QObjects to a QAction.

Hope you found it useful. Please let me know!

Write a Comment

Comment