[Development] Virtual hook in qobject?

Volker Hilsheimer volker.hilsheimer at qt.io
Fri Sep 11 13:44:44 CEST 2020



> On 11 Sep 2020, at 13:34, Allan Sandfeld Jensen <kde at carewolf.com> wrote:
> 
> On Freitag, 11. September 2020 12:44:09 CEST Volker Hilsheimer wrote:
>>> On 11 Sep 2020, at 12:37, Allan Sandfeld Jensen <kde at carewolf.com> wrote:
>>> 
>>> Should we add a generic virtual hook in qobject, so any qobject can expand
>>> 
> their with effectively virtual functions in Qt6?
>>> 
>>> I imagine something like QVariant QObject::virtual_hook(int id, QVariant 
>>> args);
>>> 
>>> Or do we already have other useful work-arounds for this common problem?
>>> 
>>> Best regards
>>> ‘Allan
>> 
>> 
>> We have that already. It’s called qt_metacall :)
>> 
> Right :) but that typically gets generated by moc, but I see what you mean. 
> You can manually implement it, or get a similar effect through careful use of 
> INVOKABLE and an indirection.
> 
> And the two places I was thinking about cleaning up also turned out to be non-
> QObject.
> 
> Best regards
> Allan

Yes, the idea is not to implement your own qt_metacall, but that you can make methods that you would like to be virtual into slots and then call them via QMetaObject::invoke. You can then “override" that slot in subclasses (ie declare an identical member, also as a slot).

Not going to be lightning fast, but neither is going to be a dispatch through a virtual_hook and QVariant.

Cheers,
Volker



More information about the Development mailing list