Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QByteArray Initialization
Qt 6.11 is out! See what's new in the release blog

QByteArray Initialization

Scheduled Pinned Locked Moved Solved Mobile and Embedded
17 Posts 7 Posters 22.6k Views 2 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
    mostefa
    wrote on last edited by
    #2

    HI @vivekyuvan

    What if you initialize it like this:

    QByteArray m_data("\x0c\x06\x04\x04\x02\x00",6);
    

    ?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vivekyuvan
      wrote on last edited by
      #3

      HI @mostefa I got an an error like this below
      error: could not convert '{12, 6, 4, 4, 2, 0}' from '<brace-enclosed initializer list>' to 'QByteArray'
      QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};

      M 1 Reply Last reply
      0
      • V vivekyuvan

        HI @mostefa I got an an error like this below
        error: could not convert '{12, 6, 4, 4, 2, 0}' from '<brace-enclosed initializer list>' to 'QByteArray'
        QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};

        M Offline
        M Offline
        mostefa
        wrote on last edited by
        #4

        @vivekyuvan said in QByteArray Initialization:

        HI @mostefa I got an an error like this below
        error: could not convert '{12, 6, 4, 4, 2, 0}' from '<brace-enclosed initializer list>' to 'QByteArray'
        QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};

        Can you share the code of what you are doing exactly?

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vivekyuvan
          wrote on last edited by
          #5

          Hi @mostefa
          Its a test code I want to initalize array in normal C Standard type

          If I initialize qbyte array in Private Slot

          Ex

          QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
          

          I got an error See the screen shot ![alt text](0_1505900733317_ErrorScreenShot.png image url)

          M 1 Reply Last reply
          0
          • V vivekyuvan

            Hi @mostefa
            Its a test code I want to initalize array in normal C Standard type

            If I initialize qbyte array in Private Slot

            Ex

            QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
            

            I got an error See the screen shot ![alt text](0_1505900733317_ErrorScreenShot.png image url)

            M Offline
            M Offline
            mostefa
            wrote on last edited by mostefa
            #6

            @vivekyuvan said in QByteArray Initialization:

            Hi @mostefa
            Its a test code I want to initalize array in normal C Standard type

            If I initialize qbyte array in Private Slot

            Ex

            QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
            

            I got an error See the screen shot ![alt text](0_1505900733317_ErrorScreenShot.png image url)

            What if you change this line :

            QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
            

            To this following line :

            QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
            

            QByteArray Constructor

            V 1 Reply Last reply
            3
            • M mostefa

              @vivekyuvan said in QByteArray Initialization:

              Hi @mostefa
              Its a test code I want to initalize array in normal C Standard type

              If I initialize qbyte array in Private Slot

              Ex

              QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
              

              I got an error See the screen shot ![alt text](0_1505900733317_ErrorScreenShot.png image url)

              What if you change this line :

              QByteArray m_data={0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
              

              To this following line :

              QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
              

              QByteArray Constructor

              V Offline
              V Offline
              vivekyuvan
              wrote on last edited by vivekyuvan
              #7

              Hi@mostefa

              QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
              

              The Error message gone and I got the output thanks. But I want to send the array to emit signal? is that possible ? I am newbie to QT soft/Development guide me to resolve this problem

              M 1 Reply Last reply
              0
              • V vivekyuvan

                Hi@mostefa

                QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
                

                The Error message gone and I got the output thanks. But I want to send the array to emit signal? is that possible ? I am newbie to QT soft/Development guide me to resolve this problem

                M Offline
                M Offline
                mostefa
                wrote on last edited by
                #8

                @vivekyuvan said in QByteArray Initialization:

                Hi@mostefa The Error message gone and I got the output thanks. But I want to send the array to emit signal? is that possible ? I am newbie to QT soft/Development guide me to resolve this problem

                You can actually send array in signal,

                let's say you have the following signal :

                void sendByteArray(QByteArray);// on your .h file
                

                inside your testArrayInitialization you can do something like this :

                QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
                emit sendByteArray(m_data);
                

                If this is not what you want , can you explain what you want to do?

                1 Reply Last reply
                6
                • V Offline
                  V Offline
                  vivekyuvan
                  wrote on last edited by
                  #9

                  @mostefa thanks The problem is solved

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mostefa
                    wrote on last edited by mostefa
                    #10

                    You are welcome =) , you can change the state of your post to solved

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      titan83
                      wrote on last edited by
                      #11

                      What about:

                      QByteArray text = QByteArray::fromHex("517420697320677265617421");
                      
                      jsulmJ 1 Reply Last reply
                      1
                      • T titan83

                        What about:

                        QByteArray text = QByteArray::fromHex("517420697320677265617421");
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @titan83 This is just a string containing ASCII characters.
                        It should be like @mostefa shown:

                        QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
                        

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

                        V 1 Reply Last reply
                        3
                        • joaopagottoJ Offline
                          joaopagottoJ Offline
                          joaopagotto
                          wrote on last edited by
                          #13

                          Hi friend, use this:

                          #include <QCoreApplication>
                          
                          int main(int argc, char *argv[])
                          {
                             QCoreApplication a(argc, argv);
                          
                             QByteArray data;
                          
                             data.append((char)0x0c);
                             data.append((char)0x06);
                             data.append((char)0x04);
                             data.append((char)0x04);
                             data.append((char)0x02);
                             data.append((char)0x00);
                             data.append((char)0xFF);
                          
                             printf("%d\n", data.size());
                             printf("%s\n", data.toHex().toStdString().c_str());
                          
                             return a.exec();   
                          }
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • joaopagottoJ joaopagotto

                            Hi friend, use this:

                            #include <QCoreApplication>
                            
                            int main(int argc, char *argv[])
                            {
                               QCoreApplication a(argc, argv);
                            
                               QByteArray data;
                            
                               data.append((char)0x0c);
                               data.append((char)0x06);
                               data.append((char)0x04);
                               data.append((char)0x04);
                               data.append((char)0x02);
                               data.append((char)0x00);
                               data.append((char)0xFF);
                            
                               printf("%d\n", data.size());
                               printf("%s\n", data.toHex().toStdString().c_str());
                            
                               return a.exec();   
                            }
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by jsulm
                            #14

                            @joaopagotto This is actually the same he already did. He asked for something like this or similar:

                            QByteArray m_data{0x0c, 0x06, 0x04, 0x04, 0x02, 0x00};
                            

                            Also there is no need to use printf in C++. Instead of

                            printf("%d\n", data.size());
                            

                            you can just write

                            qDebug() << data.size();
                            

                            or C++ without Qt

                            std::cout << data.size();
                            

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

                            1 Reply Last reply
                            2
                            • jsulmJ jsulm

                              @titan83 This is just a string containing ASCII characters.
                              It should be like @mostefa shown:

                              QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00",6);
                              
                              V Offline
                              V Offline
                              vivekyuvan
                              wrote on last edited by vivekyuvan
                              #15
                              This post is deleted!
                              1 Reply Last reply
                              0
                              • aha_1980A Offline
                                aha_1980A Offline
                                aha_1980
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @jsulm said in QByteArray Initialization:

                                @titan83 This is just a string containing ASCII characters.

                                Of course @titan83 has a point here. His string is made of ASCII characters, but he uses QByteArray::fromHex(). This may not be the fastest operation, but allows to read the data from UI or an ASCII file, for example.

                                Qt has to stay free or it will die.

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  delverdl
                                  wrote on last edited by
                                  #17

                                  You can basically use a C++11 initializer list the following way (I've added a char higher than 0x7f and initializtion by variables):

                                  char        v1 = 0x06, v2 = 0x04;
                                  QByteArray  ba(std::begin<char>({0x0c, v1, v2, v2, 0x02, 0x00, '\xf3'}), 7);
                                  
                                  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