QT中进行debug输出和使用cout cin等的问题

来源:互联网 发布:58同城网招聘淘宝客服 编辑:程序博客网 时间:2024/05/29 03:08

在qt中大家都知道不能像纯C++那样使用cout等进行debug, 因为输出的内容会保留到程序退出的时候才进行输出。

 

Warning and Debugging Messages

Qt includes four global functions for writing out warning and debug text. You can use them for the following purposes:

Qt中包含了四个全局方法, 用于输出警告和调试信息, 列举如下:

  • qDebug () is used for writing custom debug output.用于输出调试信息
  • qWarning () is used to report warnings and recoverable errors in your application.警告信息
  • qCritical () is used for writing critical error mesages and reporting system errors.严重错误信息
  • qFatal () is used for writing fatal error messages shortly before exiting.很严重的错误, 输出后退出程序

If you include the <QtDebug> header file, the qDebug() function can also be used as an output stream. For example:

如果你include了<QtDebug>头文件, 上述的方法都可以当输出流进行使用, 例如:

 qDebug() << "Widget" << widget << "at position" << widget->pos();

 

 

如果想要使用cout, 应使用QTextStream重载

Cpp代码 复制代码 收藏代码
  1. #include <QApplication>    
  2. #include <QTextStream>    
  3.     
  4. int main(int argc, char *argv[])    
  5. {    
  6.     QApplication app(argc, argv);    
  7.     
  8.     QTextStream out(stdout);  //重载  
  9.     
  10.     out << "is QTextStream out " << endl;    
  11.     
  12.     return app.exec();    
  13. }  
0 0