Results 1 to 6 of 6

Thread: QCoreApplication question

  1. #1
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QCoreApplication question

    Hello ..

    I have built a few Gui apps with QT and I am familiar with the concept of building
    a main window and then building widgets and having them talk to each other ...
    ... but I am a little confused how I would write a
    command line program using QCoreApplication.

    If I subclassed QCoreApplication and had a main function like

    Qt Code:
    1. #include "MyCoreApplication.h"
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. MyCoreApplication a(argc, argv);
    6.  
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Then what is the next step ? ... I want to pass my program some arguments
    and then have it perform some tasks and then quit i.e. I don't believe I need
    an event loop ... However the structure of the code is confusing me.

    Would I put most of my core code now in the subclass of QCoreApplication
    and then call exit() inside its constructor or one of its methods ?

    As you can see I am confused on a very fundamental level so I would appreciate
    if someone could post some code of just a simple command line app that
    does something simple and then quits.

    Thank you very much if you can help.

    barnaby.

  2. #2
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QCoreApplication question

    If your command line application is not event driven, then you probably don't need QCoreApplication.

  3. #3
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QCoreApplication question

    Well that is what I did try before but some QT functions do not work as expected
    if you have all your code in the main loop.

    For example qWarning and qDebug can not be passed a QString but you have
    to say ..

    Qt Code:
    1. qDebug(str.toAscii().constData())
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. qWarning("%s",str.toAscii().constData());
    To copy to clipboard, switch view to plain text mode 

    However once you pass control to the sub class of QCoreApplication then things
    work as I expect.

    barnaby

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QCoreApplication question

    From docs:
    If you pass the function a format string and a list of arguments, it works in similar way to the C printf() function.
    Example:
    qDebug("Items in list: %d", myList.size());
    If you include <QtDebug>, a more convenient syntax is also available:
    qDebug() << "Brush:" << myQBrush << "Other value:" << i;
    This syntax automatically puts a single space between each item, and outputs a newline at the end. It supports many C++ and Qt types.
    So, with Qt types (e.g. QString) you could use qDebug like this:
    #include <QtDebug>
    qDebug() << str;

  5. #5
    Join Date
    Mar 2006
    Location
    San Francisco, CA
    Posts
    23
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QCoreApplication question

    hey there jpn

    I do use that other style too in most of my qt apps. I was just remarking that you have to
    explicitly pass a const char * to qDebug if you have all your code in a main function.
    and don't instance QCoreApplication.

    have you written any simple command line apps with Qt or do you know where some
    examples are at ?

    thanks

  6. #6
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QCoreApplication question

    Quote Originally Posted by barnabyr
    Qt Code:
    1. qWarning("%s",str.toAscii().constData());
    To copy to clipboard, switch view to plain text mode 
    I banged my head quite a bit over stuff like this, until I discovered I could do:

    Qt Code:
    1. qWarning(qPrintable(str));
    To copy to clipboard, switch view to plain text mode 

  7. The following 3 users say thank you to Brandybuck for this useful post:

    barnabyr (24th March 2006), sunil.thaha (28th March 2006), wysota (25th March 2006)

Similar Threads

  1. QLocalServer without QCoreApplication ?
    By Raistlin in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2008, 22:44
  2. Exceptions / setjmp/longjmp question
    By Aceman2000 in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2008, 17:14
  3. Replies: 5
    Last Post: 24th July 2007, 17:35
  4. QCoreApplication!!!!
    By fellobo in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2007, 00:56

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.