PDA

View Full Version : Use a QTimer in a Qt-non-GUI app.



hakermania
28th August 2010, 11:03
This is the source code:
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <QTimer>
using namespace std;
time_t seconds;
seconds = time (NULL); //EROR No1
QTimer *timer = new QTimer(this); //ERROR No2
connect(timer, SIGNAL(timeout()), this, SLOT(update_time())); //ERROR No3

int main ()
{ timer->start(1000);
return 0;
}

void update_time()
{
int a=1;
while(a){
time_t seconds;
seconds = time (NULL);
cout << "Seconds = " << seconds << endl;}
}

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:

7: error: expected constructor, destructor, or type conversion before ‘=’ token
8: error: invalid use of ‘this’ at top level
9: error: expected constructor, destructor, or type conversion before ‘(’ token
thank you for any replies!

wysota
28th August 2010, 11:07
How about:

QTimer *timer = new QTimer(...);
connect(timer, SIGNAL(timeout()), ...);
timer->start(1000);
//...
void X::timerSlot() {
qDebug() << "Time:" << QDateTime::currentDateTime().toString();
}

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().

hakermania
28th August 2010, 11:18
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?

hakermania
28th August 2010, 12:08
I mean where to place the
QTimer *timer = new QTimer(...);
connect(timer, SIGNAL(timeout()), ...);
timer->start(1000); ??

hakermania
28th August 2010, 13:45
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!:confused:

TorAn
28th August 2010, 15:16
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 http://en.wikipedia.org/wiki/C_(programming_language), look for "Hello, world" example

hakermania
28th August 2010, 16:49
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!:mad::mad::mad:

squidge
28th August 2010, 18:45
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.

wysota
28th August 2010, 21:21
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.

hakermania
29th August 2010, 01:52
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 :rolleyes:
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!

wysota
29th August 2010, 06:53
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.

// main.cpp
#include <QtCore>

class Timer : public QTimer {
Q_OBJECT
public:
explicit Timer(QObject *parent = 0) : QTimer(parent) {
connect(this, SIGNAL(timeout()), this, SLOT(showTime()));
}
private slots:
void showTime() {
qDebug() << "Current date and time:" << QDateTime::currentDateTime().toString();
}
};

#include "main.moc"

int main(int argc, char **argv){
QCoreApplication app(argc, argv);
Timer timer;
timer.start(1000); // show time approximately every second
return app.exec();
}

hakermania
29th August 2010, 10:36
Thanks for the answer....
I swear I will analyze it.:)