Reputation: 38255
I would like to add a debugging mode for Qt GUI application. In debugging mode, any GUI widget events would dump out debug information rather than doing the original functionality. For example, clicking button A
would print out the checkbox options being selected, where button A
's original function is process something to the selected options.
I was thinking to add a menu option "debug mode
", or a secret keyboard shortcut to enable it. And in the callback functions associated with widget signals (clicked
/ textEdited
), use if(isDebugMode)/else
to distinguish regular function mode code.
Is this a good model to test GUI functionality? Is there any better way?
Upvotes: 0
Views: 1713
Reputation: 52327
You might want to use the Signal/Slot system for your debugging. It reminds me of aspect-oriented programming.
Debug
class derived from QObject
that holds several slots with the typical signatures.QObject::sender()
function to retrieve the originator of the eventThis will help you in keeping most of your code clean of the debug stuff.
For an inspiration, here is some example code that tracks focus changes within the application:
class Debug : public QObject
{
Q_OBJECT
...
// method for debugging focus
void focusChange(QWidget * old, QWidget * now);
};
void Debug::focusChange(QWidget *old, QWidget *now)
{
if (!old || !now)
return;
std::cerr << "Focus changed from " << old->objectName().toStdString()
<< " to " << now->objectName().toStdString() << std::endl;
}
QApplication app(argc, argv);
Debug dbg;
...
app.connect(&app, SIGNAL(focusChanged(QWidget*, QWidget*)),
&debug, SLOT(focusChange(QWidget*, QWidget*)));
// run Qt event loop
return app.exec();
Upvotes: 1