Qt Debugging Aids

Objectives

  • Be able to write debug messages
  • Understand the usage of asserts
  • Be able to use GDB with Qt
  • Understand why event filters can be helpful for debugging

Warning and Debugging Messages

  • 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.

Assert Functions

  • void Q_ASSERT ( bool test ): Prints a warning message containing the source code file name and line number if test is false.
  • void Q_ASSERT_X ( bool test, const char * where, const char * what ): Prints the message what together with the location where, the source file name and line number if test is false.

Using GDB with Qt

  • GDB is included in the Qt Creator.
  • There are three ways to debug an application with gdb:
    • You can start the application from within gdb.
    • You can attach gdb to an already running application.
    • You can run gdb after an application has crashed using a core file.

Using Event Filters for Debugging

  • An event filter gets to process events before the target object does, allowing it to inspect and discard the events as required.
  • Once an event filter has been registered for a {QApplication}} object, every event for every object in the application is sent to eventFilter() before it is sent to any event filter. This is useful for debugging.

Examples

Debugging QStrings

QString fileName = "Hello";
qDebug(fileName.toLatin1());
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.