Results 1 to 5 of 5

Thread: Use connect() to a Qt terminal application?

  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

    Smile Use connect() to a Qt terminal application?

    Qt Code:
    1. #include <stdlib.h>
    2. #include <QtCore/QTimer>
    3.  
    4. void check(){
    5. //perform a check every 10 secs
    6. }
    7.  
    8. int main()
    9. {
    10. QTimer *timer = new QTimer;
    11. connect(timer, SIGNAL(timeout()), this, SLOT(check()));
    12. timer->start(10000)
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Ok, this is the code.... i get error that connect() wasn't declared over here :/
    What Qt library should I include to use connect() function? Thank you!
    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
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use connect() to a Qt terminal application?

    I believe 'connect' is part of QObject, but I doubt it'll work without a QApplication.

  3. #3
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use connect() to a Qt terminal application?

    Hey,

    Right,first off you have to initiate a QApplication object (for event dispatching,application initialization,etc. look at the Assistant for full info).

    Then,it's not quite clear what you're trying to get connected.I mean if the code's snippet you provided is the main() program function then...what is "this" then? THere are two "types" of timers available:

    1.Event-driven timer (look at the int QObject::startTimer(int) )

    2.QTimer signals

    There's a great explanation of both in Assistant,so check it out...

    If you're using connect outside the class (let's say in the main(int argc,char* argv[]) function),you should use the static version of it:

    Qobject::connect(...);

  4. The following user says thank you to AlexSudnik for this useful post:

    hakermania (27th December 2010)

  5. #4
    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: Use connect() to a Qt terminal application?

    Thx for the replies, I really appreciate it!
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  6. #5
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use connect() to a Qt terminal application?

    Quote Originally Posted by hakermania View Post
    Qt Code:
    1. #include <stdlib.h>
    2. #include <QtCore/QTimer>
    3.  
    4. void check(){
    5. //perform a check every 10 secs
    6. }
    7.  
    8. int main()
    9. {
    10. QTimer *timer = new QTimer;
    11. connect(timer, SIGNAL(timeout()), this, SLOT(check()));
    12. timer->start(10000)
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Ok, this is the code.... i get error that connect() wasn't declared over here :/
    What Qt library should I include to use connect() function? Thank you!
    What you're trying to do, the way you're trying to do it, is not possible in Qt. Beyond what others have stated, that you need a QApplication, the Qt signal/slot mechanism unfortunately doesn't work the way you're trying to use it. All Qt slots must be member functions within an object that inherits from QObject and uses the Q_OBJECT macro. You can't connect free functions to Qt signals like you can with other signal/slot mechanisms (like boost or GTKmm).

    You CAN call the connect function from outside of any Qt object though:
    Qt Code:
    1. int main()
    2. {
    3. ... setup ...
    4. QObject::connect(timer, SIGNAL(timeout()), ptr_to_object_containing_check, SLOT(check()));
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 

    Thus, fixing your code might resemble something like so:

    Qt Code:
    1. #include <QtCore/QTimer>
    2. #include <QCoreApplication>
    3. #include <QObject>
    4.  
    5. class checker : public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. checker(QObject * o = 0) : QObject(o) {}
    10.  
    11. public slots:
    12. void check() { ... }
    13. };
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QCoreApplication a(argc, argv);
    18. QTimer * timer = new QTimer;
    19. QObject::connect(timer, SIGNAL(timeout()), new checker(timer), SLOT(check()));
    20. timer->start(10000);
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by nroberts; 27th December 2010 at 20:14.

Similar Threads

  1. Terminal into Qt application
    By paF4uko in forum Qt Programming
    Replies: 4
    Last Post: 27th February 2009, 11:22
  2. help about terminal window
    By andyyeng in forum Qt Programming
    Replies: 0
    Last Post: 29th October 2008, 15:22
  3. Looking for widget to run terminal app
    By marcell in forum Qt Programming
    Replies: 0
    Last Post: 16th May 2008, 16:24
  4. qtopia terminal application
    By Savita in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 29th February 2008, 07:24
  5. My application can't connect to database when deploy it???
    By gtthang in forum Installation and Deployment
    Replies: 1
    Last Post: 15th February 2006, 11:01

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.