Page 1 of 2 12 LastLast
Results 1 to 20 of 21

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

  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 :-)

  14. #14
    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
    Any how passing reference to thread is not working now, is there a particular reason for this ?
    Passing a reference is possible, just not via a queued connection.

    A queued connection needs to transfer the data via an event, so each signal argument needs to be stored in a QVariant and then put into an event that is sent to the receiver via the receiver thread's event loop.
    There the arguments are extraced again and the slot is called.

    Aside from references being very in appropriate due to implicit life time assumption (easily leading to destroy before use like in your code), they cannot be stored (neither in a QVariant but also not in a normal member variable).

    Quote Originally Posted by prasad_N View Post
    Can we request Qt guys to have this feature in future versions ??
    What feature?
    To store a reference like a pointer? That you would have to take up with the C++ standard comittee.

    Cheers,
    _

  15. #15
    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
    you would have the thread access a destroyed object._
    you are referring to below function I think & myData obj; is a class member in my actual code & It never gets destructed before thread finishes. again this is due to writing some code on fly sorry for that.

    Qt Code:
    1. void myclass::startThread()
    2. {
    3. myData obj; //this is class member in actual code & never gets destructed before thread finishes, I just wanted to show the reference variable problem here so I just didn't care this
    4.  
    5. emit startMyThread(obj);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by anda_skoa View Post
    Passing a reference is possible, just not via a queued connection._
    So what connect should I use, Direct connection making slot to execute in main thread. I think other connections more or less same as these 2 connections except unique connection.

    Quote Originally Posted by anda_skoa View Post
    What feature?
    To store a reference like a pointer? That you would have to take up with the C++ standard comittee._
    passing reference to thread when It is queued connection if there is no other way.
    Thanks :-)

  16. #16
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    myData obj is destroyed immediately after emiting signal. Slot in another thread is run in the future when the object referenced by reference no longer exists.

  17. #17
    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 Lesiok View Post
    myData obj is destroyed immediately after emiting signal. Slot in another thread is run in the future when the object referenced by reference no longer exists.
    That is what I clarified in the above reply, myData obj is actually class member of myclass in my original code. It never gets destroyed before worker thread finishes its work.
    Thanks :-)

  18. #18
    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
    you are referring to below function I think & myData obj; is a class member in my actual code & It never gets destructed before thread finishes. again this is due to writing some code on fly sorry for that.
    Then you should probably not have used a local variable definition.

    Quote Originally Posted by prasad_N View Post
    So what connect should I use, Direct connection making slot to execute in main thread. I think other connections more or less same as these 2 connections except unique connection.
    It depends on your requirements.
    If you want the slot to execute in the context of the emitter thread, use a direct connection.
    If you want to use the signal/slot bridge as a mean to conveniently copy data between threads, use a queued connection.


    Quote Originally Posted by prasad_N View Post
    passing reference to thread when It is queued connection if there is no other way.
    I wouldn't get my hope up. If the C++ Standard committee wanted to allow assigment of reference variables, that would have been possible a long time ago, especially since that has always been possible for pointers.

    I.e. if C programmers want to refer to the same object from two different variables (aliasing), they use pointers.
    C++ programmers can also use pointers and can use references for the special case where the alias can be initialized with the address of the object, e.g. passing a reference to a constructor, storing it in a reference member.

    Cheers,
    _

  19. #19
    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

    Then you should probably not have used a local variable definition.
    @prasad_N:
    Or at least written it as a commented-out line, with an explanation that "obj" is a class member variable. How are we to understand that when you write it as you did, you weren't posting your actual code? A lot of the discussion here was based on your incorrect posting, and it could have been avoided if you had posted the actual code in the first place.

    C++ programmers can also use pointers and can use references for the special case where the alias can be initialized with the address of the object, e.g. passing a reference to a constructor, storing it in a reference member.
    And this has the added benefit over storing a pointer member variable in that you are absolutely assured that the member variable is a valid reference to an object instance, whereas anything can be assigned to a pointer member, including nothing.

  20. #20
    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
    A lot of the discussion here was based on your incorrect posting, and it could have been avoided if you had posted the actual code in the first place.
    There was a mistake & people are over emphasizing on that rather than a actual question, Anyhow I will take care from next time while posting.


    Quote Originally Posted by anda_skoa View Post
    It depends on your requirements.
    If you want the slot to execute in the context of the emitter thread, use a direct connection.
    If you want to use the signal/slot bridge as a mean to conveniently copy data between threads, use a queued connection.
    _
    None of them are not my requirements. As per my requirement & Replay's from you guys I think there is no solution for this at least for now (passing reference across threads if it it is queued connection).
    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.