Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-36177

Emiting signal with custom type fails

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.2.0
    • None
    • Visual studio 2012, Qt 5.2

    Description

      I'm trying to emit signal with custom type. Type is declared with *Q_DECLARE_METATYPE* and registered with *qRegisterMetaType*.

      When I emit a signal, then I get next error to the output stream:

      Type "MyType" has id: 1024 ; register status: true
      QObject::connect: Cannot queue arguments of type 'MyType' (Make sure 'MyType' is registered using qRegisterMetaType().)

      Bug is reproducable only if objects are in different threads and MyType declared inside a namespace.

      Sample code:
      MyType.h

          #define SHOW_BUG
          
          #ifdef SHOW_BUG
          
              namespace NS
              {
                  struct MyType
                  {
                      int val;
                  };
              }
              Q_DECLARE_METATYPE( NS::MyType );
          
          #else
          
              struct MyType
              {
                  int val;
              };
              Q_DECLARE_METATYPE( MyType );
          
          #endif
      

      MyClass.h:

          #include "MyType.h"
          
          namespace NS
          {
              
              class MyClass
                  : public QObject
              {
                  Q_OBJECT
              public:
                  MyClass( QObject *parent = NULL );
                  ~MyClass();
          
              signals:
                  void sendMyType( const MyType& tt );
          
              public slots:
                  void invokeBug();
                  void getMyType( const MyType& tt );
              };
          
          }
      

      MyClass.cpp

          #include <QDebug>
          
          namespace NS
          {
              
              MyClass::MyClass(QObject *parent)
                  : QObject(parent)
              {
                  qRegisterMetaType< MyType >();
              }
          
              MyClass::~MyClass()
              {
              }
          
              void MyClass::invokeBug()
              {
                  const int id = qMetaTypeId< MyType >();
                  const bool test = QMetaType::isRegistered( id );
                  qDebug() << "Type \"MyType\" has id: " << id << "; register status: " << test;
          
                  MyType tt;
                  tt.val = 42;
                  emit sendMyType( tt );
              }
          
              void MyClass::getMyType( MyType const& tt )
              {
                  qDebug() << "Slot fired: " << tt.val;
              }
          
          }
      

      Main.cpp

          #include "MyClass.h"
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a( argc, argv );
          
              NS::MyClass c1;
              NS::MyClass c2;
              QThread thread;
              thread.start();
          
              c2.moveToThread( &thread ); // Comment this to fix
          
              QObject::connect( &c1, &NS::MyClass::sendMyType, &c2, &NS::MyClass::getMyType );
              QTimer::singleShot( 0, &c1, SLOT( invokeBug() ) );
          
              return a.exec();
          }
      

      If namespace in 'MyType' declaration is removed OR objects are in same thread - then everything is OK.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            sazonov Dmitry Sazonov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes