Results 1 to 4 of 4

Thread: Q_DECLARE_METATYPE vs qRegisterMetaType for non global namespace classes (QTest)

  1. #1
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Q_DECLARE_METATYPE vs qRegisterMetaType for non global namespace classes (QTest)

    Hello guys,

    I'm trying to test my class which emits a signal where one of parameters is my custom class:

    Qt Code:
    1. namespace MyNamespace
    2. {
    3.  
    4. struct MyData
    5. {
    6. };
    7.  
    8. class MyClass : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. ...
    14. signals:
    15. void mySignal(const MyData &);
    16. };
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    So my testing code looks like:

    Qt Code:
    1. Q_DECLARE_METATYPE(MyNamespace::MyData)
    2.  
    3. namespace MyNamespace
    4. {
    5.  
    6. class MyClassTest : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public slots:
    11. void initTestCase()
    12. {
    13. qRegisterMetaType<MyNamespace::MyData>("MyData"); // case 1
    14. //qRegisterMetaType<MyNamespace::MyData>("MyNamespace::MyData"); // case 2
    15. }
    16.  
    17. private slots:
    18.  
    19. void test_mySignal()
    20. {
    21. ...
    22. QSignalSpy mySignalSpy(my_object, SIGNAL(mySignal(const MyData &)));
    23.  
    24. MyData expected_data;
    25. ... // code which triggers the signal
    26.  
    27. QCOMPARE(mySignalSpy.count(), 1); // Works
    28.  
    29. QList<QVariant> mySignalResult = mySignalSpy.takeFirst();
    30. MyData arrived_data = mySignalResult.at(0).value<MyNamespace::MyData>(); // Here I get empty data
    31.  
    32. QCOMPARE(arrived_data, expected_data);
    33. }
    34. };
    35. }
    To copy to clipboard, switch view to plain text mode 

    1. I've put Q_DECLARE_METATYPE in order to have MyData to / from QVariant functionality working. And the following code works properly:
    Qt Code:
    1. MyData data;
    2. QVariant data_variant = QVariant::fromValue<MyData>(data);
    3. MyData data_new = data_variant.value<MyData)();
    To copy to clipboard, switch view to plain text mode 

    2. I've put qRegisterMetaType in order to have signals / slots with MyData parameters working.

    As you see I have two cases of qRegisterMetaType usage: the first case seems to make signals / slots working, but I get empty MyData from the mySignalResult; the second case leads to warning "Dont't know how to hande 'MyData'" emitted by QSignalSpy.

    So the question is - what I'm doing wrong ?
    Last edited by topoden; 18th December 2009 at 18:25. Reason: Just amended the subject to better outline the topic

  2. #2
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Q_DECLARE_METATYPE vs qRegisterMetaType in QSignalSpy (QTest)

    As I understand here I have type names conflict:

    Qt Code:
    1. Q_DECLARE_METATYPE(MyNamespace::MyData) // registers the type with "MyNamespace::MyData" name
    2.  
    3. qRegisterMetaType<MyNamespace::MyData>("MyData") // registers the same type with "MyData" name
    To copy to clipboard, switch view to plain text mode 

    The signal I have expects the type to be registered with "MyData" name. So if I change declaration of the signal to be:

    Qt Code:
    1. namespace MyNamespace
    2. {
    3.  
    4. class MyClass : public QObject
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. ...
    10. signals:
    11. void mySignal(const MyNamespace::MyData &);
    12. };
    13. }
    To copy to clipboard, switch view to plain text mode 


    Then everything starts working.

    The question now is, why do I have to fully qualify the class in the signal declaration ? The class declaration (where the signal is declared) is located in the same namespace.

    Could someone please explain me the full logic covered behind this ?

  3. #3
    Join Date
    Jul 2011
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Q_DECLARE_METATYPE vs qRegisterMetaType for non global namespace classes (QTest)

    The reason for this behavior might be caused by the fact that runtime signal/slot connections are checked by string manipulation - both Q_DECLARE_METATYPE, SIGNAL, SLOT macros and 'moc' are (among other things) converting type-names to text and pass it along into qt's machinery. For C++ compiler changing function declaration to fully qualified parameter shouldn't make any difference, but probably 'moc' parsing file has some problems figuring it out. For some time I was wondering how smart this Meta-Object-Compiler really is, and how deeply it inspects my code - clearly some more investigation is needed.

    Apart from that, how do you connect your signals? if you wrote your code manually using qt-creator then it's likely that autocompletion led you in each case (signal with and without full name) to slightly different 'connect' lines, which could also make some difference.
    Last edited by j_kubik; 21st April 2013 at 02:31.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Q_DECLARE_METATYPE vs qRegisterMetaType for non global namespace classes (QTest)

    Additional to having the fully qualified name in the signal and slot declarations, I would just call qRegisterMetaType<MyNamespace::MyType>(), i.e. without function argument and let Qt figure out the most appropriate string.

    Also, Q_DECLARE_METATYPE(MyNamespace::MyType) should be in the header declaring MyType so that you don't have to repeat it all over again and again.

    In normal applications you usually won't need qRegisterMetaType(), here in the unittest you need it for QSignalSpy

    Cheers,
    _

Similar Threads

  1. Qtest QTableWidget
    By tc in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2010, 22:47
  2. QTest dir not included by qmake
    By allstar in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2007, 11:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.