Qt Slot Derived Class
The connection mechanism uses a vector indexed by signals. But all the slots waste space in the vector and there are usually more slots than signals in an object. So from Qt 4.6, a new internal signal index which only includes the signal index is used. While developing with Qt, you only need to know about the absolute method index. And slots are functions/methods that can be associated with signals - so when a signal is received - a slot function can be called. Some classes have pre-defined slots, but you can also create your own slot methods. In order to connect a QT widgets signal to another classes slot method, you need to use QT's 'connect' function.
![Class Class](https://imgs.developpaper.com/imgs/2085020-20201107112658586-655435592.png)
I’ve been using Qt for several years now in a couple different projects. Only today did I learn about QMetaObject::connectSlotsByName.
![Derived Derived](https://slideplayer.com/slide/13084620/79/images/13/Faire+une+connexion+Le+slot+doit+avoir+autant+d+arguments+que+le+signal+et+ils+doivent+être+de+même+type..jpg)
Qt Slot Derived Class C
![Derived Derived](https://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/media/vectorart/qt_class_hierarchy.png)
As the documentation states, this function will recursively search the given object for signals matching the format of:
If you’re using Qt Designer and MOC’ing, this fuction gets called automatically in your ui_<mainwindow>.h
file inside of setupUi()
. This means that you don’t have to manually connect the objects in your .ui
files. I didn’t know this! You can simply do something like:
Qt Slot Derived Class Example
And that’s it! You don’t have to define the signals/slots in Designer, you don’t have to manually connect them (as I had been doing). Nothing! It just works!
Qt Slot Derived Classes
I had never seen this mentioned in any tutorial or even code examples. I only stumbled upon it when looking at another project for something completely unrelated. But this is a great time saver!