简体   繁体   中英

Monitor/Output emitted Qt-Signals

I defined some signals which are emitted on different occasions:

signals:
    void buttonXClicked(int x);
    void numButtonsChanged(int num);

Now I would just like to see how these signals look like and if the parameters are correct. It seems there are several approaches to monitor the signals.

In the post here rohanpm refers to the parameter -vs which is specified closer here: http://qt-project.org/doc/qt-4.8/qtestlib-manual.html#qtestlib-command-line-arguments

This seems to be an elegant and quick way of getting the information I require. But to be honest I'm unable to understand how and where I have to run -vs . It's not part of qmake . Where else do I have to put it? (I'm pretty new to qt).

Related to the QSignalSpy it seems to be necessary to change the existing classes? Isn't there an "external" approach as well?

There is plenty of documentation around how to test a slot - but related to signals? Could I use a printf or cout somewhere?

I got this idea while reading more about the moc and its functionality. (At least while using NetBeans) I get additional to my File ButtonTest.cpp the file moc_ButtonTest.cpp. Inside is a method called:

// SIGNAL 0
void ButtonTest::buttonXClicked(int _t1)
{
    void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
    QMetaObject::activate(this, &staticMetaObject, 0, _a);    
}

I could hardly believe it was so easy but I've just added a

std::cout <<"buttonXClicked: "<<_t1;

and it seems to do exactly what I want.

As the linked documentation writes:

Runs the toUpper test function with all available test data, and the toInt test function with the testdata called zero (if the specified test data doesn't exist, the associated test will fail).

/myTestDirectory$ testMyWidget -vs -eventdelay 500

where testMyWidget is the test binary built. Here goes the -vs documentation:

-vs outputs every signal that gets emitted

There is also some more documentation if you grep the help output:

/myTestDirectory$ testMyWidget --help | grep "\-vs"
-vs outputs every signal that gets emitted

If you happen to have trouble with writing QTestLib based unit tests, this is a good starting point for you with Qt 4:

QTestLib Manual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM