2016-03-14 101 views
0

有了這個代碼爲什麼在關閉Qt C++主窗口後打印消息?

#include "mainwindow.h" 
#include <QApplication> 
#include <iostream> 
#include <QDir> 
#include <QTextStream> 

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

    QTextStream out(stdout); 
    out << QDir::currentPath(); 
    std::cout << "Why is that?"; 

    MainWindow mainWindow; 
    mainWindow.show(); 
    return a.exec(); 
} 

印刷兩個消息都合我的應用程序的主窗口之後,只有,這是爲什麼? 我試圖調試,調試器認爲他完成了這一行,但我看不到任何消息。

+2

沖洗流。 –

+1

或輸出到std :: err,那裏應該沒有緩衝。 –

回答

2

extern std::ostream cout;被緩衝,因此它可以選擇何時刷新其緩衝區stdout。在你的情況下,它是在你的程序終止時執行的。

你可以告訴std::ostream使用std ::沖洗沖洗,因爲這樣的:

std::cout << "Why is that?" << std::flush; 
相關問題