Results 1 to 12 of 12

Thread: emitting QSharedPointer

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default emitting QSharedPointer

    Hi,
    I got a compiler error when i tried to emit QSharedPointer
    QObject::connect: Cannot queue arguments of type 'QSharedPointer<MyClass>'. So i tried this

    Q_DECLARE_METATYPE(QSharedPointer<MyClass> >); and
    qRegisterMetaType<QSharedPointer<MyClass> > ("MyClassSharedPointer");

    But still i get the warning. How to register the QSharedPointer class.

  2. #2
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: emitting QSharedPointer

    Try:

    Qt Code:
    1. ...
    2. typedef QSharedPointer<myClass> myClassSharedPointer;
    3. Q_DECLARE_METATYPE(myClassSharedPointer);
    4. ...
    5. qRegisterMetaType<myClassSharedPointer>("myClassSharedPointer");
    6. ...
    To copy to clipboard, switch view to plain text mode 

    That should do it for you.

    [EDIT] Grammar correction

  3. The following user says thank you to TemporalBeing for this useful post:

    babu198649 (24th July 2010)

  4. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: emitting QSharedPointer

    no even this fails, i am calling
    Qt Code:
    1. qRegisterMetaType<myClassSharedPointer>("myClassSharedPointer");
    To copy to clipboard, switch view to plain text mode 
    in my MainWindow constructor.
    and i have put the lines

    Qt Code:
    1. typedef QSharedPointer<myClass> myClassSharedPointer;
    2. Q_DECLARE_METATYPE(myClassSharedPointer);
    To copy to clipboard, switch view to plain text mode 

    in the myclass.h file after the class declaration.

    is it fine?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: emitting QSharedPointer

    What is the point of emitting a QSharedPointer?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: emitting QSharedPointer

    Quote Originally Posted by wysota View Post
    What is the point of emitting a QSharedPointer?
    The worker thread reads the files computes the data allocates and fills the memory with data, wraps it in QSharedPointer and passes it to the mainThread, which is used for plotting.
    The problem of owner ship of the memory is avoided.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: emitting QSharedPointer

    And how are you protecting the data structure inside the pointer object from being accessed simoultaneously from multiple threads? Does MyClass inherit QObject?

    As for ownership - there is no problem of ownership here, if your class is just data that can be copied - simply don't use pointers but rather use objects (possibly based on QSharedData and friends).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: emitting QSharedPointer

    MyClass does not inherit anything. It just contains some variables and pointer which points to the dynamically allocated memory locations which are deleted in ~MyClass. Only the worker thread will write to the memory, the main thread will only read the memory area(although it is possible to write).

    QSharedData and QSharedDataPointer requries comparitavely lot of work to be done(subclassing base class, implementing copy constructor, e.t.c) than using QSharedPointer which needs very less coding.

  9. #8
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: emitting QSharedPointer

    Quote Originally Posted by TemporalBeing View Post
    Try:

    Qt Code:
    1. ...
    2. typedef QSharedPointer<myClass> myClassSharedPointer;
    3. Q_DECLARE_METATYPE(myClassSharedPointer);
    4. ...
    5. qRegisterMetaType<myClassSharedPointer>("myClassSharedPointer");
    6. ...
    To copy to clipboard, switch view to plain text mode 

    That should do it for you.

    [EDIT] Grammar correction
    Thanks, it actually works. The problem earlier was i did not used the typdefed name in the slot and signal arguments.

    I tried without using Q_DECLARE_METATYPE, still it worked.

  10. #9
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: emitting QSharedPointer

    I write example, all works fine:

    Qt Code:
    1. QT -= gui
    2.  
    3. TARGET = QSharedPointerQueue
    4. CONFIG += console
    5. CONFIG -= app_bundle
    6.  
    7. TEMPLATE = app
    8.  
    9. HEADERS += main.cpp
    10. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtCore/QMetaType>
    3. #include <QtCore/QSharedPointer>
    4. #include <QtCore/QTextStream>
    5.  
    6. struct MyStruct
    7. {
    8. QString message;
    9. };
    10.  
    11. Q_DECLARE_METATYPE(QSharedPointer<MyStruct>)
    12.  
    13. static void myStructDeleter(MyStruct* ms)
    14. {
    15. QTextStream(stdout)
    16. << "Do delete MyStruct with message: "
    17. << ms->message
    18. << endl;
    19. delete ms;
    20. }
    21.  
    22. class MyObject : public QObject
    23. {
    24. Q_OBJECT
    25. public:
    26. explicit MyObject(QObject* parent = 0) : QObject(parent) {}
    27. virtual ~MyObject() {}
    28.  
    29. void sendMessage(const QString& message)
    30. {
    31. MyStruct* ms = new MyStruct;
    32. ms->message = message;
    33. emit sendPointer(QSharedPointer<MyStruct>(ms, myStructDeleter));
    34. }
    35.  
    36. signals:
    37. void sendPointer(QSharedPointer<MyStruct>);
    38. };
    39.  
    40. class MyObjectTwo : public QObject
    41. {
    42. Q_OBJECT
    43. public:
    44. explicit MyObjectTwo(QObject* parent = 0) : QObject(parent) {}
    45. virtual ~MyObjectTwo() {}
    46.  
    47. public slots:
    48. void pointerReceiver(QSharedPointer<MyStruct> ptr)
    49. {
    50. QTextStream(stdout)
    51. << ptr->message
    52. << "(" << Q_FUNC_INFO << ")"
    53. << endl;
    54. QCoreApplication::exit(0);
    55. }
    56. };
    57.  
    58. int main(int argc, char* argv[])
    59. {
    60. QCoreApplication a(argc, argv);
    61.  
    62. qRegisterMetaType<QSharedPointer<MyStruct> >();
    63. MyObject mobj;
    64. MyObjectTwo mobj2;
    65.  
    66. QObject::connect(&mobj, SIGNAL(sendPointer(QSharedPointer<MyStruct>)),
    67. &mobj2, SLOT(pointerReceiver(QSharedPointer<MyStruct>)),
    68. Qt::QueuedConnection);
    69.  
    70. mobj.sendMessage("Goodbye, cruel world!");
    71.  
    72. return a.exec();
    73. }
    74.  
    75. #include "moc_main.cpp"
    To copy to clipboard, switch view to plain text mode 

    P.S.: Without Q_DECLARE_METATYPE compilator issue errors, so just try recompile project remove moc_* files first.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: emitting QSharedPointer

    Quote Originally Posted by babu198649 View Post
    QSharedData and QSharedDataPointer requries comparitavely lot of work to be done(subclassing base class, implementing copy constructor, e.t.c)
    Well, yeah... it's like... 10 lines of code
    than using QSharedPointer which needs very less coding.
    And much more debugging...

    But QSharedPointer doesn't protect you from multithreaded access. If you write to an area of memory with one thread and at the same time read the same area in another thread there is a good chance you will get garbage on the reading side. QSharedPointer is not a solution here. It only prevents you from having to call delete on the object if you are carefull with how you access the object. But this is not your main concern here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: emitting QSharedPointer

    Quote Originally Posted by wysota View Post
    Well, yeah... it's like... 10 lines of code
    But QSharedPointer doesn't protect you from multithreaded access. If you write to an area of memory with one thread and at the same time read the same area in another thread there is a good chance you will get garbage on the reading side. QSharedPointer is not a solution here. It only prevents you from having to call delete on the object if you are carefull with how you access the object. But this is not your main concern here.
    Why not? QSharedPointer is thread safe.

    Thread-Safety

    QSharedPointer and QWeakPointer are thread-safe and operate atomically on the pointer value. Different threads can also access the same QSharedPointer or QWeakPointer object at the same time without need for locking mechanisms.

    It should be noted that, while the pointer value can be accessed in this manner, QSharedPointer and QWeakPointer provide no guarantee about the object being pointed to. Thread-safety and reentrancy rules for that object still apply.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: emitting QSharedPointer

    Quote Originally Posted by SABROG View Post
    Why not? QSharedPointer is thread safe.
    As you quoted yourself:

    It should be noted that, while the pointer value can be accessed in this manner, QSharedPointer and QWeakPointer provide no guarantee about the object being pointed to. Thread-safety and reentrancy rules for that object still apply.
    QSharedPointer doesn't release you from care about thread synchronization regarding the data carried by the smart pointer. It only guarantees that operations on the pointer itself are atomic.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 23rd July 2010, 03:41
  2. QSharedPointer of type void
    By HERC in forum Qt Programming
    Replies: 0
    Last Post: 12th May 2010, 12:59
  3. Problem with QSharedPointer
    By weaver4 in forum Newbie
    Replies: 2
    Last Post: 19th April 2010, 14:22
  4. QSharedPointer - how to prevent delete?
    By Piskvorkar in forum Qt Programming
    Replies: 7
    Last Post: 31st March 2010, 15:46
  5. QSharedPointer vs Boost::tr1::shared_ptr()
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 16:48

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.