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. Convert QStringList to multiple integers
Qt 6.11 is out! See what's new in the release blog

Convert QStringList to multiple integers

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.3k 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.
  • S Offline
    S Offline
    samy03
    wrote on last edited by
    #1

    I have a QStringList like ["2", "3", "4"] and I'm trying to compare numbers in this QStringList to a QString which only has one number in it.

    QStringList list = ["2", "3", "4"];
    QString number = "3";
    
    for (int i=0 ; i<3 ; i++)
    {
               if (number == list.at(i))
               {
                     //do something
               }
              else
              {
                //do something else 
              }
    }
    

    I am not able to make it work. What am I doing wrong?

    Any suggestions will be greatly appreciated.
    Thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      What exactly is not working ?

      On a side note, why not use a QVector<int> rather than a QStringList of numbers ?

      You should also rather write your for loop based on your container rather than use fixed values:

      for (const QString& value : list) {
          if (value == "3") {
              // Do something 
          } else {
              // Do other something 
          }
      }
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • S Offline
        S Offline
        samy03
        wrote on last edited by
        #3

        @SGaist said in Convert QStringList to multiple integers:

        What exactly is not working ?

        It acts like if true condition was not met.

        Even with your code its doing the same thing.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          This works fine:

          #include <QtDebug>
          #include <QStringList>
          
          int main(int argc, char *argv[])
          {
              Q_UNUSED(argc);
              Q_UNUSED(argv);
          
              QStringList list{"1", "2", "3"};
              for (const QString& value : list) {
                  if (value == QStringLiteral("3")) {
                      // Do something
                      qDebug() << "Found";
                  } else {
                      // Do other something
                      qDebug() << "Nop" << value;
                  }
              }
              return 0;
          }
          

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          • Login

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