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. How to send QMap over socket?
Forum Updated to NodeBB v4.3 + New Features

How to send QMap over socket?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 952 Views 3 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.
  • Y Offline
    Y Offline
    YouKnowMe
    wrote on last edited by
    #1

    Hello everybody,

    I have a QMap object:

    QMap<QString, int> map; 
    

    and I would like to send it over TCP socket. Is there a way to do that somehow?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2
      QDataStream stream(socket);
      stream << map;
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      10
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        @VRonin said in How to send QMap over socket?:

        stream << map;

        Did not expect that. Apparently a lot of Qt objects have serialization operators defined for them. Mind blown! Really, really, really cool!

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        5
        • A Offline
          A Offline
          arsinte_andrei
          wrote on last edited by
          #4

          to send as QByteArray

          QByteArray atpCommandCentre::messageToQByteArray(const QMap<QString, int> data)  {
          	qDebug() <<this << "messageToQByteArray" << data;
          	QByteArray ar;
          	QDataStream out(&ar,QIODevice::WriteOnly); //Serializing
          	out << data; // write the data
          
          	return ar;
          }
          

          On Server or where you have to read it back to QMap

          QMap<QString, int> atpCommandCentre::messageFromQByteArray(QByteArray data) {
          	qDebug() <<this << "messageFromQByteArray" << data;
          	QMap<QString, int> myTmpMessage;
          	QDataStream in(&data, QIODevice::ReadOnly); //Deserializing
          	in >> myTmpMessage;
          
          	return myTmpMessage;
          }
          

          Hope that help you

          Y 1 Reply Last reply
          3
          • A Offline
            A Offline
            arsinte_andrei
            wrote on last edited by
            #5

            Anyway, I do think that this post can be safely marked as Solved now, in order for others to know that it has been solved.

            1 Reply Last reply
            0
            • A arsinte_andrei

              to send as QByteArray

              QByteArray atpCommandCentre::messageToQByteArray(const QMap<QString, int> data)  {
              	qDebug() <<this << "messageToQByteArray" << data;
              	QByteArray ar;
              	QDataStream out(&ar,QIODevice::WriteOnly); //Serializing
              	out << data; // write the data
              
              	return ar;
              }
              

              On Server or where you have to read it back to QMap

              QMap<QString, int> atpCommandCentre::messageFromQByteArray(QByteArray data) {
              	qDebug() <<this << "messageFromQByteArray" << data;
              	QMap<QString, int> myTmpMessage;
              	QDataStream in(&data, QIODevice::ReadOnly); //Deserializing
              	in >> myTmpMessage;
              
              	return myTmpMessage;
              }
              

              Hope that help you

              Y Offline
              Y Offline
              YouKnowMe
              wrote on last edited by YouKnowMe
              #6

              @arsinte_andrei when I qDebug "out" variable, it says: QVariant(Invalid)... is that ok? I have some problems receiving the data, so I'm trying to find out whats wrong step by step.

              VRoninV A 2 Replies Last reply
              0
              • Y YouKnowMe

                @arsinte_andrei when I qDebug "out" variable, it says: QVariant(Invalid)... is that ok? I have some problems receiving the data, so I'm trying to find out whats wrong step by step.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                @YouKnowMe said in How to send QMap over socket?:

                when I qDebug "out" variable, it says: QVariant(Invalid)

                What is out?

                if the one socket sends:

                QDataStream stream(socket);
                stream << map;
                

                the receiving end should be something like:

                // slot connected to QTcpSocket::readyRead
                QDataStream socketStream(socket);
                QMap<QString, int> data;
                for (;;) {
                socketStream.startTransaction();
                socketStream >> data;
                if (socketStream.commitTransaction()) 
                qDebug() << "Received " << data;
                else
                break;
                }
                

                See ChatClient::onReadyRead of this example for an explanation of what that code is doing

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                3
                • Y YouKnowMe

                  @arsinte_andrei when I qDebug "out" variable, it says: QVariant(Invalid)... is that ok? I have some problems receiving the data, so I'm trying to find out whats wrong step by step.

                  A Offline
                  A Offline
                  arsinte_andrei
                  wrote on last edited by
                  #8

                  @YouKnowMe
                  so... first of all, how do you use my functions??
                  it should be something like that
                  pseudo code

                  QMap<QString, int> myMap
                  myMap.insert("one", 1);
                  myMap.insert("two", 2);
                  myMap.insert("three", 3);
                  
                  QByteArray myMessageToSend = messageToQBuyteArray(myMap);
                  qDebug() << myMessageToSend; // this has to work - this is the bytes that you are sending
                  
                  

                  anyway, if what you try is not working please post a bit of code to understand better what are you trying to do

                  1 Reply Last reply
                  1

                  • Login

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