Results 1 to 3 of 3

Thread: connect several signal and slots in one function?

  1. #1
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Smile connect several signal and slots in one function?

    hello, suppose i hava class A and B with signal123 and slot123,
    I want to connect the A's signals to B's slots in just one function, because it is often used, and it is boring to write 3 connects.
    my program wont compile.

    is there a correct way to do this?

    thank you.

    example class A and B:

    class A : public QObject
    {
    Q_OBJECT
    public:
    explicit A(QObject *parent = nullptr):QObject(parent){}

    template<typename slot1, typename slot2, typename slot3>
    void setSlot(QObject *recv, slot1 f1, slot2 f2, slot3 f3) {
    connect(this,&A::signal1,recv,f1);
    connect(this,&A::signal2,recv,f2);
    connect(this,&A::signal3,recv,f3);
    }

    signals:
    void signal1();
    void signal2();
    void signal3();
    };

    class B : public QObject
    {
    Q_OBJECT
    public:
    explicit B(QObject *parent = nullptr):QObject(parent){}

    public slots:
    void slot1() {printf("slot1");}
    void slot2() {printf("slot2");}
    void slot3() {printf("slot3");}
    };

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: connect several signal and slots in one function?

    Qt Code:
    1. class A : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit A(QObject *parent = nullptr):QObject(parent){}
    6.  
    7. template<typename T>
    8. void setSlot(T *recv, void (T::*f1)(), void (T::*f2)(), void (T::*f3)())
    9. {
    10. connect(this,&A::signal1,recv,f1);
    11. connect(this,&A::signal2,recv,f2);
    12. connect(this,&A::signal3,recv,f3);
    13. }
    14.  
    15. signals:
    16. void signal1();
    17. void signal2();
    18. void signal3();
    19. };
    20.  
    21. class B : public QObject
    22. {
    23. Q_OBJECT
    24. public:
    25. explicit B(QObject *parent = nullptr):QObject(parent){}
    26.  
    27. public slots:
    28. void slot1() {printf("slot1");}
    29. void slot2() {printf("slot2");}
    30. void slot3() {printf("slot3");}
    31. };
    32.  
    33. int main()
    34. {
    35. A a;
    36. B b;
    37.  
    38. a.setSlot(&b, &B::slot1, &B::slot2, &B::slot3);
    39.  
    40. a.signal1();
    41. a.signal2();
    42. a.signal3();
    43. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    cosmos (18th October 2017)

  4. #3
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: connect several signal and slots in one function?

    cool. it works. thank you!

Similar Threads

  1. Connect two signal to function
    By updaterr in forum Qt Quick
    Replies: 1
    Last Post: 17th June 2014, 09:35
  2. Replies: 2
    Last Post: 18th April 2013, 12:15
  3. Replies: 13
    Last Post: 30th November 2010, 12:47
  4. Replies: 16
    Last Post: 16th February 2010, 13:17
  5. function 'connect' always look super class for slots.
    By Intaek Lim in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2007, 13:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.