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 copy a QString value to a struct
Forum Updated to NodeBB v4.3 + New Features

how to copy a QString value to a struct

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 2.6k Views
  • 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.
  • ManiRonM ManiRon

    @aha_1980 its a variable in which i will store the values read from the data transmitted from server side

    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @ManiRon Yeah, but what are it's contents? Is it printable? Then how does it look?

    Qt has to stay free or it will die.

    ManiRonM 1 Reply Last reply
    0
    • aha_1980A aha_1980

      @ManiRon Yeah, but what are it's contents? Is it printable? Then how does it look?

      ManiRonM Offline
      ManiRonM Offline
      ManiRon
      wrote on last edited by
      #5

      @aha_1980

      it is contains a normal data which can be printed like for example(1,2,3) like these values it has

      aha_1980A 1 Reply Last reply
      0
      • ManiRonM ManiRon

        @aha_1980

        it is contains a normal data which can be printed like for example(1,2,3) like these values it has

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @ManiRon If you cannot give us a concrete full example then we cannot give you a concrete answer. Sorry

        Qt has to stay free or it will die.

        ManiRonM 2 Replies Last reply
        2
        • aha_1980A aha_1980

          @ManiRon If you cannot give us a concrete full example then we cannot give you a concrete answer. Sorry

          ManiRonM Offline
          ManiRonM Offline
          ManiRon
          wrote on last edited by
          #7

          @aha_1980

          i am not understanding and how to explain it . its a simple QString variable and i store the read data in that variable and i want to copy that values to the struct which i have mentioned above .

          ManiRonM jsulmJ 2 Replies Last reply
          0
          • ManiRonM ManiRon

            @aha_1980

            i am not understanding and how to explain it . its a simple QString variable and i store the read data in that variable and i want to copy that values to the struct which i have mentioned above .

            ManiRonM Offline
            ManiRonM Offline
            ManiRon
            wrote on last edited by
            #8
            This post is deleted!
            1 Reply Last reply
            0
            • aha_1980A aha_1980

              @ManiRon If you cannot give us a concrete full example then we cannot give you a concrete answer. Sorry

              ManiRonM Offline
              ManiRonM Offline
              ManiRon
              wrote on last edited by VRonin
              #9

              @aha_1980

               QDataStream in(tcpSocket);
                  //in.setVersion(QDataStream::Qt_5_10);
                  for (;;)
                  {
                      if (!m_nNextBlockSize)
                      {
                          if (tcpSocket->bytesAvailable() < sizeof(quint16)) { break; }
                          in >> m_nNextBlockSize;
                      }
              
                      if (tcpSocket->bytesAvailable() < m_nNextBlockSize) { break; }
              
                      QString str; in >> str;
                      
                      
              
                      if (str == "0")
                      {
                          str = "Connection closed";
                          closeConnection();
                      }
              
                      emit hasReadSome(str);
                      m_nNextBlockSize = 0;
                  }
              } 
              

              and i want to copy the QString str contained values to the struct given above

              J.HilkJ 1 Reply Last reply
              0
              • ManiRonM ManiRon

                @aha_1980

                i am not understanding and how to explain it . its a simple QString variable and i store the read data in that variable and i want to copy that values to the struct which i have mentioned above .

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @ManiRon said in how to copy a QString value to a struct:

                i am not understanding and how to explain it

                Can't you simply show an example of such a string?
                Also, why do you convert the byte array to a string first?
                The way it usually works is: implement << and >> operators for your structure (for QDataStream, https://doc.qt.io/qt-5/qdatastream.html). Then you can simply do it like:

                TxCommandPacket packet;
                in >> packet;
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                6
                • ManiRonM ManiRon

                  @aha_1980

                   QDataStream in(tcpSocket);
                      //in.setVersion(QDataStream::Qt_5_10);
                      for (;;)
                      {
                          if (!m_nNextBlockSize)
                          {
                              if (tcpSocket->bytesAvailable() < sizeof(quint16)) { break; }
                              in >> m_nNextBlockSize;
                          }
                  
                          if (tcpSocket->bytesAvailable() < m_nNextBlockSize) { break; }
                  
                          QString str; in >> str;
                          
                          
                  
                          if (str == "0")
                          {
                              str = "Connection closed";
                              closeConnection();
                          }
                  
                          emit hasReadSome(str);
                          m_nNextBlockSize = 0;
                      }
                  } 
                  

                  and i want to copy the QString str contained values to the struct given above

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #11

                  @ManiRon this only works, because QString haccepts a QByteArray as a Constructor/DataStream overload.

                  Are you really sure you want to save the data in a QString and not in a QByteArray ?
                  a QString will terminate if the QByteArray contains char it can not represent.

                  That said, there are multiple ways to skin this cat.

                  • give your struct a QDataStream operator laid @jsulm said.

                  • copy the raw data form the string or QbyteArray into the allocated char array

                  struct Foo{
                      int a;
                      int b;
                      char data [500];
                  };
                  
                      QString s ("Hello World");
                      Foo bar;
                  
                      memcpy(&bar.data, s.data(),s.size()*2);
                  
                  • or if you wan't to go really old school:
                      QString s ("Hello World");
                      Foo bar;
                  
                      memcpy(reinterpret_cast<char*>(&bar) + sizeof(int)*2, s.data(),s.size()*2);
                  

                  just the 3 way's I'm thinking of right now.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  aha_1980A 1 Reply Last reply
                  2
                  • J.HilkJ J.Hilk

                    @ManiRon this only works, because QString haccepts a QByteArray as a Constructor/DataStream overload.

                    Are you really sure you want to save the data in a QString and not in a QByteArray ?
                    a QString will terminate if the QByteArray contains char it can not represent.

                    That said, there are multiple ways to skin this cat.

                    • give your struct a QDataStream operator laid @jsulm said.

                    • copy the raw data form the string or QbyteArray into the allocated char array

                    struct Foo{
                        int a;
                        int b;
                        char data [500];
                    };
                    
                        QString s ("Hello World");
                        Foo bar;
                    
                        memcpy(&bar.data, s.data(),s.size()*2);
                    
                    • or if you wan't to go really old school:
                        QString s ("Hello World");
                        Foo bar;
                    
                        memcpy(reinterpret_cast<char*>(&bar) + sizeof(int)*2, s.data(),s.size()*2);
                    

                    just the 3 way's I'm thinking of right now.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @J.Hilk you are aware that QChar is 16 bit, so QString::size() is only half the memory it uses...

                    Anyway, the OP don't want QString, rather QByteArray.

                    Qt has to stay free or it will die.

                    J.HilkJ 1 Reply Last reply
                    2
                    • aha_1980A aha_1980

                      @J.Hilk you are aware that QChar is 16 bit, so QString::size() is only half the memory it uses...

                      Anyway, the OP don't want QString, rather QByteArray.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #13

                      @aha_1980 said in how to copy a QString value to a struct:

                      @J.Hilk you are aware that QChar is 16 bit, so QString::size() is only half the memory it uses...

                      thanks for clarifying that I was looking through the documentation, scratching my head, why my example project only copied "Hello " back!

                      I'll fix my post!


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      ManiRonM 1 Reply Last reply
                      1
                      • J.HilkJ J.Hilk

                        @aha_1980 said in how to copy a QString value to a struct:

                        @J.Hilk you are aware that QChar is 16 bit, so QString::size() is only half the memory it uses...

                        thanks for clarifying that I was looking through the documentation, scratching my head, why my example project only copied "Hello " back!

                        I'll fix my post!

                        ManiRonM Offline
                        ManiRonM Offline
                        ManiRon
                        wrote on last edited by
                        #14
                        This post is deleted!
                        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