If you want to debug Qt application on Windows you can use some basic debugging techniques provided by Qt.
To make sure that debug version of binary is compiled, do not forget to specify this in .pro file (no matter which platform). Keep in mind that it only has effect if you have QT compiled in debug mode (or both - debug and release). Some linux distros may ship QT compiled in just "release" mode. In that case you have to compile QT yourself, see preparations.
CONFIG += debug
When developing on Windows it is essential to enable console application output. Add this into your .pro file:
CONFIG += console
Just run binary from console:
cd myqtapp_directory ./myqtapp
Re-run "qmake" and recompile with "make". If you get strange errors, try to run "make distclean" first.
If we run debug/myqtapp.exe, we see the console alongside with our application.
Let's modify the clear() slot of our myQtApp in myqtapp.cpp:
void myQtApp::clear()
{
qDebug() << "myQtApp::clear() slot invoked";
qDebug() << "spinBox1 value: " << spinBox1->value();
qDebug() << "textEdit text: " << textEdit->toPlainText();
textEdit->clear();
qDebug() << "textEdit cleared";
qDebug() << "Widget" << this << "at position" << this->pos();
// qDebug() just writes to console, nothing more
}
Recompile, run and press Clear button.
The console catches non-fatal messages. I.e. if we connect signal to non nonexistent slot like this:
connect( pushButton_about, SIGNAL( clicked() ), this, SLOT( nonexistent() ) );
When application is run following message appears in console:
See also Debugging Techniques in Qt documentation.
We'll show some basic usage of gdb - GNU Debugger for windows (console application). You can download and install it from http://mingw.org. I recommend GDB 6.x.
Make sure to add path of gdb.exe to PATH environment variable. When you invoke gdb -v from commandline you should see gdb version report.
Let's make some buggy code.
void myQtApp::clear()
{
QProcess *proc;
delete proc; // pointer proc is uninitialized
}
Pushing clear button produces error and application closes.
Load application into debugger.
gdb debug/myqtapp.exe GNU gdb 6.3 Copyright 2004 Free Software Foundation, Inc. ..
Now let's run program using "run".
(gdb) run
Application is run in the debugger which catches fatal message:
Program received signal SIGSEGV, Segmentation fault. 0x004021d5 in myQtApp::clear() (this=0x392bff8) at myqtapp.cpp:105 105 delete p;
Backtrace
Backtrace is very usefull. It provides maximum information available about crash. It's invoked with bt command.
(gdb) bt
#0 0x004021d5 in myQtApp::clear (this=0x392bff8) at myqtapp.cpp:105
#1 0x004028f2 in myQtApp::qt_metacall (this=0x392bff8, _c=InvokeMetaMethod,
_id=2, _a=0x22eaf0) at debug/moc_myqtapp.cpp:69
#2 0x100f8d93 in QMetaObject::activate (sender=0x392cf18,
from_signal_index=28, to_signal_index=29, argv=0x22eaf0)
at kernel/qobject.cpp:2809
#3 0x100f90c4 in QMetaObject::activate (sender=0x392cf18, m=0xbfa2c0,
from_local_signal_index=2, to_local_signal_index=3, argv=0x22eaf0)
at kernel/qobject.cpp:2860
..
..
To quit GDB, use "q" or "quit".
Redirecting GDB output into file
If you would like to redirect GDB output, use following gdb command (works with GDB 6.x):
(gdb) set logging on
gdb.txt will be created in current directory.