Results 1 to 12 of 12

Thread: Use a QTimer in a Qt-non-GUI app.

  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

    Question Use a QTimer in a Qt-non-GUI app.

    This is the source code:
    Qt Code:
    1. #include <stdio.h>
    2. #include <time.h>
    3. #include <iostream>
    4. #include <QTimer>
    5. using namespace std;
    6. time_t seconds;
    7. seconds = time (NULL); //EROR No1
    8. QTimer *timer = new QTimer(this); //ERROR No2
    9. connect(timer, SIGNAL(timeout()), this, SLOT(update_time())); //ERROR No3
    10.  
    11. int main ()
    12. { timer->start(1000);
    13. return 0;
    14. }
    15.  
    16. void update_time()
    17. {
    18. int a=1;
    19. while(a){
    20. time_t seconds;
    21. seconds = time (NULL);
    22. cout << "Seconds = " << seconds << endl;}
    23. }
    To copy to clipboard, switch view to plain text mode 
    As you understood I wanna have the system time in secs since 1/1/1970 and to be updated every 1 sec...
    Unfortunately I get plenty of errors:
    Qt Code:
    1. 7: error: expected constructor, destructor, or type conversion before ‘=’ token
    2. 8: error: invalid use of ‘this’ at top level
    3. 9: error: expected constructor, destructor, or type conversion before ‘(’ token
    To copy to clipboard, switch view to plain text mode 
    thank you for any replies!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use a QTimer in a Qt-non-GUI app.

    How about:
    Qt Code:
    1. QTimer *timer = new QTimer(...);
    2. connect(timer, SIGNAL(timeout()), ...);
    3. timer->start(1000);
    4. //...
    5. void X::timerSlot() {
    6. qDebug() << "Time:" << QDateTime::currentDateTime().toString();
    7. }
    To copy to clipboard, switch view to plain text mode 

    By the way, you get errors because you are trying to write C++ implementation code outside any function body. Here you can only have declarations but not assignments. Move the code into a class (its constructor, to be exact) and use the class from within main().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #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: Use a QTimer in a Qt-non-GUI app.

    Ok, most of the erros were gone, but I didn't understand where is tha constructor. in a GUI app is at ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    // code here }

    But where is it in a non-GUI app?

  4. #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 a QTimer in a Qt-non-GUI app.

    I mean where to place the
    QTimer *timer = new QTimer(...);
    connect(timer, SIGNAL(timeout()), ...);
    timer->start(1000);
    ??

  5. #5
    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 a QTimer in a Qt-non-GUI app.

    Is my question so difficult?
    The question is simple: How do I use a QTimer in a Qt-non-GUI application, which means that you don't have and don't want to create header file!

  6. #6
    Join Date
    Nov 2009
    Location
    US, Midwest
    Posts
    215
    Thanks
    62
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use a QTimer in a Qt-non-GUI app.

    Yes, hakermania, your question is very difficult. In fact, it is almost impossible for users of this forum to give you an answer that you will understand. People much better then me tried (btw, try once again to comprehend wysota's answer), but failed. Do you know why your question is that difficult? Because you lack the very basic understanding not only of C++, but C. I am saying it to you not to discourage your usage of Qt, but instead to redirect your efforts elsewere. For example, here [WIKI]http://en.wikipedia.org/wiki/C_(programming_language)[/WIKI], look for "Hello, world" example

  7. The following user says thank you to TorAn for this useful post:

    hakermania (28th August 2010)

  8. #7
    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 a QTimer in a Qt-non-GUI app.

    Thank you, I expected a better answer. I thought that's why the newbie section exists!
    And thx for all the encouragement you gave me to go on! And yes, i admit that I don't have an excellent knowledge of C++ but I find Qt an excellent tool to start with!

  9. #8
    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 a QTimer in a Qt-non-GUI app.

    You must understand that even though this is the newbie Qt forum, we still expect a certain level of C++ knowledge. Learning people C++ is really beyond the scope of this forum.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use a QTimer in a Qt-non-GUI app.

    Quote Originally Posted by hakermania View Post
    But where is it in a non-GUI app?
    It's not relevant whether the application has a GUI or not. A constructor is still a constructor. It's a C++ (or OOP in general) concept, not a Qt one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    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 a QTimer in a Qt-non-GUI app.

    yeah, ok, I got it. I've gone and read a bit about constructor on C++ and I've understood some things but I still cannot figure out where to place every single thing in the source code
    I've created a header file with the class Timer {} in the header file but I've tried everything but I still cannot get it to work!

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use a QTimer in a Qt-non-GUI app.

    Here is a complete code, just please analyze it and make sure you understand everything before using it as it is not the most trivial approach to the problem. In doubt please come back and ask if you can't find an answer using your favourite web search engine.
    Qt Code:
    1. // main.cpp
    2. #include <QtCore>
    3.  
    4. class Timer : public QTimer {
    5. Q_OBJECT
    6. public:
    7. explicit Timer(QObject *parent = 0) : QTimer(parent) {
    8. connect(this, SIGNAL(timeout()), this, SLOT(showTime()));
    9. }
    10. private slots:
    11. void showTime() {
    12. qDebug() << "Current date and time:" << QDateTime::currentDateTime().toString();
    13. }
    14. };
    15.  
    16. #include "main.moc"
    17.  
    18. int main(int argc, char **argv){
    19. QCoreApplication app(argc, argv);
    20. Timer timer;
    21. timer.start(1000); // show time approximately every second
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    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 a QTimer in a Qt-non-GUI app.

    Thanks for the answer....
    I swear I will analyze it.

Similar Threads

  1. Use of QTimer
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 4
    Last Post: 7th August 2009, 13:52
  2. QTimer
    By link7 in forum Newbie
    Replies: 4
    Last Post: 2nd June 2009, 08:35
  3. QTimer
    By dragon in forum Qt Programming
    Replies: 18
    Last Post: 16th November 2008, 14:15
  4. QTimer
    By Ahmad in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2007, 18:26
  5. QTimer
    By nirup in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2007, 14:13

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.