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 Update on Monday, May 27th 2025

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.
  • M Offline
    M Offline
    ManiRon
    wrote on 19 Feb 2019, 04:13 last edited by
    #1

    how to copy the data stored in a QString variable to the below structure ?

    typedef struct{
    int iTestMode;
    int iTestName;
    char arrConfigurationData[500];
    int iSizeOfData;
    int iChecksum;
    }TxCommandPacket;

    A 1 Reply Last reply 19 Feb 2019, 04:21
    0
    • M ManiRon
      19 Feb 2019, 04:13

      how to copy the data stored in a QString variable to the below structure ?

      typedef struct{
      int iTestMode;
      int iTestName;
      char arrConfigurationData[500];
      int iSizeOfData;
      int iChecksum;
      }TxCommandPacket;

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 19 Feb 2019, 04:21 last edited by
      #2

      @ManiRon How does your QString look like?

      Qt has to stay free or it will die.

      M 1 Reply Last reply 19 Feb 2019, 04:22
      2
      • A aha_1980
        19 Feb 2019, 04:21

        @ManiRon How does your QString look like?

        M Offline
        M Offline
        ManiRon
        wrote on 19 Feb 2019, 04:22 last edited by
        #3

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

        A 1 Reply Last reply 19 Feb 2019, 04:26
        0
        • M ManiRon
          19 Feb 2019, 04:22

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

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 19 Feb 2019, 04:26 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.

          M 1 Reply Last reply 19 Feb 2019, 04:28
          0
          • A aha_1980
            19 Feb 2019, 04:26

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

            M Offline
            M Offline
            ManiRon
            wrote on 19 Feb 2019, 04:28 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

            A 1 Reply Last reply 19 Feb 2019, 04:36
            0
            • M ManiRon
              19 Feb 2019, 04:28

              @aha_1980

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

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 19 Feb 2019, 04:36 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.

              M 2 Replies Last reply 19 Feb 2019, 04:38
              2
              • A aha_1980
                19 Feb 2019, 04:36

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

                M Offline
                M Offline
                ManiRon
                wrote on 19 Feb 2019, 04:38 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 .

                M J 2 Replies Last reply 19 Feb 2019, 04:43
                0
                • M ManiRon
                  19 Feb 2019, 04:38

                  @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 .

                  M Offline
                  M Offline
                  ManiRon
                  wrote on 19 Feb 2019, 04:43 last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • A aha_1980
                    19 Feb 2019, 04:36

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

                    M Offline
                    M Offline
                    ManiRon
                    wrote on 19 Feb 2019, 04:47 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 19 Feb 2019, 05:37
                    0
                    • M ManiRon
                      19 Feb 2019, 04:38

                      @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 .

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 19 Feb 2019, 05:14 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
                      • M ManiRon
                        19 Feb 2019, 04:47

                        @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 19 Feb 2019, 05:37 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.

                        A 1 Reply Last reply 19 Feb 2019, 06:42
                        2
                        • J.HilkJ J.Hilk
                          19 Feb 2019, 05:37

                          @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.

                          A Offline
                          A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on 19 Feb 2019, 06:42 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 19 Feb 2019, 06:45
                          2
                          • A aha_1980
                            19 Feb 2019, 06:42

                            @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 19 Feb 2019, 06:45 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.

                            M 1 Reply Last reply 19 Feb 2019, 11:54
                            1
                            • J.HilkJ J.Hilk
                              19 Feb 2019, 06:45

                              @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!

                              M Offline
                              M Offline
                              ManiRon
                              wrote on 19 Feb 2019, 11:54 last edited by
                              #14
                              This post is deleted!
                              1 Reply Last reply
                              0

                              1/14

                              19 Feb 2019, 04:13

                              • Login

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