2013-02-01 2 views
11

qDebug()ステートメントがQtの標準メッセージハンドラでうまく動作するが、自分自身に切り替えると失敗する理由を調べた後、ここで魅力的です他の誰かが問題を経験しているかどうかを確認してください。何もしない試してみました/について私が知っているQt5はqDebug()ステートメントをQt Creator 2.6コンソールにリダイレクトします

物事、...

1)CONFIG += console

2)DEFINES -= QT_NO_WARNING_OUTPUT QT_NO_DEBUG_OUTPUT

3)::fprintf(stderr, "ERROR\n"); ::fflush(stderr);

4)::fprintf(stdout, "OUTPUT\n"); ::fflush(stdout);

5)std::cerr << "CERROR" << std::endl; std::cerr.flush();

ハンドラに建て使用している場合、それが正しく動作ただし

は、最新のテストがWinAPIのはこれがすべて正しい動作になりコマンドを使用して自分自身にコンソールを割り当てた

int main(int argc, char *argv[]) { 
    // Use my handler 
    qInstallMessageHandler(MyCustomLogger); 
    qDebug() << "Not Printed"; 

    // Use standard handler 
    qInstallMessageHandler(0); 
    qDebug() << "Correctly Printed"; 

    // Use my handler again 
    qInstallMessageHandler(MyCustomLogger); 
    qDebug() << "Not Printed Again..."; 
} 

(すなわち、それはQtCreatorコンソールにメッセージを出力します)私が作成したコンソールでstderrとstdoutへの出力が見えます。しかし、これは私が望む振る舞いではなく、私はこの出力をQtCreatorで見ることができるようにしたいと考えています。

標準メッセージハンドラがどのようにデバッガに出力されるかについての考えはありますか? まだQtソースでは見つかりませんでした。

+5

、qDebugは()デバッグチャネルではなく、標準エラー出力を使用しています。 –

+0

私は以前、デバッグチャネルについて聞いたことがないことを認めなければなりません。 –

答えて

14

フランク・オスタッフェルドは、彼のコメントで述べたように:

をWindowsでは、qDebugは()デバッグチャネルではなく、標準エラー出力を使用しています。

QDebugコードとQMessageLoggerを掘り下げた後、私は答えを見つけました。便利なWinAPI関数OutputDebugString。 (Peppeの者から修正)

使用方法:Windows上で

#include <QApplication> 
#include <QtDebug> 
#include <QtGlobal> 

#include <stdio.h> 
#include <stdlib.h> 
#include <Windows.h> 

void MyMessageOutput(QtMsgType Type, const QMessageLogContext& Context, const QString &Message) 
{ 
    OutputDebugString(reinterpret_cast<const wchar_t *>(Message.utf16())); 
} 

int main(int argc, char **argv) 
{ 
    // A GUI application 
    QApplication app(argc, argv); 

    // Custom handler 
    qInstallMessageHandler(myMessageOutput); 
    qDebug() << "Printed in the console using my message handler in a windows GUI application"; 

    // Default handler 
    qInstallMessageHandler(0); 
    qDebug() << "Also printed in the console!"; 

    // Show GUI here 
    //MainForm *MF = new MainForm(); 
    //MF->show(); 

    return app.exec(); 
} 
+1

興味深いことに、これはCreatorがカスタムハンドラからメッセージを出力する方法です* *(そうでない場合、ハンドラは入力されますが、何もCreatorコンソールで終了しません)。 – mlvljr

2

問題を再現できません:正しく動作します。

#include <QCoreApplication> 
#include <QtDebug> 
#include <QtGlobal> 

#include <stdio.h> 
#include <stdlib.h> 

void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) 
{ 
    QByteArray localMsg = msg.toLocal8Bit(); 
    fprintf(stderr, "MESSAGE (%s:%u %s): %s\n", context.file, context.line, context.function, localMsg.constData()); 
    fflush(stderr); 
} 

int main(int argc, char **argv) 
{ 
    QCoreApplication app(argc, argv); 
    qInstallMessageHandler(myMessageOutput); 
    qDebug() << "Printed in the console"; 
    qInstallMessageHandler(0); 
    qDebug() << "Also printed in the console"; 
    return app.exec(); 
} 
+0

それはとても奇妙です。私は使用している他のlibとやり取りできますか?私のコードでそのハンドラを実行するとき、私はまだ1行だけを取得します... –

+0

私はあなたの例では動作しないguiを持つことができるようにQCoreApplicationではなくQApplicationを使用しています。 –

+1

問題を解決し、OutputDebugString(...)を使用してください システムが私に応答する時間を5時間で追加します... –

関連する問題