[Interest] subclassed QTimer

André Somers andre at familiesomers.nl
Thu Nov 12 18:21:24 CET 2015



> Op 12 nov. 2015 om 17:40 heeft René J. V. Bertin <rjvbertin at gmail.com> het volgende geschreven:
> 
> René J.V. Bertin wrote:
> 
> Hi,
> 
> Sorry for forgetting completely about this thread, my bad. It turned out that I 
> hadn't completely understood the editor class. Rather than having a single 
> instance of that class and then a list of open documents, there is actually a 
> list of instances of that editor class. That means I don't need to maintain my 
> own class of timers, and also that I could simply add a timer slot to the editor 
> class, because each editor instance needs to poll the state of only a single 
> document.
> 
> Now, to react to the suggestions:
>> 
>> I'm missing the Q_OBJECT macro
> 
> That would only be required if QTimer doesn't already have one (it does), right?
Wrong!

You need it in any subclass that needs anything "meta", including defining signals or slots. It actually expands to useful code, but it also acts as the trigger for moc. Why else would you ever need it: QObject itself has it after all...
> 
>> Why are you subclassing QTimer and not just use it  as it is ??
> 
> See above, I needed to add a timeout slot somewhere, thought I best define a new 
> class for that. The reason I subclassed QTimer rather than defining a class with 
> a QTimer member variable? It seemed more elegant, and also that it should work.
In general: prefer composition over inheritance. 
> 
> And I'm still curious as to why it didn't work, no matter how weird it might 
> seem.
See above: the Q_OBJECT macro. 

André


More information about the Interest mailing list