Results 1 to 8 of 8

Thread: QVariant, QtDBus, pointers & custom classes

  1. #1
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QVariant, QtDBus, pointers & custom classes

    Hi, I need to send a pointer to a custom class (a model derived from QSqlRelationalTableModel) over the DBus. What I already know:

    - I can incapsulate an arbitrary object into a QVariant instance.
    - I can incapsulate a pointer to an arbitrary object into a QVariant instance.
    - I can save and restore this object if I implement << and >> operators (with QDataStream argument).
    - I can incapsulate a QVariant into a QDBusVariant if I need to send it over the DBus.
    - I can send an arbitrary object over the DBus if I implement << and >> operators (with QDBusArgument argument).

    So, how can I do this?

    My idea is:

    1) Add Q_DECLARE_METATYPE(MyClass) and Q_DECLARE_METATYPE(MyClass*) at the end of MyClass declaration.
    2) Implement QDataStream &operator<<(QDataStream &out, const MyClass &myObj) and QDataStream &operator>>(QDataStream &in, MyClass &myObj) operators.
    3) Implement QDBusArgument &operator<<(QDBusArgument &out, const MyClass &myObj) and QDBusArgument &operator>>(QDBusArgument &in, MyClass &myObj) operators.
    4) Implement MyClass(), MyClass(const MyClass& obj), ~MyClass() c/dtors.
    5) Add qDBusRegisterMetaType<MyClass>() call into all ctors.

    With these steps, can I write something like this (if we suppose remote and local objects are in the same process)?

    Qt Code:
    1. // Remote object
    2. class MyQDBusAdaptor
    3. {
    4. // ...
    5. MyClass* my_method();
    6. }
    7.  
    8. // Local object
    9. QDBusInterface app("...", "...", "...");
    10. MyClass* ret = app->call("my_method");
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVariant, QtDBus, pointers & custom classes

    Up! I think this is a interesting duscussion topic...

  3. #3
    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: QVariant, QtDBus, pointers & custom classes

    Quote Originally Posted by zuck View Post
    Hi, I need to send a pointer to a custom class (a model derived from QSqlRelationalTableModel) over the DBus.
    Forget it, you can't pass pointers between processes as each process has its own memory space. The same address can point to two different places in physical memory in different processes.
    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.


  4. #4
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVariant, QtDBus, pointers & custom classes

    Quote Originally Posted by wysota View Post
    Forget it, you can't pass pointers between processes as each process has its own memory space. The same address can point to two different places in physical memory in different processes.
    Yes, I know. In fact this is the point: how can I pass a complex object over the DBUS?

    In my specific case I can pass only the SQL driver name, the database name, the connection name and the table name and then construct the class instance with this data, but if I can't send a pointer I can't send, for example, a QWidget instance or similar...is it correct?

    I ask for this because I want to centralize in a service plugin the access to a specific model class from all the user plugins of my framework (they run in the same process). In the user plugin code there are the views and in the service plugin code there is the model access management. Now I want to connect the views with the model over the DBus (each plugin is decoupled with the rest of the framework).

  5. #5
    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: QVariant, QtDBus, pointers & custom classes

    You can't transfer any complex object between processes directly. You have to serialize the object, transfer it as a series of bytes and then recreate the object from those bytes in the other process.
    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. #6
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVariant, QtDBus, pointers & custom classes

    Do you think using QSharedMemory could be a good solution for this?

  7. #7
    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: QVariant, QtDBus, pointers & custom classes

    Could be but still you can't just place the object in shared memory in one process and take it from there in the other one. You have to operate on a per-byte level and reconstruct the object properly (starting from calling its constructor) on the other side.
    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. #8
    Join Date
    Mar 2009
    Posts
    72
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVariant, QtDBus, pointers & custom classes

    Ok, thank you!

Similar Threads

  1. Replies: 2
    Last Post: 18th June 2013, 11:31
  2. Streaming custom QVariant?
    By brcain in forum Qt Programming
    Replies: 12
    Last Post: 22nd May 2009, 21:15
  3. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 10:07
  4. QVariant types support for custom properties
    By Dusdan in forum Qt Tools
    Replies: 9
    Last Post: 11th January 2006, 10:55

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.