PDA

View Full Version : Get signal sender



wombats
31st October 2019, 10:11
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:

qApp->connect(header, &QHeaderView::sectionClicked, header, &ListIconHeaderClicked)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.

d_stranz
5th November 2019, 22:19
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:



connect( header, &QHeaderView::sectionClicked, myClass, &MyClass::onSectionClicked ); // or whatever you call your class and slot


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:



class MyClass : public SomeClassDerivedFromQObject
{
Q_OBJECT

// constructor, destructor, etc.

public slots:
void onSectionClicked( int logicalIndex );
};

void MyClass::onSectionClicked( int logicalIndex )
{

// do whatever you want with the header and index.
// If you need the pointer to the QHeaderView:

QHeaderView * pView = qobject_cast< QHeaderView * >( sender() );
if ( pView != nullptr )
{
// Safe to use the pView pointer
}
}