[SOLVED] QMimeData: Serializing custom Object



  • I need to create non-member operators to Serialize my custom Object in the Stream (<< and >>) I am a bit stuck and mostly due to the C++ syntax that I don't get.

    From what I read "here":http://stackoverflow.com/questions/2314078/passing-data-around-with-qmimedata-in-qt-drag-and-drop
    You have to define the non-member operators outside the class, so I created another class "StreamMyObject" that is a friend of my custom object so that I can access it's private member.

    After many tries, i'm still unable to compile that class, C4430 missing type specifier on both of my operator function in StreamMyObject.h
    Thanks for your help Qt!



  • I found a good place to learn about non-member function:
    good exercice here:
    http://www.cplusplus.com/forum/general/73983/

    So here is what I did

    1-Added outside of Interval.h bracket this function
    @Class Interval {};
    QDataStream& operator<<(QDataStream& out, Interval& interval);@

    2- Coded in Interval.cpp
    @QDataStream& operator<<(QDataStream& stream, Interval& obj) {

    qDebug() << "OPERATOR SERIALIZE CALLED!";
    
    /* as long as first_member and second_member are types supported
       by QDataStream, I can serialize them directly.  If they're not
       supported, I'd need an operator for them as well unless I can
       convert them to a QString or something else supported by Qt /
       QDataStream */
    
    stream << obj.getDurationQTime();
    

    // stream << obj.type;
    // stream << obj.displayMessage;

    //TOFIX: how to access private member? can't put friend on the function as it is outside class..

    return stream;
    

    }@

    I still got to figure how to access private member of Interval, or i'll have to create getters/setters for all members



  • If you want your function to have access to your private members, declare it this way:

    Inside Interval.h, in the public function
    @friend QDataStream& operator<<(QDataStream& out, const Interval& interval);@


  • Lifetime Qt Champion

    Hi,

    Why do you need to do 1. ? That sounds like a strange workaround



  • Hi SGaist,

    I added the function directly in Interval class and it works now.
    I didn't know what I was doing mostly, no reason :)

    @class Interval
    {
    public:

    Interval();
    Interval( const Interval& other );
    bool operator==(const Interval &other) const;
    
    friend QDataStream& operator<<(QDataStream& out, const Interval& interval);
    friend QDataStream& operator>>(QDataStream& out, Interval& interval);
    

    ...
    };@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.