Problems with Open-Source Downloads read and

Find difference between two QStringList

  • 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.


  • using "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.

  • 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?


  • 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.

  • 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;



    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?


  • here is one way of doing it :

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

    happy coding

Log in to reply