Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to protected slot in derived class

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);
                                                          ^
like image 890
ManuelSchneid3r Avatar asked May 02 '15 11:05

ManuelSchneid3r


People also ask

How do you access protected members in a derived class?

The protected members are inherited by the child classes and can access them as its own members. But we can't access these members using the reference of the parent class. We can access protected members only by using child class reference.

Can derived classes be access protected?

A derived class cannot access protected members of its base class through an instance of the base class.

How are protected members of a base class accessed in the derived class when inherited?

If a class is derived privately from a base class, all protected base class members become private members of the derived class. Class A contains one protected data member, an integer i . Because B derives from A , the members of B have access to the protected member of A .

Can slot be virtual?

Yes, just like regular c++ pure virtual methods. The code generated by MOC does call the pure virtual slots, but that's ok since the base class can't be instantiated anyway... Again, just like regular c++ pure virtual methods, the class cannot be instantiated until the methods are given an implementation.


1 Answers

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);
like image 142
cmannett85 Avatar answered Sep 27 '22 15:09

cmannett85