Results 1 to 20 of 21

Thread: qRegisterMetaType- reference as argument in connect throwing run time error

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default qRegisterMetaType- reference as argument in connect throwing run time error

    I have typedef in my code & I wanted to use this in connect function so I register this new data type.
    Qt Code:
    1. typedef std::vector< std::vector<int> > myData;
    2. qRegisterMetaType<myData>("myData");
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. connect(this, SIGNAL(startFetchingRecordsInThread(myData&)), this, SLOT(startFetchingRecords(myData&))); //is throwing below error
    To copy to clipboard, switch view to plain text mode 
    QObject::connect: Cannot queue arguments of type 'myData&'
    (Make sure 'myData&' is registered using qRegisterMetaType().)
    When I remove & (pass by reference) from argument
    SIGNAL(startFetchingRecordsInThread(myData))
    Its working fine.
    Is there some thing I missed here ??
    Thanks :-)

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Have you implemented the operators required for serializing your metatype?

  3. #3
    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: qRegisterMetaType- reference as argument in connect throwing run time error

    Reference arguments cannot be used with queued connection.

    After all a queued connection transports the arguments via copying them into an event and sending the event to the receiver object.

    Cheers,
    _

  4. #4
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by d_stranz View Post
    Have you implemented the operators required for serializing your metatype?
    Sorry I am not sure with this, What are the operators I need to implement & In which class I have to implement them ??
    Thanks :-)

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    QMetaType and qRegisterMetaTypeStreamOperators():

    If we want the stream operators operator<<() and operator>>() to work on QVariant objects that store custom types, the custom type must provide operator<<() and operator>>() operators.

  6. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by anda_skoa View Post
    Reference arguments cannot be used with queued connection.

    After all a queued connection transports the arguments via copying them into an event and sending the event to the receiver object.

    Cheers,
    _

    Changing my connect function to Qt:irectConnection worked out, I hope it will not create any problem as this is going to start my thread.


    Added after 4 minutes:


    Quote Originally Posted by d_stranz View Post
    QMetaType and qRegisterMetaTypeStreamOperators():
    My new data type is just typedefed one
    typedef std::vector< std::vector<int> > myData;
    , Sorry but I did not get how implementing this operators related to my problem.
    Last edited by prasad_N; 29th December 2015 at 19:36.
    Thanks :-)

  7. #7
    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: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by prasad_N View Post
    Changing my connect function to Qt:irectConnection worked out, I hope it will not create any problem as this is going to start my thread.
    Is the receiver objects in a different thread?
    If so, is the slot properly taking care of the concurrent access if there is one?
    Why have references at all?

    Cheers,
    _

  8. #8
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by anda_skoa View Post
    Is the receiver objects in a different thread?
    If so, is the slot properly taking care of the concurrent access if there is one?
    Why have references at all?

    Cheers,
    _
    Yes its in different thread. I am fetching few records from data base & playing with it main thread besides I am launching a thread parallel which fetches all the data from data base in thread.
    Why reference: I need the data in my class (from where I created my thread & start) so I am sending a vector of vector as reference & In thread it fetches all the data once my thread finishes I have all the data in my class, I am using reference here just to avoid one more finished signal with the data as argument (to avoid copy).
    Thanks :-)

  9. #9
    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: qRegisterMetaType- reference as argument in connect throwing run time error

    In your example both sender and receiver are "this" so if you don't want to copy, why not just have the data as a member?

    In any case, if you really must allocate the data structure on the main thread, then hand it to the secondary thread, you might want to do that via pointers, the reference cannot be stored, so even with a direct connection it will only be available in the slot, not in any code executed by the other thread.

    Still a very strange pattern, maybe some more code would clarify what you are actually trying to do?

    Cheers,
    _

  10. #10
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by anda_skoa View Post
    In your example both sender and receiver are "this" so if you don't want to copy, why not just have the data as a member?
    Still a very strange pattern, maybe some more code would clarify what you are actually trying to do?_
    There was a typo in my question (i directly copied from my project code & while changing it I messed up) & here is the simplified code


    Qt Code:
    1. Ex:
    2.  
    3. typedef std::vector< std::vector<int> > myData;
    4. qRegisterMetaType<myData>("myData");
    5.  
    6. class testClass // I want to pass this obj as reference
    7. {
    8. public:
    9. int a;
    10. };
    11.  
    12.  
    13. //In my main thread (myclass) : QThread m_thread & workerClass m_worker are the class members & below is constructor code
    14.  
    15. connect(&m_thread, SIGNAL(started()), this, SLOT(startThread()));
    16. connect(this, SIGNAL(startMyThread(myData&)), &m_worker, SLOT(heavyWork(myData&)));
    17. m_worker.moveToThread(&m_thread);
    18. m_thread.start();
    19.  
    20. void myclass::startThread()
    21. {
    22. myData obj;
    23.  
    24. emit startMyThread(obj);
    25. }
    To copy to clipboard, switch view to plain text mode 


    So here I am getting same error:
    QObject::connect: Cannot queue arguments of type 'myData&'
    (Make sure 'myData&' is registered using qRegisterMetaType().)
    when I make it direct connection (connect(this, SIGNAL(startMyThread(myData&)), &m_worker, SLOT(heavyWork(myData&)), Qt:irectConnection) , the slot is getting executed in the main thread.

    i am just looking for a way to pass reference to the thread. i can do it with the pointer as you suggested, But as I am using vector of vector, using pointer to the vector of vector may complicate or confuse or may lead to some memory leaks which I don't want & exactly for the same reason we have reference.
    Last edited by prasad_N; 30th December 2015 at 18:40.
    Thanks :-)

  11. #11
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Sorry but I did not get how implementing this operators related to my problem.
    You may not need them; however, if your data needs to marshalled across a thread boundary you might need them if they aren't already defined for std:: vector<>

  12. #12
    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: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by prasad_N View Post
    i am just looking for a way to pass reference to the thread. i can do it with the pointer as you suggested, But as I am using vector of vector, using pointer to the vector of vector may complicate or confuse or may lead to some memory leaks which I don't want & exactly for the same reason we have reference.
    Well, right now, if it would work, you would have the thread access a destroyed object.
    I would consider that even worse than a memory leak :-)

    Cheers,
    _

  13. #13
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by anda_skoa View Post
    Well, right now, if it would work, you would have the thread access a destroyed object.
    I would consider that even worse than a memory leak :-)_
    Any how passing reference to thread is not working now, is there a particular reason for this ?
    Can we request Qt guys to have this feature in future versions ??
    Thanks :-)

Similar Threads

  1. Replies: 0
    Last Post: 19th January 2012, 16:52
  2. Replies: 2
    Last Post: 9th May 2011, 11:38
  3. Replies: 6
    Last Post: 9th April 2011, 20:23
  4. qRegisterMetaType - problems with connect()
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 23:55
  5. Replies: 2
    Last Post: 8th October 2007, 16:02

Tags for this Thread

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.