Important: Please read the Qt Code of Conduct -

How to test if type of data emitted by signal is quint8 using QSignalSpy?

  • Using Qt 5.4.x

    QVariant::type() (still?) doesn't support basic C++ (and their respective Qt typedefs) so the following will always fail:

    Obj* o = new Obj(); // A class that contains signalFoo(quint8)
    QSignalSpy* oS = new QSignalSpy(o, SIGNAL(signalFoo(quint8)));
    // Trigger signalFoo(quint8)
    QCOMPARE(this->oS->count(), 1);
    QList<QVariant> oSArgs = this->oS->takeFirst();
    QVERIFY(oSArgs->at(0).type() == QVariant::UInt);

    The same applies if I use QVariant::Char.

    So my question is how exactly am I supposed to test the type of the returned data if the type is not supported? UInt stands for unsigned int (as per QMetaType::UInt), which is 4 bytes and not 1 that the quint8 stands for.

    I know that in the case above it's not actually required if I have just a single signal that the instance of Obj can emit but still it's something I'd like to know.

  • Lifetime Qt Champion


    Out of curiosity, why do you need to test the datatype ?

  • I wanted to make sure that if someone changes my code (the value range of the data carried by that signal; for example from quint8 to quint16) the respective unit test will detect this and fail. On multiple occasions I had to waste several hours hunting down bugs related to generating a numeric value with one range and then silently converting it to another (less accurate or with a different (usually smaller) value range).

    It's not that necessary but since I gave it a shot and found out that QVariant doesn't support it I decided to ask here. :)

  • It's much easier that you think. oS->isValid() will return false if someone changes the argument of signalFoo to anything other than quint8

Log in to reply