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. How to pass method pointer as parameter for connecting signals&slots?
Forum Updated to NodeBB v4.3 + New Features

How to pass method pointer as parameter for connecting signals&slots?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 3.9k 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by kitfox
    #1

    I've written a method in which I would like to connect a signal to a slot. However, I want the slot to be passed in as a parameter. I'm having trouble figuring out what data type I should use for this.

    How would I replace the ???? in the below?

    class Connector : public QObject
    {
        Q_OBJECT
        
        QString _name;
        
    public:
        void connectMe(QObject* slotObj, ???? slotMethod);
        
    public signals:
        void nameChanged();
    }
    
    void Connector::connectMe(QObject* slotObj, ???? slotMethod)
    {
        connect(this, &Connector::nameChanged, slotObj, slotMethod);
    }
    
    
    void MyOtherObject::listenForNameChanges()
    {
        //Do something
    }
    
    void MyOtherObject::doConnect()
    {
        Connector con;
        con.connectMe(this, &MyOtherObject::listenForNameChanges);
    }
    
    Taz742T 1 Reply Last reply
    0
    • K kitfox

      I've written a method in which I would like to connect a signal to a slot. However, I want the slot to be passed in as a parameter. I'm having trouble figuring out what data type I should use for this.

      How would I replace the ???? in the below?

      class Connector : public QObject
      {
          Q_OBJECT
          
          QString _name;
          
      public:
          void connectMe(QObject* slotObj, ???? slotMethod);
          
      public signals:
          void nameChanged();
      }
      
      void Connector::connectMe(QObject* slotObj, ???? slotMethod)
      {
          connect(this, &Connector::nameChanged, slotObj, slotMethod);
      }
      
      
      void MyOtherObject::listenForNameChanges()
      {
          //Do something
      }
      
      void MyOtherObject::doConnect()
      {
          Connector con;
          con.connectMe(this, &MyOtherObject::listenForNameChanges);
      }
      
      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #2

      @kitfox
      https://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c

      Do what you want.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3

        Maybe this will help: https://www.cprogramming.com/tutorial/function-pointers.html?

        On the other hand, why do you want to make it that way? In the calling code you already must know the signal's object, the signal, the slot's object and the slot, so why not just call connect() there?

        1 Reply Last reply
        1
        • K Offline
          K Offline
          kitfox
          wrote on last edited by
          #4

          I want to tidy up the code I'm using to initialize a QAction. Rather than create a new QAction object and then call about 5 different methods on it to initialize it, I'd like to have a simpler method like

          createAction(id, name, shortcut, tooltip, slotObj, slotMethod);

          kshegunovK 1 Reply Last reply
          0
          • K kitfox

            I want to tidy up the code I'm using to initialize a QAction. Rather than create a new QAction object and then call about 5 different methods on it to initialize it, I'd like to have a simpler method like

            createAction(id, name, shortcut, tooltip, slotObj, slotMethod);

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            That's a rather odd way of going about it, but it can be done, if you really, really want to (which I'm not convinced you do) ...
            Here's a short snippet:

            template <typename Slot>
            QMetaObject::Connection Connector::connectMe(const typename QtPrivate::FunctionPointer<Slot>::Object *slotObj, Slot slotMethod)
            {
                return QObject::connect(this, &Connector::nameChanged, slotObj, slotMethod);
            }
            

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1

            • Login

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