Results 1 to 2 of 2

Thread: Get signal sender

  1. #1
    Join Date
    Oct 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Get signal sender

    Hi,

    I use PureBasic, which uses Qt for its GUI implementation on Linux. While PureBasic provides access to a measure of the Qt API, it doesn't give everything I need, so I'm developing a shared library I can use with my program to make use of more of what it offers.

    I connect signals to the PureBasic 'gadgets' like this:
    Qt Code:
    1. qApp->connect(header, &QHeaderView::sectionClicked, header, &ListIconHeaderClicked)
    To copy to clipboard, switch view to plain text mode 
    In this example, I am trying to detect when the header of PureBasic's ListIconGadget, which uses QTreeWidget, is clicked.

    I'm not sure how I could use classes in this situation, since I know you can use QObject::sender() to get the signal sender. Is there a way I can find out which widget send the signal in the ListIconHeaderClicked(int logicalIndex) function?

    Thank you.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get signal sender

    qApp->connect(header, &QHeaderView::sectionClicked, header, &ListIconHeaderClicked)
    This is incorrect syntax for a connect() statement. The first half is OK - you are saying you want to connect to an instance of the QHeaderView class ("header") and its sectionClicked() signal. The second half is wrong for two reasons: first, if you want to handle that signal in some other class that you have implemented, then you put the instance pointer for that class as the third argument (not "header"); and second, the next argument must be the address of a method for the class you used as the third argument:

    Qt Code:
    1. connect( header, &QHeaderView::sectionClicked, myClass, &MyClass::onSectionClicked ); // or whatever you call your class and slot
    To copy to clipboard, switch view to plain text mode 

    Calling connect() through a qApp pointer is not necessary unless you are doing it from some code that is not in a class derived from QObject. QObject implements the connect() methods and all derived classes inherit them. Your slot definition should look like this:

    Qt Code:
    1. class MyClass : public SomeClassDerivedFromQObject
    2. {
    3. Q_OBJECT
    4.  
    5. // constructor, destructor, etc.
    6.  
    7. public slots:
    8. void onSectionClicked( int logicalIndex );
    9. };
    10.  
    11. void MyClass::onSectionClicked( int logicalIndex )
    12. {
    13.  
    14. // do whatever you want with the header and index.
    15. // If you need the pointer to the QHeaderView:
    16.  
    17. QHeaderView * pView = qobject_cast< QHeaderView * >( sender() );
    18. if ( pView != nullptr )
    19. {
    20. // Safe to use the pView pointer
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 5th November 2019 at 22:24.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Deleting the signal sender from a slot
    By Tulon in forum Qt Programming
    Replies: 6
    Last Post: 29th November 2016, 02:22
  2. Signal from multipal sender
    By suneel1310 in forum Qt Programming
    Replies: 7
    Last Post: 27th June 2013, 13:31
  3. Replies: 3
    Last Post: 20th April 2011, 16:42
  4. Replies: 0
    Last Post: 15th December 2010, 07:18
  5. Replies: 3
    Last Post: 30th July 2010, 17:34

Tags for this Thread

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.