- Qt Signal Slot With Parameter Free
- Qt Signal Slot Lambda Parameter
- Qt Signal Slot With Parameter Number
- Qt Signal Slot Parameter
- Qt Signal Slot Default Parameter
- Qt Signal Slot With Different Parameters
I have an old codebase I started writing using the Qt 3.x framework—a little while before Qt4 was released. It’s still alive! I still work on it, keeping up-to-date with Qt and C++ as much as possible, and I still ship the product. Over the years I have moved the codebase along through Qt4 to Qt5, and on to compilers supporting C++11. One of the things I’ve sometimes found a little excessive is declaring slots for things that are super short and won’t be reused.
Here’s a simplified example from my older code that changes the title when a project is “dirty” (needs to be saved):
Qt is well known for its signals and slots mechanism. But how does it work? In this blog post, we will explore the internals of QObject and QMetaObject and discover how signals and slot work under the hood. In this blog article, I show portions of Qt5 code, sometimes edited for formatting and brevity. Jun 03, 2008 I'm making a Sudoku (solving/generating) program in Qt. Now, I'm just a beginner as a programmer, and I have no experience with Qt. I'm stuck in a strange situation. I want to connect a signal and slot with different parameters. My grid is made up of an array of QLineEdits, and here's how I'm making the connections.
2 4 6 8 10 12 14 16 18 20 22 | classmyMainWindow... privateslots: void_setTitle(constQString&inTitle ); // implementation connect(inProject,SIGNAL(signalDirtyChange(bool)), ... voidmyMainWindow::slotProjectDirtyChanged() QString name=mProject->name(); name+='*'; _setTitle(name); |
(Aside: If you are wondering about my naming convention, I developed a style when using Qt early on (c. 2000) where signals are named signalFoo() and slots are named slotFoo(). This way I know at a glance what the intent is. If I’m about to modify a slot function I might take an extra minute to look around since most IDEs can’t tell syntactically where it’s used in a
SLOT()
macro. In this case you have to search for it textually.)Thanks to C++11 lambdas and Qt’s ongoing evolution, these short slots can be replaced by a more succinct syntax. This avoids having to declare a method in your class declaration and shortens your implementation code. Both desirable goals!
Let’s take a look.
Getting rid of SIGNAL() and SLOT() macros
The first really nice thing about Qt5 is that we can move away from the classic
SIGNAL()
and SLOT()
macros to using method pointers.This does several things for us:
- relieves us of having to lookup and fill in the parameters for the methods
- shortens your code
- IDEs can identify the methods used in a connect call when you search for uses of a method
- allows implicit conversion of arguments
- catches problems at compile time rather than runtime
This last one is the most important. If the two methods you are trying to connect are mismatched it simply won’t compile. I can say with authority that problems found at compile time can save you many, many hours of debugging!
So if we apply this idea to our example, the
connect()
call becomes: