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. Find difference between two QStringList
Forum Updated to NodeBB v4.3 + New Features

Find difference between two QStringList

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 11.2k 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.
  • T Offline
    T Offline
    tesmai4
    wrote on last edited by
    #1

    I have two QStringList @ QStringList list1, list2; @

    List2 is newer version of list1.I need to find difference of these two lists. Is there any built in function in QT for this?
    There is a way to find intersection of the two QStringList with
    @ QSet<QString> intersection = list1.toSet().intersect(list2.toSet());@
    But how to find the difference of the two lists?

    If not, What is the easiest and most efficient.

    Regards,

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      using "subtract ":http://qt-project.org/doc/qt-5/qset.html#subtract
      you can determine what is in list 1 and not in list 2 and the other way around.

      Afterwards, you can combine these results with unite.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tesmai4
        wrote on last edited by
        #3

        Thanks Eddy for your reply. It worked.

        There is only one difference element after subtraction, as expected.
        I cant print the element using qDebug().
        How will I return that element from the function as Qstring?

        Regards,

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          In your specific case the result is just one string, but it could be more, even none.

          You will have to iterate over the QSet, test the results and get the QString you want.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tesmai4
            wrote on last edited by
            #5

            Dear Eddy,
            Thanks for you reply. Below is my code snippet. I am having problem at line 24. I know that difference will be 1 value between new and old list.

            @ QStringList mOldList, mNewList;
            mNewList.append("1");
            mNewList.append("2");
            mNewList.append("3");
            mNewList.append("4");
            qDebug("mNewList.count():%d",mNewList.count());

            mNewList.append("1");
            mNewList.append("2");
            mNewList.append("3");
            mNewList.append("4");
            mNewList.append("5");
            

            qDebug("mNewList.count():%d",mNewList.count());

            QSet<QString> subtraction = mNewList.toSet().subtract(mOldList.toSet());

            qDebug("Difference size:%d\n ", subtraction.size());

            QSet<QString>::iterator i ;

            QString DiffFileName;

            for (i = subtraction.begin(); i != subtraction.end();++i)
            {
                DiffFileName <<i; //This statement is giving compilation error
            }
            
            qDebug(" DiffFileName:\n ");
            
            qDebug(" DiffFileName:%s\n ",  DiffFileName);@
            

            Above is my code for populating the lists and checking the difference. Statement 24 is giving compilation error. How shall I assign the value to my QString variable?

            Regards

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              here is one way of doing it :

              @
              foreach (const QString &DiffFileName, subtraction)
              qDebug() << " DiffFileName: "<< DiffFileName;
              @

              happy coding

              Qt Certified Specialist
              www.edalsolutions.be

              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