7

This is how the declarations in the base class look:

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

In the 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 variant of connect, since it does some compile time checks. Why is this returning the 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);
                                                          ^
ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103

2 Answers2

17

The 'old' style syntax works because signal emission runs through qt_static_metacall(..) which is a member of FileIndex and so has protected access.

The 'new' style syntax does work, but for this reason won't let you take the address directly of the parent class method. It will however take the 'inherited' address of indexAll(), so just change the code to:

connect(&_timer, &QTimer::timeout, this, &Derived::indexAll);
cmannett85
  • 21,725
  • 8
  • 76
  • 119
0

The first one is ruled by normal C++ accessibility rules. The QTimer::timeout signal call the FileIndex::indexAll over the provided function pointer directly. Of course, this is only possible when this function pointer is public (disregarding possible friend solutions). If you use function pointers you don't even need to mark the function as SLOT in the header file.

The second one is moc magic. Calls through the meta-object system. I never delved into this topic any deeper... it just worked. :-)

Ok, not the best explanation. If you want more info:

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

Good reads, but... only of interest if your are interested in the deeper workings of Qt. IMHO only necessary if you want to develop Qt itself.

Greenflow
  • 3,935
  • 2
  • 17
  • 28