PDA

View Full Version : qtimer in console application



JeanC
31st March 2011, 12:42
I'd like to make a console app which outputs a message every second.
Could someone please show me how to do that?
All I find are examples for QTimer::sinlgeShot() for console apps.



int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTimer *timer = new QTimer(NULL);
QObject::connect(timer, SIGNAL(timeout()), update, SLOT(msg())); ???????
timer->start(1000);
return 0;
}


Thanks for helping.

wysota
31st March 2011, 12:54
If you exit the application immediately then how do you expect it to do anything one second later? The fact that your program doesn't have a gui is meaningless. You need an object with a slot and a running event loop.

Zlatomir
31st March 2011, 12:58
I have two comments:
1) Where is update declared/defined? //that class will need a slot mgs()
2) You don't exec() your QCoreApplication, so you don't have an event loop (and you need one for QTimer (http://doc.qt.nokia.com/latest/qtimer.html#details))

LE: too late, Wysota was faster ;)

JeanC
31st March 2011, 14:03
The exit 0 is because I am doing a filecopy:



int main(int argc, char *argv[])
{
// QStringList l gets filled with data
QFile out(l[0]);
if (!out.open(QIODevice::WriteOnly))
return 0;
for (int i = 1; i < l.size(); i++)
{
QFile in(l.at(i));
in.open(QIODevice::ReadOnly);
QByteArray b = in.readAll();
out.write(b);
in.close();
}
out.close();
return 0;
}


It's for merging .vob files into one .mpg
And I'd like a QTimer to report progress (with cout << "\rmessage" and looking at filesize of destination)

do I still need QCoreApplication?
And how do I connect that slot please?

wysota
31st March 2011, 14:05
It won't, you need an event loop for that. And your way you are doing the copy is a great way of crashing your app or even your system. Using QFile::copy() static method might be a better suited approach.

JeanC
31st March 2011, 14:14
Why crashing?? What am I doing wrong?

I need to copy several files into one. I don't think I can use QFile::copy() for that, can i?

wysota
31st March 2011, 14:27
Why crashing?? What am I doing wrong?
Ask your debugger. It's likely you run out of memory as you probably need twice as much memory as the file you are copying.


I need to copy several files into one. I don't think I can use QFile::copy() for that, can i?
It depends if you limit yourself to asking this question or if you look at how the method is implemented and do something similar.

JeanC
31st March 2011, 14:44
Ask your debugger. It's likely you run out of memory as you probably need twice as much memory as the file you are copying.


I haven't gotten around to working with the debugger yet, I only use qDebug().

Honestly I don't see where I corrupt memory. That QByteArray will be destroyed each iteration because it goes out of scope won't it?
Please show me why this will crash / corrupt because I don't understand.

wysota
31st March 2011, 14:59
I haven't gotten around to working with the debugger yet, I only use qDebug().
Good time to start then.


Honestly I don't see where I corrupt memory.
That's why you need a debugger. It's meant to help you and not to provide entertainment in your free time.


Please show me why this will crash / corrupt because I don't understand.
I'm not your debugger.

JeanC
31st March 2011, 15:03
not to provide entertainment in your free time.


This is uncalled for.

wysota
31st March 2011, 15:30
This is uncalled for.

Let's see....

1. I'm spending my free time to help you
2. You are sitting in front of a computer with the problematic and crashing code
3. Instead of using a debugger as suggested you complain about me being "unfriendly" and providing "shitty remarks"
4. You want me to help you, which requires:

me to ask you for the complete code
me to run your code under a debugger to see why it crashes (assuming it does crash at all)
me to explain to you why your code crashes
you to understand and hopefully to correct the problem
optionally you to complain the code still "doesn't work", then me to ask about a debugger, you to complain about shitty remarks and so on...
eventually you to run to your boss/teacher/friend/whatever to gloat that you fixed the code


If you consider (1) and (4) I would be spending my free time waiting for you to provide code and then being "entertained by a debugger" while you whine about unfriendly people not wanting to do your work for you. If you don't want to use a tool then don't use it, it is your problem. You can be sitting in front of this code for ages and eventually maybe you will figure it out but then why don't you just spend ten seconds and let the debugger tell you where the code crashes? No time for whining, that's true, but at least the job gets done.

You can either invest your time to learn how to fix such problems or continue living in dark ages hoping somebody will tell you what you did wrong. This was the reason for saying what I said, I didn't want to offend you. The point is you have to provide information so that I can help you. Either you invest your time and evolve or you don't invest your time and stay behind.