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.
I’ve been using Qt for several years now in a couple different projects. Only today did I learn about QMetaObject::connectSlotsByName.
Qt Slot Derived Class C
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!