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. QVarient outputting bogus data
Forum Updated to NodeBB v4.3 + New Features

QVarient outputting bogus data

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 510 Views 1 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.
  • S Offline
    S Offline
    SOrton
    wrote on last edited by
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Apart from the strange conversion you're doing please show the contents of values[] and the value of z in a minimal, compilable example.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SOrton
        wrote on last edited by SOrton
        #3

        Sorry should've given those already.
        A basic example of what i'm doing would be:

        QString data(8, *) //Set temp data into the string
        int values[7] = {1,2,3,4};
        for(z=0;z<8;z++)
        {
        
                if(z % 2 != 0)
                { //I probably don't need this bit if toChar() works properly, 
                  // I was just trying to locate the error.
                   data[z] = ',';
                }
                else
                {
                    data[z] = QVariant(values[z]).toChar();
                }
        }
        qDebug() << "output: " << data; //And this outputs 1,2,3,4 but as a string
        

        I know its odd, but I need it as a string sadly. There may be a better conversion method that I've not found

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You're accessing values{z] out-of-bounds.

          btw: Take a look at QString::number().

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          S 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            You're accessing values{z] out-of-bounds.

            btw: Take a look at QString::number().

            S Offline
            S Offline
            SOrton
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            If you mean out of bounds because my loop is bigger than the array; that's a type by me they're actually both set to the same integer variable.

            With QString::number() I get somekind of overload error:

            for(int z=0; z<longestString; z++)
                { //Set data to actaul values
                    tempChar = QString::number(values[z]);
                    if(z % 2 != 0)
                    {
                       data[z] = ',';
                    }
                    else
                    {
                        data[z] = tempChar; //error = no viable overload '='
                    }
                }
            

            I did actually try this method already but couldn't get around this error.

            Christian EhrlicherC 1 Reply Last reply
            0
            • S SOrton

              @Christian-Ehrlicher
              If you mean out of bounds because my loop is bigger than the array; that's a type by me they're actually both set to the same integer variable.

              With QString::number() I get somekind of overload error:

              for(int z=0; z<longestString; z++)
                  { //Set data to actaul values
                      tempChar = QString::number(values[z]);
                      if(z % 2 != 0)
                      {
                         data[z] = ',';
                      }
                      else
                      {
                          data[z] = tempChar; //error = no viable overload '='
                      }
                  }
              

              I did actually try this method already but couldn't get around this error.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              @SOrton said in QVarient outputting bogus data:

              data[z] = tempChar; //error = no viable overload '='

              Because data[z] refers to a QChar and you try to assign a QString.
              Use QString data; ... data += QString::number(whatever) instead array access.

              that's a type by me they're actually both set to the same integer variable.

              But even then your loop is not correct since your output would then contain 1,3,5,7,

              Fix your code and post it again (completely)

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @SOrton said in QVarient outputting bogus data:

                data[z] = tempChar; //error = no viable overload '='

                Because data[z] refers to a QChar and you try to assign a QString.
                Use QString data; ... data += QString::number(whatever) instead array access.

                that's a type by me they're actually both set to the same integer variable.

                But even then your loop is not correct since your output would then contain 1,3,5,7,

                Fix your code and post it again (completely)

                S Offline
                S Offline
                SOrton
                wrote on last edited by SOrton
                #7

                @Christian-Ehrlicher [link text]

                I tried to use string, because char was causing me to get random outputs. Ill go back to my original attempt and show you my outputs.
                Also the last , makes sense what you say but it doesn't seem to be happening, not sure why.

                code:

                //I cant give the code where these variables are set as the program is too big but
                //but ill set them to exact values they're in my program at this points in time.
                int longestString = 7;
                int values[7] = {1,2,3,4}; //may not be these numbers but will be 4 single digit numbers.
                 qDebug() << "longest string" << longestString;
                    QString data(longestString, '*'); //create data string with bogus data
                    qDebug() << "Initial data" << data;
                    char tempChar;
                    for(int z=0; z<longestString; z++)
                    { //Set data to actaul values
                        tempChar = values[z]; //using char(values[z]) yields same result
                        if(z % 2 != 0)
                        {
                           data[z] = ',';
                        }
                        else
                        {
                            data[z] = tempChar;
                        }
                    }
                
                
                    qDebug() << "new data " << data;
                

                The final debug gives an output of: "\u0001,\b,\u0000,\u0000"

                So the , are placed correctly (without one at the end) and the chars I'm not sure what's happening

                I believe these must be unicode. How do i stop it going to unicode/convert it back to true char?

                M 1 Reply Last reply
                0
                • S SOrton

                  @Christian-Ehrlicher [link text]

                  I tried to use string, because char was causing me to get random outputs. Ill go back to my original attempt and show you my outputs.
                  Also the last , makes sense what you say but it doesn't seem to be happening, not sure why.

                  code:

                  //I cant give the code where these variables are set as the program is too big but
                  //but ill set them to exact values they're in my program at this points in time.
                  int longestString = 7;
                  int values[7] = {1,2,3,4}; //may not be these numbers but will be 4 single digit numbers.
                   qDebug() << "longest string" << longestString;
                      QString data(longestString, '*'); //create data string with bogus data
                      qDebug() << "Initial data" << data;
                      char tempChar;
                      for(int z=0; z<longestString; z++)
                      { //Set data to actaul values
                          tempChar = values[z]; //using char(values[z]) yields same result
                          if(z % 2 != 0)
                          {
                             data[z] = ',';
                          }
                          else
                          {
                              data[z] = tempChar;
                          }
                      }
                  
                  
                      qDebug() << "new data " << data;
                  

                  The final debug gives an output of: "\u0001,\b,\u0000,\u0000"

                  So the , are placed correctly (without one at the end) and the chars I'm not sure what's happening

                  I believe these must be unicode. How do i stop it going to unicode/convert it back to true char?

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

                  @SOrton
                  Hi,
                  If you want the ascii value of a digit, just add 48 ('0')
                  data[z] =values[z]+48;

                  Apart from that, I would say you need to improve your programming skills :)

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SOrton
                    wrote on last edited by SOrton
                    #9

                    @mpergand I have tried simply +48 and it didnt seem to work, yielding 1,8,0,0 from the above output

                    in this case 1,2,8,11 becomes 1,8,0,0 (with +48) I can understand 11 not working but not sure about the other two.

                    I never claimed to be a top programmer, and i am learning by doing. I am thankful for all the help you guys give me but I don't think saying that is necessary ;/

                    M 1 Reply Last reply
                    0
                    • S SOrton

                      @mpergand I have tried simply +48 and it didnt seem to work, yielding 1,8,0,0 from the above output

                      in this case 1,2,8,11 becomes 1,8,0,0 (with +48) I can understand 11 not working but not sure about the other two.

                      I never claimed to be a top programmer, and i am learning by doing. I am thankful for all the help you guys give me but I don't think saying that is necessary ;/

                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by
                      #10

                      @SOrton said in QVarient outputting bogus data:

                      1,2,8,11 becomes 1,8,0,0

                      Because of that:
                      if(z % 2 != 0)

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by Christian Ehrlicher
                        #11

                        @SOrton said in QVarient outputting bogus data:

                        int values[7] = {1,2,3,4}; //may not be these numbers but will be 4 single digit numbers.

                        this is still wrong - there are only 4 numbers so it should be values[4]...

                        And here the simple version of your code:

                        values[4] = {1, 2, 3, 4}
                        QString output;
                        for (int val : values)
                          output += QString::number(val) + ',';
                        

                        --> output = "1,2,3,4"

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        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