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. Copying the content of int array to QString
Forum Update on Monday, May 27th 2025

Copying the content of int array to QString

Scheduled Pinned Locked Moved Solved Mobile and Embedded
12 Posts 4 Posters 9.2k 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.
  • V Offline
    V Offline
    vivekyuvan
    wrote on last edited by
    #1

    Hi Friends

    Help need to convert integer array To QString and emit the data to my slot

    int a[10]={196,192,200,194,178,147,196,200,194,200};
    

    I already tried this

    char text[] = { '1', '2', '3', '4', '5', '\0' };
        QString string(text);
       qDebug()<<string;
       emit Send_Data_Array_to_form(string);
    

    above snippet work perfectly. But when i send integer array to QString the Receiver slot not properly receive my data i don know why. help me to resolve this issue.

    this is my receiver slot

    void MainWindow::Test_Array_Initialization(QString S_Data)
    {
        qDebug()<<"*****SIGNAL EMITED From My_signal_get_form1**********"<<endl;
        qDebug()<<"data received"<<S_Data.size()<<S_Data;
    }
    

    Thanks in Advance.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Please post the code which gets the int array and sends it. It is the crucial part of the code in this case.

      Please also clarify what exact output do you expect (what S_Data you think should look like).

      (Z(:^

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

        hi@sierdzio I have problem with convert int array to QString . I just compiled my own test code

        void Array_Data_emit_Form1::Raw_Data_Initialization()
        
        {
           unsigned int a[5]={1,2,3,-4,5};
            QString My_String;
           for(int i=0; i<5; i++)
            {
               My_String=a[i];
            }
        qDebug()<<"My_String datas:"<<My_String;
        emit SEND_Array_To_Mainw(My_String);
        }
        

        This is my connect signal in mainwindow.cpp

        ```
        

        connect(data_connect,SIGNAL(SEND_Array_To_Mainw(QString)),this,SLOT(Show_Data_to_Window(QString)));

        
        
        This is my Receiver Slot in mainwindow.cpp
        
        

        void MainWindow::Show_Data_to_Window(QString S_data)
        {

        qDebug() <<"Data Received"<<S_data.size()<<S_data;
        

        }

        
        The Out put I saw in Console
        
        My_String datas: "" 
        Data Received 1 "" 
        My_String datas: "" 
        Data Received 1 ""![0_1506504541397_Console_output.png](https://ddgobkiprc33d.cloudfront.net/41b66b87-8b0c-4c1e-bbf2-d298ff3882f3.png) 
        
        
        
        
        
        What I expect is My array data will convert to String 
        In Receiver slot 
        
        the array data must be converted to string like 1 2 3 -4 5
        
        
        Help me to resolve this issue  Thanks in advance
        J.HilkJ 1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @vivekyuvan said in Copying the content of int array to QString:

          unsigned int a[5]={1,2,3,-4,5};
          QString My_String;
          for(int i=0; i<5; i++)
          {
          My_String=a[i];
          }

          You replace My_String each time in this loop. You probably want something like this instead:

          My_String+=QString::number(a[i]);
          

          (Z(:^

          1 Reply Last reply
          5
          • V vivekyuvan

            hi@sierdzio I have problem with convert int array to QString . I just compiled my own test code

            void Array_Data_emit_Form1::Raw_Data_Initialization()
            
            {
               unsigned int a[5]={1,2,3,-4,5};
                QString My_String;
               for(int i=0; i<5; i++)
                {
                   My_String=a[i];
                }
            qDebug()<<"My_String datas:"<<My_String;
            emit SEND_Array_To_Mainw(My_String);
            }
            

            This is my connect signal in mainwindow.cpp

            ```
            

            connect(data_connect,SIGNAL(SEND_Array_To_Mainw(QString)),this,SLOT(Show_Data_to_Window(QString)));

            
            
            This is my Receiver Slot in mainwindow.cpp
            
            

            void MainWindow::Show_Data_to_Window(QString S_data)
            {

            qDebug() <<"Data Received"<<S_data.size()<<S_data;
            

            }

            
            The Out put I saw in Console
            
            My_String datas: "" 
            Data Received 1 "" 
            My_String datas: "" 
            Data Received 1 ""![0_1506504541397_Console_output.png](https://ddgobkiprc33d.cloudfront.net/41b66b87-8b0c-4c1e-bbf2-d298ff3882f3.png) 
            
            
            
            
            
            What I expect is My array data will convert to String 
            In Receiver slot 
            
            the array data must be converted to string like 1 2 3 -4 5
            
            
            Help me to resolve this issue  Thanks in advance
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @vivekyuvan what you need is to convert the raw int to a QStirng. Lucky for you QString has a method that does that for you

            do the following change:

            QString My_String;
               for(int i=0; i<5; i++)
                {
                   My_String.append(QString::number(a[i]));
                }
            

            and you should be fine.

            Edit: @sierdzio was a bit faster x) but great minds think alike *cough*


            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.

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

              hi@sierdzio and @J-Hilk Thanks for the reply. I tried both method but no luck

              this is what my output in console window.

              My_String datas: "12342949672925" 
              Data Received 14 "12342949672925"
              

              here is the change in my Sending slot:

              void Array_Data_emit_Form1::Raw_Data_Initialization()
              
              {
                  
                  unsigned int a[5]={1,2,3,-4,5};
                  QString My_String;
                 for(int i=0; i<5; i++)
                  {
                  
                   //My_String=a[i];
                   My_String+=QString::number(a[i])
                    //My_String.append(QString::number(a[i]));
              
              }
              qDebug()<<"My_String datas:"<<My_String;
              emit SEND_Array_To_Mainw(My_String);
              }
              

              0_1506505800575_Console_Output2.png

              J.HilkJ 1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                If you want spaces between the numbers... insert them :-)

                My_String+=QString::number(a[i]) + " ";
                

                Also, another issue:

                unsigned int a[5]={1,2,3,-4,5};
                

                "unsigned" type will not store -4. It's actually quite surprising that the compiler does not throw an error here.

                (Z(:^

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

                  @sierdzio thank you. Sorry for the silly mistake that made by me. I just changed datatype as signed integer now its working i got 123-45. now my problem solved.

                  ```
                  

                  signed int a[5]={1,2,3,-4,5};

                  1 Reply Last reply
                  1
                  • V vivekyuvan

                    hi@sierdzio and @J-Hilk Thanks for the reply. I tried both method but no luck

                    this is what my output in console window.

                    My_String datas: "12342949672925" 
                    Data Received 14 "12342949672925"
                    

                    here is the change in my Sending slot:

                    void Array_Data_emit_Form1::Raw_Data_Initialization()
                    
                    {
                        
                        unsigned int a[5]={1,2,3,-4,5};
                        QString My_String;
                       for(int i=0; i<5; i++)
                        {
                        
                         //My_String=a[i];
                         My_String+=QString::number(a[i])
                          //My_String.append(QString::number(a[i]));
                    
                    }
                    qDebug()<<"My_String datas:"<<My_String;
                    emit SEND_Array_To_Mainw(My_String);
                    }
                    

                    0_1506505800575_Console_Output2.png

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

                    @vivekyuvan it is doing exactly what yopu tell it to,
                    unsigned int can be anything between 0 and 4,294,967,295 : -4 therefore => 4,294,967,292


                    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.

                    1 Reply Last reply
                    2
                    • sierdzioS sierdzio

                      If you want spaces between the numbers... insert them :-)

                      My_String+=QString::number(a[i]) + " ";
                      

                      Also, another issue:

                      unsigned int a[5]={1,2,3,-4,5};
                      

                      "unsigned" type will not store -4. It's actually quite surprising that the compiler does not throw an error here.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @sierdzio said in Copying the content of int array to QString:

                      It's actually quite surprising that the compiler does not throw an error here.

                      There's no error from the compiler's point of view, at best it should be a warning. There's no significant difference between signed and unsigned numbers aside from our interpretation of the data, so the compiler has no reason to complain. If you consider this:

                      unsigned short x = -3; // 0xFFFD
                      unsigned short y = 5;  // 0x0005
                      
                      short z = x + y;  // = 0xFFFD + 0x0005 = 0x10002 -> (due to truncation from overflow) 0x0002 == 2
                      short z2 = x - y; // = 0xFFFD - 0x0005 = 0xFFFD + 0xFFFB = 0x1FFF8 -> (due to truncation from overflow) 0xFFF8 == -8
                      

                      It's perfectly valid from the compiler's point of view, it's just a hell of a confusing way to do things and can get you in big trouble whenever comparing is involved. ;)

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      2
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #11

                        Yes, thanks for the explanation.

                        My intuition here was that since we clearly and openly want unsigned, the compiler will be clever enough to point out that "-4" is likely a bug. With gcc and clang being pretty clever about a lot of things (like pointing out signed/ unsigned mismatches in if statements etc.) I assumed it would shoot some (at least) warning here.

                        Actually, maybe it did print a warning but OP ignored it, who knows.

                        (Z(:^

                        kshegunovK 1 Reply Last reply
                        0
                        • sierdzioS sierdzio

                          Yes, thanks for the explanation.

                          My intuition here was that since we clearly and openly want unsigned, the compiler will be clever enough to point out that "-4" is likely a bug. With gcc and clang being pretty clever about a lot of things (like pointing out signed/ unsigned mismatches in if statements etc.) I assumed it would shoot some (at least) warning here.

                          Actually, maybe it did print a warning but OP ignored it, who knows.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by kshegunov
                          #12

                          It may not be a bug, that's what one could use, albeit there are better ways, to get the upper limit of the unsigned type.

                          unsigned maxint = -1; // And voila we have the UINT_MAX
                          

                          Although, I agree that perhaps a warning is in order, this doesn't seem to be the case with g++ (7.2.x) on my machine. The above line will not generate even a warning and I do compile with -Wall.

                          like pointing out signed/ unsigned mismatches in if statements etc.

                          This is when it comes to comparisons, because due to representation (and integral promotion) -1 is the biggest number there possibly can be in a mixed signed-unsigned comparison.

                          Actually, maybe it did print a warning but OP ignored it, who knows.

                          It'd depend on the actual compiler, but it might not have warned him.

                          Read and abide by the Qt Code of Conduct

                          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