当前位置: 首页 > news >正文

Qt中QDebug的使用

      QDebug类为调试信息(debugging information)提供输出流。它的声明在<QDebug>中,实现在Core模块中。将调试或跟踪信息(debugging or tracing information)写出到device, file, string or console时都会使用QDebug。

      此类的成员函数参考:https://doc.qt.io/qt-6/qdebug.html
      通常情况下,调用qDebug()函数获取调试信息
      qDebug()函数返回QDebug对象。QDebug格式化输出会自动在参数之间添加空格,并在QString、QChar参数周围添加引号。可以通过space()、nospace()和quote()、noquote()方法来调整这些选项。
      将自定义类型(custom types)写入流:可以将许多标准类型写入QDebug对象,Qt提供对大多数Qt值类型的支持。若要添加对自定义类型的支持,需要实现流操作符(streaming operator)。

      以下为测试代码:

namespace {typedef struct {long x, y, z;
} Coordinate;QDebug operator<<(QDebug debug, const Coordinate& c)
{// QDebugStateSaver limits changes to the formatting to the current scopeQDebugStateSaver saver(debug);debug.nospace() << '(' << c.x << ", " << c.y << ", " << c.z << ')';return debug;
}} // namespaceint test_qdebug_1()
{// qDebug(const char *message, ...):与C的printf(const char * format, ...)函数类似qDebug("current date: %d:%d:%d", QDate::currentDate().year(), QDate::currentDate().month(), QDate::currentDate().day());printf("current date: %d:%d:%d\n", QDate::currentDate().year(), QDate::currentDate().month(), QDate::currentDate().day());qDebug() << "Date:" << QDate::currentDate();QString s("beijing");qDebug() << "s:" << s;QByteArray ba("haidian");qDebug() << "ba:" << ba;// 注意以下两条语句输出的差异qDebug() << "Types:" << QString("String") << QChar('x') << QRect(0, 10, 50, 40);qDebug().nospace().noquote() << "Types:" << QString("String") << QChar('x') << QRect(0, 10, 50, 40);// 将自定义类型写入流Coordinate c = { 10, 20, 30 };qDebug() << "Coordinate:" << c;return 0;
}

      执行结果如下图所示:

      GitHub:https://github.com/fengbingchun/Qt_Test

http://www.lryc.cn/news/64343.html

相关文章:

  • vue使用路由的query配置项时如何清除地址栏的参数
  • Redis-列表(List)
  • ripro主题修改教程-首页搜索框美化教程
  • 写作业用白光还是暖光?盘点色温4000K的护眼台灯
  • Java时间类(一)-- SimpleDateFormat类
  • 07 Kubernetes 网络与服务管理
  • 并发编程之Atomic原子操作类
  • 管家婆辉煌Ⅱ 13.32版安装方法
  • 常见的接口优化技巧思路
  • 【Java EE】-使用Fiddler抓包以及HTTP的报文格式
  • Java异步编程
  • C++类与对象(二)——构造函数与析构函数
  • c++标准模板(STL)(std::array)(四)
  • vue3计算属性
  • Java 中的访问修饰符有哪些(九)
  • HR员工管理的三重境界:管事、管人、管心
  • 延迟队列与SpringBoot实战
  • 【算法】九键输入法
  • jvm之类加载器
  • Chapter4:频率响应法(上)
  • 【6. 激光雷达接入ROS】
  • Java 基础进阶篇(三)—— 面向对象的三大特征之二:继承
  • [angstromctf 2023] 部分
  • 死信队列
  • 基于YOLOv5的目标检测系统详解(附MATLAB GUI版代码)
  • 使用ChatGPT工具阅读文献的实战教程
  • 实训笔记1
  • CCD视觉检测设备如何选择光源
  • 基于协同过滤的旅游推荐系统设计与实现(论文+源码)_kaic
  • 代码随想录补打卡 746 使用最小花费爬楼梯