Results 1 to 3 of 3

Thread: Connect to global function?

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Connect to global function?

    So, I make this connection of a timer:
    main.cpp:
    Qt Code:
    1. connect(this, SIGNAL(timeout()), Global, SLOT(connected_to_internet()));
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. class Global{
    2. public:
    3. static bool connected_to_internet(){
    4. QProcess *connected = new QProcess(0);
    5. QString exec="ping";
    6. QStringList params;
    7. params << "-c" << "1" << "www.google.com";
    8. connected->start(exec,params);
    9. if(!connected->waitForFinished())
    10. return false;
    11. int exitcode=connected->exitCode();
    12. delete connected;
    13. if(!exitcode){
    14. qDebug() << "CONNECTED!\n";
    15. return true;
    16. }
    17. else{
    18. qDebug() << "NOT CONNECTED!\n";
    19. return false;
    20. }
    21. }
    22. };
    To copy to clipboard, switch view to plain text mode 

    Do I do anything wrong here?
    The compiler complains about the connection I do that:
    Qt Code:
    1. /home/alex/Workstation/test-build-desktop/../test-1.0/main.cpp:263: error: expected primary-expression before ‘,’ token
    To copy to clipboard, switch view to plain text mode 
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Connect to global function?

    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):
    Qt Code:
    1. #include "global.h"
    2.  
    3. class MainWindow : public QSomething{
    4. Q_OBJECT
    5. /*...*/
    6. protected slots:
    7. void checkConnection(){
    8. bool check = Global::connected_to_internet();
    9. doSomething(check);
    10. }
    11. };
    To copy to clipboard, switch view to plain text mode 
    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.

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

    hakermania (3rd September 2011)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Connect to global function?

    Quote Originally Posted by stampede View Post
    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):
    Qt Code:
    1. #include "global.h"
    2.  
    3. class MainWindow : public QSomething{
    4. Q_OBJECT
    5. /*...*/
    6. protected slots:
    7. void checkConnection(){
    8. bool check = Global::connected_to_internet();
    9. doSomething(check);
    10. }
    11. };
    To copy to clipboard, switch view to plain text mode 
    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.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

Similar Threads

  1. Replies: 4
    Last Post: 14th August 2011, 16:37
  2. QtConcurrentRun and global function
    By hakermania in forum Newbie
    Replies: 2
    Last Post: 16th July 2011, 11:05
  3. Slot function not being executed in connect
    By Leirbag89 in forum Newbie
    Replies: 7
    Last Post: 19th May 2011, 20:53
  4. Connect slot to an int function !?
    By hakermania in forum Newbie
    Replies: 23
    Last Post: 30th December 2010, 13:08
  5. connect a QPushButton matrix with a function
    By harmodrew in forum Newbie
    Replies: 6
    Last Post: 6th August 2010, 12:11

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.