View Full Version : Connecting SIGNAL to functions

5th November 2014, 17:40
Is there any way to connect SIGNAL to a function which have global scope? Like when button is clicked a function display_error() is evoked? Is it possible or I would need to make a custom class for the widget inheriting from QPushButton adding display_error function in it?
I want to ignore the Object Oriented design as I am more of a C guy.

5th November 2014, 18:26
It is possible in Qt5. In Qt4 you'd have to create a wrapper class to make the call to the function.

5th November 2014, 18:36
A custom class inheriting from QObject who have display_error() function as SLOT would work? like this example:

#include <QObject>

class Counter : public QObject

Counter() { m_value = 0; }

int value() const { return m_value; }

public slots:
void setValue(int value);
void display_error();

void valueChanged(int newValue);

int m_value;

5th November 2014, 20:41
Yes, but I rather meant that the class accepts a function pointer as its constructor's argument and executes the function when it receives a signal. Then you can attach any global function to it.