Results 1 to 11 of 11

Thread: QStringList ......how to ignore a string

  1. #1
    Join Date
    Feb 2006
    Posts
    51
    Thanks
    7

    Default QStringList ......how to ignore a string

    Hi folks,

    I have a question on QstringLists.. I have a list of strings which is produced and in that list, i have a string which can have the same name like..

    john_1
    john_2
    john_1
    john_2

    How can i ignore the last two strings and show only the first two ones??

    here is my code and explanation...

    Qt Code:
    1. //this is the list which will see how many category of the names are there
    2. // for example two category :
    3. //1- NameList One
    4. //2- NameList Two
    5.  
    6. QStringList test1 = project->getTest1List();
    7.  
    8. for ( unsigned int i=0; i <test1.size(); i++ )
    9. {
    10.  
    11. // this is the list which will look inside the above two categories and collect
    12. //the names for example :
    13.  
    14. //NameList One: has the following names
    15. //john_1
    16. //john_2
    17.  
    18. //NameList Two: has the following names
    19. //john_1
    20. //john_2
    21. //john_3
    22.  
    23. //I get now the following:
    24. //john_1
    25. //john_2
    26. //john_1
    27. //john_2
    28. //john_3
    29.  
    30. // i want the following results:
    31. //john_1
    32. //john_2
    33. //john_3
    34.  
    35. QStringList totall = test->getfromFunction();
    36. SomethingItem * someItem;
    37.  
    38. someItem->getItem()->setText(NAME, QString("test: %1").arg(totall.size()));
    39. someItem->getItem()->setOpen(false);
    40.  
    41. for ( unsigned int j=0; j <totall.size(); j++ )
    42. {
    43.  
    44. someItem->setName(totall[j]);
    45.  
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 
    Love::Peace

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringList ......how to ignore a string

    Before inserting into the list, can't you check if the string exists
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QStringList ......how to ignore a string

    Use your strings as keys for a QMap. After that you can get a QList with unique string from QMap::keys().

    Easy and quick hack if performance is not that of a problem.

  4. #4
    Join Date
    Feb 2006
    Posts
    51
    Thanks
    7

    Default Re: QStringList ......how to ignore a string

    Quote Originally Posted by sunil.thaha
    Before inserting into the list, can't you check if the string exists
    no because the list is produced by database and based on situation i get the categories of name.. it can be 10 categories and each can have the same names.. i just want to ignore the names which are same..
    Love::Peace

  5. #5
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QStringList ......how to ignore a string

    Just seen: QStringList takes a QList<QString> parameter.

    So QMap<QString bla, bool whocares> bla;
    bla.insert("string1",0);
    bla.insert("string2",0);

    QStringList(bla.keys()) <- Your stringlist with unique strings.

    Don't take my 'example code' too literal. ;-)

  6. #6
    Join Date
    Feb 2006
    Posts
    51
    Thanks
    7

    Default Re: QStringList ......how to ignore a string

    Quote Originally Posted by Kumosan
    Just seen: QStringList takes a QList<QString> parameter.

    So QMap<QString bla, bool whocares> bla;
    bla.insert("string1",0);
    bla.insert("string2",0);

    QStringList(bla.keys()) <- Your stringlist with unique strings.

    Don't take my 'example code' too literal. ;-)
    no it cant work on this way.. because i never know which strings are the same, every time i get different strings.. i think i should make another list and insert the strings which will come one time and check if the strings are the same should not be inserted.. but how to implement dont know
    Love::Peace

  7. #7
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: QStringList ......how to ignore a string

    Quote Originally Posted by :db:sStrong
    no it cant work on this way.. because i never know which strings are the same, every time i get different strings.. i think i should make another list and insert the strings which will come one time and check if the strings are the same should not be inserted.. but how to implement dont know
    I don't quite understand. You don't need to know which strings are the same. The keys of a QMap are automaticall unique.

    You do twenty times:

    bla["someString"] = true;

    forty times:

    bla["anotherString"] = true;

    one time:

    bla["lastString"] = true;

    and the QList<String> bla.keys() contains exactly three strings:

    "someString" and "anotherString" and "lastString".

    If you initialize your QStringList with it you have your stringlist with unique strings. You don't need to know which strings are the same. QMap does this automatically.

  8. #8
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringList ......how to ignore a string

    Since it is database U can either use distinct or group by
    to eliminate the repeated !!!
    We can't solve problems by using the same kind of thinking we used when we created them

  9. #9
    Join Date
    Jan 2006
    Location
    India
    Posts
    54
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QStringList ......how to ignore a string

    make a sub-function

    Qt Code:
    1. bool isNameExistInList(QStringList &list, QString &name) {
    2. for(QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
    3. if(name == *it) {
    4. return TRUE;
    5. }
    6. }
    7. return FALSE;
    8. }
    To copy to clipboard, switch view to plain text mode 

    then use this function at the time of insertion of strings
    Qt Code:
    1. if(!isNameExistInList(yourList, "john_1") {
    2. yourList.append("john_1")
    3. }
    4.  
    5. and so on...
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to sumsin for this useful post:

    :db:sStrong (8th August 2006)

  11. #10
    Join Date
    Feb 2006
    Posts
    51
    Thanks
    7

    Default Re: QStringList ......how to ignore a string

    Quote Originally Posted by sumsin
    make a sub-function

    Qt Code:
    1. bool isNameExistInList(QStringList &list, QString &name) {
    2. for(QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
    3. if(name == *it) {
    4. return TRUE;
    5. }
    6. }
    7. return FALSE;
    8. }
    To copy to clipboard, switch view to plain text mode 

    then use this function at the time of insertion of strings
    Qt Code:
    1. if(!isNameExistInList(yourList, "john_1") {
    2. yourList.append("john_1")
    3. }
    4.  
    5. and so on...
    To copy to clipboard, switch view to plain text mode 
    hello,
    thanx this is working for me but the Qmap is not needed in this situation ..
    Love::Peace

  12. #11
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringList ......how to ignore a string

    or

    Qt Code:
    1. if( ! list.contains( name ) ) {
    2. list << name;
    3. }
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. ignore following link in anchorClicked()
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 16th July 2006, 16:33
  2. QStringList with Signal/Slot
    By Sivert in forum Qt Programming
    Replies: 4
    Last Post: 3rd May 2006, 20:34
  3. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48
  4. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55
  5. need help to classify some QStringList
    By patcito in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 21:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.