Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QMimeData: Serializing custom Object

[SOLVED] QMimeData: Serializing custom Object

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    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!


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on last edited by
      #2

      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


      Free Indoor Cycling Software - https://maximumtrainer.com

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        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);@


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maximus
            wrote on last edited by
            #5

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

            ...
            };@


            Free Indoor Cycling Software - https://maximumtrainer.com

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved