Connection to a secure slot in a derived class

Here's what the base class ads look like:

protected: void indexAll(); void cleanAll(); 

In a derived class, the following does not compile:

 indexAll(); // OK connect(&_timer, &QTimer::timeout, this, &FileIndex::indexAll); // ERROR connect(&_timer, SIGNAL(timeout()), this, SLOT(indexAll())); // OK 

I would like to use the first connect option, as it does some compile-time checks. Why does this return an error:

 error: 'void Files::FileIndex::indexAll()' is protected void FileIndex::indexAll() ^ [...].cpp:246:58: error: within this context connect(&_timer, &QTimer::timeout, this, &FileIndex::indexAll); ^ 
+6
source share
2 answers

The syntax of the "old" style works because the radiation from the signal passes through qt_static_metacall(..) , which is a member of FileIndex and therefore has secure access.

The syntax of the "new" style works, but for this reason it will not allow you to take the address directly from the method of the parent class. However, it takes the "inherited" address of indexAll() , so just change the code to:

 connect(&_timer, &QTimer::timeout, this, &Derived::indexAll); 
+7
source

The first is governed by the usual C ++ accessibility rules. The QTimer :: timeout signal calls the FileIndex :: indexAll file at the specified function pointer directly. Of course, this is possible only if the pointer to this function is publicly available (without taking into account the possible decisions of friends). If you use function pointers, you don’t even need to mark the function as SLOT in the header file.

The second is the magic of moka. Call through a system of metaobjects. I never delved deeper into this topic ... it just worked. :-)

Well, not the best explanation. If you want more information:

http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html

http://woboq.com/blog/how-qt-signals-slots-work.html

http://woboq.com/blog/how-qt-signals-slots-work-part2-qt5.html

He reads well, but ... only interest if you are interested in deeper development of Qt. IMHO is only necessary if you want to develop Qt yourself.

0
source

Source: https://habr.com/ru/post/986410/


All Articles