Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. senderSignalIndex() not accessible
Forum Updated to NodeBB v4.3 + New Features

senderSignalIndex() not accessible

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 316 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Redman
    wrote on last edited by
    #1
    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

    1 Reply Last reply
    0
    • JonBJ JonB

      @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.

      R Offline
      R Offline
      Redman
      wrote on last edited by
      #8

      @JonB Simply calling
      senderSignalIndex() was sufficient to get the correct signalIndex.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        As the error message says, you are trying to access a protected member of class outside of it.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • R Offline
          R Offline
          Redman
          wrote on last edited by
          #3

          But technically im in QObject, since B inherits from it

          JonBJ 2 Replies Last reply
          0
          • R Redman

            But technically im in QObject, since B inherits from it

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #4
            This post is deleted!
            1 Reply Last reply
            0
            • R Redman

              But technically im in QObject, since B inherits from it

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #5

              @Redman
              But you are trying to access protected members via sender()->.... You can only access protected members directly inside the derived class (i.e. on this), not on some other object. You could do it inside classes A or IA, but not from B. Either do the work inside A/IA instead, or have them export something public which calls senderSignalIndex() for you.

              R 1 Reply Last reply
              2
              • JonBJ JonB

                @Redman
                But you are trying to access protected members via sender()->.... You can only access protected members directly inside the derived class (i.e. on this), not on some other object. You could do it inside classes A or IA, but not from B. Either do the work inside A/IA instead, or have them export something public which calls senderSignalIndex() for you.

                R Offline
                R Offline
                Redman
                wrote on last edited by
                #6

                @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.

                JonBJ 1 Reply Last reply
                0
                • R Redman

                  @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.

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #7

                  @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.

                  R 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @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.

                    R Offline
                    R Offline
                    Redman
                    wrote on last edited by
                    #8

                    @JonB Simply calling
                    senderSignalIndex() was sufficient to get the correct signalIndex.

                    1 Reply Last reply
                    0
                    • R Redman has marked this topic as solved on
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #9

                      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.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved