senderSignalIndex() not accessible
-
class IA : public QObject { Q_OBJECT . . . }; class A : public IA { Q_OBJECT . . . }; class IB : public QObject { Q_OBJECT . . . }; class B : public IB { Q_OBJECT . . . }; B::slotForSignalFromA() { sender()->senderSignalIndex() <-- C2248 qobject_cast<A*>(sender())->senderSignalIndex() <-- C2248 qobject_cast<IA*>(sender())->senderSignalIndex() <-- C2248 }
error: C2248: 'QObject::senderSignalIndex': cannot access protected member declared in class 'QObject'
What am I missing
-
@Redman
Well, I only know about correcting you on the C++ issue you asked about. I know nothing about the method. I note the docs include:Returns the meta-method index of the signal that called the currently executing slot, which is a member of the class returned by sender(). If called outside of a slot activated by a signal, -1 is returned.
Warning: The return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object's thread. Do not use this function in this type of scenario.
I don't know whether reading https://stackoverflow.com/questions/27476554/how-to-find-out-from-the-slot-which-signal-has-called-this-slot might be relevant to you.
-
Hi,
As the error message says, you are trying to access a protected member of class outside of it.
-
@Redman
But you are trying to accessprotected
members viasender()->...
. You can only accessprotected
members directly inside the derived class (i.e. onthis
), not on some other object. You could do it inside classesA
orIA
, but not fromB
. Either do the work insideA
/IA
instead, or have them export somethingpublic
which callssenderSignalIndex()
for you. -
@Redman
But you are trying to accessprotected
members viasender()->...
. You can only accessprotected
members directly inside the derived class (i.e. onthis
), not on some other object. You could do it inside classesA
orIA
, but not fromB
. Either do the work insideA
/IA
instead, or have them export somethingpublic
which callssenderSignalIndex()
for you.@JonB I see. I updated my class to this now:
class IA : public QObject { Q_OBJECT . . . qint32 signalIndex() const { return senderSignalIndex(); } };
and then I try to access it like this:
IA* a = qobject_cast<IA*>(sender()); qint32 signal = a->signalIndex();
Which always returns -1.
-
@JonB I see. I updated my class to this now:
class IA : public QObject { Q_OBJECT . . . qint32 signalIndex() const { return senderSignalIndex(); } };
and then I try to access it like this:
IA* a = qobject_cast<IA*>(sender()); qint32 signal = a->signalIndex();
Which always returns -1.
@Redman
Well, I only know about correcting you on the C++ issue you asked about. I know nothing about the method. I note the docs include:Returns the meta-method index of the signal that called the currently executing slot, which is a member of the class returned by sender(). If called outside of a slot activated by a signal, -1 is returned.
Warning: The return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object's thread. Do not use this function in this type of scenario.
I don't know whether reading https://stackoverflow.com/questions/27476554/how-to-find-out-from-the-slot-which-signal-has-called-this-slot might be relevant to you.
-
@Redman
Well, I only know about correcting you on the C++ issue you asked about. I know nothing about the method. I note the docs include:Returns the meta-method index of the signal that called the currently executing slot, which is a member of the class returned by sender(). If called outside of a slot activated by a signal, -1 is returned.
Warning: The return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object's thread. Do not use this function in this type of scenario.
I don't know whether reading https://stackoverflow.com/questions/27476554/how-to-find-out-from-the-slot-which-signal-has-called-this-slot might be relevant to you.
-
-
note that
sender()
,senderSignalIndex()
are quite expensive calls (they involves a mutex) and can most of the time be refactoring to connect to a lambda capturing the calling context.