|
Wouldnt
it be nice to have a
class that continuously tracks a QObject
for any signal that is being generated from it and report the signal
generated along with its parameter values. QpuGenericSignalSpy does
exactly that.
The
functionality of
QpuGenericSignalSpy is divided into two classes
1. QpuGenericSignalSpyBase
and
2. QpuGenericSignalSpy
While
the former class takes the
responsibility of creating signal slot connnections to spy signals on
an object, the latter (this class) takes the responsibility of
reporting signals that were caught while emiting.
To spy
for signals on an object do
the following
1.
Create an instance of
QpuGenericSignalSpy
2. Pass a pointer to the object you want to spy to the spy created
in the previous step using the spyOn() method
3. Create a widget/QObject that consists of a slot to receive
signals with one string parameter. Have that slot report the contents
of the string to you.
4. Connect caughtSignalName() and caughtSignalParam() of the spy
to the object created in the previous step.
The
functionality of this class is
best explained with the help of an example. Considering the following
main() function
|
int
main(int argc, char **argv)
{
QApplication
a(argc, argv);
QDial d(0, 50, 5, 0);
a.setMainWidget(&d); d.show();
QTextEdit textEdit;
textEdit.show();
QpuGenericSignalSpy spy;
spy.spyOn( &d );
QObject::connect(
&spy,
SIGNAL(caughtSignalName(const QString&)), &textEdit,
SLOT(append(const QString&)) );
QObject::connect( &spy,
SIGNAL(caughtSignalParam(const QString&)), &textEdit,
SLOT(append(const QString&)) );
return
a.exec();
}
|
When
compiled and executed we will
see a display like this
For the
theory on how this is done
read the article on spying signals at
Q4puGenericSignalSpy
- for Qt 4.x
Q4puGenericSignalSpy
is a Qt4 avtar
of QpuGenericSignalSpy written for Qt3. This class took lesser time to
implement because of Qt4. The meta object system in Qt4 has received a
good overhaul and the changes are very nice. Infact the new meta object
system is easier to use than the previous ones.
What does
this class do ?
This
class looks for signals and
slots emitted from a QObject in a Qt4 application. Whenever the object
that is being spied on emits a signal or has one of its slots invoked;
this class emits a signal informing the world about it.
Take a
look at the example below
|
int
main(int argc, char **argv)
{
QApplication a(argc, argv);
QWidget
* topLevel = new
QWidget;
QVBoxLayout
* layout = new
QVBoxLayout(topLevel);
QDirModel
* dirModel = new
QDirModel(topLevel);
QTreeView
* treeView = new
QTreeView(topLevel);
treeView->setObjectName("DirTreeView");
layout->addWidget(treeView);
treeView->setModel(dirModel);
Q3TextEdit
* textEdit = new
Q3TextEdit(topLevel);
layout->addWidget(textEdit);
Q4puGenericSignalSpy
* spy =
new Q4puGenericSignalSpy (&a);
spy->spyOn(treeView);
QObject::connect(spy,
SIGNAL(caughtSignal(const QString&)), textEdit, SLOT(append(const
QString&)));
QObject::connect(spy,
SIGNAL(caughtSlot(const QString&)), textEdit, SLOT(append(const
QString&))); topLevel->show(); return a.exec();
}
|
When
executed the following output
was observed
|