Unsolved "New" signal slot syntax with signals defined in abstract base class requires horrible cast?
-
Hi.
I thought I'd eliminate theSIGNAL/SLOT
macros from my code until I found out that I needed to do horrible casts like this:connect(app_->data_manager(), static_cast<void (DataManager::*)(const Data&)>(&DataManager::CurrentDataChanged), &DataBase::CurrentDataChanged);
because the signal
CurrentDataChanged
is defined in the abstract base class ofDataManager
.Is this really the correct way to do this with the new syntax? Once I realized this, I just quickly reverted to the
SIGNAL/SLOT
macros, because the new syntax just seems too much of a hassle. -
@Diracsbracket
Well, that depends on exactly how you have defined the signatures of the signal and the slot. Qt has plenty of signals/slots defined in its abstract base classes without requiring the casting you show.It's up to you, but personally at least I still miles prefer new style with edit- and compile-time help, as opposed to the old macros which if you get wrong you only find out at runtime. You can always
typedef
the signatures out of the classes to use elsewhere, so that even if that code is needed you don't have to write it every time. -
@Diracsbracket said in "New" signal slot syntax with signals defined in abstract base class requires horrible cast?:
because the signal
CurrentDataChanged
is defined in the abstract base class ofDataManager
.That doesn't sound like a reason for casting.
- What error message do you get without the cast?
- Is
CurrentDataChanged
overloaded or overridden?
As @JonB mentioned, the signals of QAbstractItemView (https://doc.qt.io/qt-5/qabstractitemview.html#signals ) don't require casting, for example.