Results 1 to 11 of 11

Thread: qtimer in console application

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qtimer in console application

    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.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4. QTimer *timer = new QTimer(NULL);
    5. QObject::connect(timer, SIGNAL(timeout()), update, SLOT(msg())); ???????
    6. timer->start(1000);
    7. return 0;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for helping.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: qtimer in console application

    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.
    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
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qtimer in console application

    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)

    LE: too late, Wysota was faster

  4. #4
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qtimer in console application

    The exit 0 is because I am doing a filecopy:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. // QStringList l gets filled with data
    4. QFile out(l[0]);
    5. if (!out.open(QIODevice::WriteOnly))
    6. return 0;
    7. for (int i = 1; i < l.size(); i++)
    8. {
    9. QFile in(l.at(i));
    10. in.open(QIODevice::ReadOnly);
    11. QByteArray b = in.readAll();
    12. out.write(b);
    13. in.close();
    14. }
    15. out.close();
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by JeanC; 31st March 2011 at 14:08.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: qtimer in console application

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


  6. #6
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qtimer in console application

    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?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: qtimer in console application

    Quote Originally Posted by JeanC View Post
    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.
    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.


  8. #8
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qtimer in console application

    Quote Originally Posted by wysota View Post
    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: qtimer in console application

    Quote Originally Posted by JeanC View Post
    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.
    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.


  10. #10
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qtimer in console application

    Quote Originally Posted by wysota View Post
    not to provide entertainment in your free time.
    This is uncalled for.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: qtimer in console application

    Quote Originally Posted by JeanC View Post
    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.
    Last edited by wysota; 31st March 2011 at 15:36.
    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.


Similar Threads

  1. Console application beginning
    By Raccoon29 in forum Newbie
    Replies: 6
    Last Post: 23rd March 2012, 23:42
  2. about console application
    By jirach_gag in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 11:39
  3. QT console application and CTRL+C
    By dawwin in forum Qt Programming
    Replies: 3
    Last Post: 18th March 2011, 16:35
  4. open console application
    By hirakjyoti in forum Newbie
    Replies: 1
    Last Post: 30th January 2011, 16:34
  5. Replies: 2
    Last Post: 21st November 2010, 18:03

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.