1

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?

Community
  • 1
  • 1
Qohelet
  • 1,459
  • 4
  • 24
  • 41

2 Answers2

1

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.

Qohelet
  • 1,459
  • 4
  • 24
  • 41
0

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

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • I've tried that too already. I've created a qttest.pro, ran qmake and make afterwards. To execute it I need to use `./qttest` but there isn't any change if I run it with our without parameters – Qohelet Jun 03 '14 at 16:00
  • Then you must be either doing it wrong or you do not have signals emitted at all which could be due to several reasons, e.g. bad test code, etc. It is difficult to say without further details, but anyway, your question was that in my understanding where to put this option, and this post answers that. ;-) All the rest would make your question too broad. – László Papp Jun 03 '14 at 16:01
  • I guess I'm doing it wrong :D - do you have maybe a small program lying around somewhere which definitively works and outputs some senseful stuff? – Qohelet Jun 03 '14 at 16:55
  • @Qohelet: just run the qt tests. – László Papp Jun 03 '14 at 20:31