Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt5: connect: How to use 'connect' in case the slot has less parameters than the signal

Tags:

c++

c++11

qt

qt5

Task is to connect from one signal with one parameter to a slot with zero parameters. With the "old" Qt4-way of connect it works like this

connect(object1, SIGNAL(signal(int param)), object2, SLOT(slot()))

But what if I want to use the type-safe Qt5-connects?

The documentation just mentions the cases with:

  • connect to default parameters in slot

  • and using the above-mentioned way of the stringbased connect.

like image 712
Marcel Petrick Avatar asked Oct 20 '25 02:10

Marcel Petrick


1 Answers

The Qt5-style connect mechanism ignores extra-arguments of the signal just like you could specify in the Qt4-style:

The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) Since the signatures are compatible, the compiler can help us detect type mismatches when using the function pointer-based syntax. The string-based SIGNAL and SLOT syntax will detect type mismatches at runtime.

From the Qt Docs (emphasis mine).

So you can use the new style like this:

connect(&object1, &ClassObject1::signal, &object2, &ClassObject2::slot);
like image 63
bunto1 Avatar answered Oct 21 '25 17:10

bunto1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!