PDA

View Full Version : QCoreApplication question



barnabyr
24th March 2006, 04:20
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


#include "MyCoreApplication.h"

int main(int argc, char *argv[])
{
MyCoreApplication a(argc, argv);

return a.exec();
}


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.

Brandybuck
24th March 2006, 06:16
If your command line application is not event driven, then you probably don't need QCoreApplication.

barnabyr
24th March 2006, 16:42
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 ..


qDebug(str.toAscii().constData()) or
qWarning("%s",str.toAscii().constData());

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

barnaby

jpn
24th March 2006, 16:50
From docs (http://doc.trolltech.com/4.1/qtglobal.html#qDebug):
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;

barnabyr
24th March 2006, 17:27
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

Brandybuck
24th March 2006, 19:38
qWarning("%s",str.toAscii().constData());

I banged my head quite a bit over stuff like this, until I discovered I could do:


qWarning(qPrintable(str));