PDA

View Full Version : Connect to global function?



hakermania
3rd September 2011, 15:59
So, I make this connection of a timer:
main.cpp:

connect(this, SIGNAL(timeout()), Global, SLOT(connected_to_internet()));
which is done inside the class of itself (that's why the 'this' in the 1st argument).

Now, I want to connect this timer with a static bool function called connected_to_internet() declared in a header file like this:
glob.h:


class Global{
public:
static bool connected_to_internet(){
QProcess *connected = new QProcess(0);
QString exec="ping";
QStringList params;
params << "-c" << "1" << "www.google.com";
connected->start(exec,params);
if(!connected->waitForFinished())
return false;
int exitcode=connected->exitCode();
delete connected;
if(!exitcode){
qDebug() << "CONNECTED!\n";
return true;
}
else{
qDebug() << "NOT CONNECTED!\n";
return false;
}
}
};


Do I do anything wrong here?
The compiler complains about the connection I do that:


/home/alex/Workstation/test-build-desktop/../test-1.0/main.cpp:263: error: expected primary-expression before ‘,’ token

stampede
3rd September 2011, 16:09
SLOT must be a non-static member of a class, which have the Q_OBJECT macro in private section. You haven't satisfied any of those two requirements.

which is done inside the class of itself (that's why the 'this' in the 1st argument).
This is wrong, "timeout()" signal belongs to QTimer object, so first parameter must be a pointer to QTimer object.
You can solve this easily by introducing a SLOT in MainWindow class (or whatever other "main" class):


#include "global.h"

class MainWindow : public QSomething{
Q_OBJECT
/*...*/
protected slots:
void checkConnection(){
bool check = Global::connected_to_internet();
doSomething(check);
}
};

Then simply connect the timer to this slot.
Your connect statement is wrong, you cant connect to "class", you need to have an instance to connect to.

hakermania
3rd September 2011, 16:36
SLOT must be a non-static member of a class, which have the Q_OBJECT macro in private section. You haven't satisfied any of those two requirements.

This is wrong, "timeout()" signal belongs to QTimer object, so first parameter must be a pointer to QTimer object.
You can solve this easily by introducing a SLOT in MainWindow class (or whatever other "main" class):


#include "global.h"

class MainWindow : public QSomething{
Q_OBJECT
/*...*/
protected slots:
void checkConnection(){
bool check = Global::connected_to_internet();
doSomething(check);
}
};

Then simply connect the timer to this slot.
Your connect statement is wrong, you cant connect to "class", you need to have an instance to connect to.

Thanks, it would be nice if connect() was more flexible, though.