tags:

views:

354

answers:

5

I have a Qt GUI application running on Windows that allows command-line options to be passed and under some circumstances I want to output a message to the console and then quit, for example:

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

  if (someCommandLineParam)
  {
    std::cout << "Hello, world!";
    return 0;
  }

  MainWindow w;
  w.show();

  return a.exec();
}

However, the console messages do not appear when I run the app from a command-prompt. Does anyone know how I can get this to work?

A: 

First of all you can try flushing the buffer

std::cout << "Hello, world!"<<std::endl;

For more Qt based logging you can try using qDebug.

Piotr Duda
qDebug won't work for a release mode build.
Rob
std::endl doesn't make any difference BTW.
Rob
A: 

I used this header below for my projects. Hope it helps.

#ifndef __DEBUG__H
#define __DEBUG__H

#include <QtGui>    

static void myMessageOutput(bool debug, QtMsgType type, const QString & msg) {

    if (!debug) return;

    QDateTime dateTime = QDateTime::currentDateTime();
    QString dateString = dateTime.toString("yyyy.MM.dd hh:mm:ss:zzz");

    switch (type) {

        case QtDebugMsg:
            fprintf(stderr, "Debug: %s\n", msg.toAscii().data());
            break;
        case QtWarningMsg:
            fprintf(stderr, "Warning: %s\n", msg.toAscii().data());
            break;
        case QtCriticalMsg:
            fprintf(stderr, "Critical: %s\n", msg.toAscii().data());
            break;
        case QtFatalMsg:
            fprintf(stderr, "Fatal: %s\n", msg.toAscii().data());
            abort();
    }
}

#endif

PS: you could add dateString to output if you want in future.

mosg
Using include guards starting with __* is asking for trouble...
rubenvb
This doesn't concern to the question... but maybe you right.
mosg
A: 

First of all, why would you need to output to console in a release mode build? Nobody will think to look there when there's a gui...

Second, qDebug is fancy :)

Third, you can try adding console to your .pro's CONFIG, it might work.

rubenvb
This application can be run in two modes - console using command-line switches amd as a GUI.
Rob
A: 

In your .pro add

CONFIG          += console
Derick
Sorry, duplicate of what rubenvb said.
Derick
+1  A: 

Windows does not really support dual mode applications.

To see console output you need to create a console application

CONFIG += console

However, if you double click on the program to start the GUI mode version then you will get a console window appearing, which is probably not what you want. To prevent the console window appearing you have to create a GUI mode application in which case you get no output in the console.

One idea may be to create a second small application which is a console application and provides the output. This can call the second one to do the work.

Or you could put all the functionality in a DLL then create two versions of the .exe file which have very simple main functions which call into the DLL. One is for the GUI and one is for the console.

David Dibben
Yes, this is what I've come to realise - two apps!
Rob