[Interest] How valid is using pure virtual functions in place of signals in base classes?
NIkolai Marchenko
enmarantispam at gmail.com
Tue Jan 13 02:52:10 CET 2026
The problem: qt doesn't really allow us to use multiple inheritance, but
sometimes it's very desirable to split some logic into a base class, so I
had an idea: why not declare "signals" before you mark them as signal?
That way base class could directly call pure virtuals and the QObject based
subclass will have the "signals:" section that will force moc to
implement these pure virtuals and call the signal magic to actually work.
That way we both have a base class that emits and can inherit however many
bases we'd like. I've verified that the pattern works for a simple test
case but then I went digging on the internet and while it is mentioned a
few times there'sd a lot of confusion on whether it can work at all(clearly
people who say it's just completely not working are mistaken as evidenced
from my testing) and whether it is problematic.
Personally, I've delved a bit into the source code of how signal
dispatching works in such cases, but didn't see anything that could
obviously break - we're just figuring out the signal index in the connect()
statement and call that later. But I am also not qualified enough template
metaprogrammer to say for sure.
An example of someone recommending this pattern (and a code example) but it
is both too simple and has no discussion on it for me to be sure it is
allowed to be used:
https://www.man42.net/blog/2012/05/qt-4-qobject-pure-virtual-signal/
So my question is - is it safe, and is it safe both for qt5 and qt6? Will
something in qt potentially break in non obvious way if this pattern is
used?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20260113/e6c0aab8/attachment.htm>
More information about the Interest
mailing list