Results 1 to 9 of 9

Thread: QString contains fails to find comma

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Posts
    31
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QString contains fails to find comma

    Thanks for the quick responses.

    Lykurg: I tried with both .value(i) and .at(i) and neither trigger when the condition is met. Unless you mean in a manner different to passedList.at(i).contains.

    tbscope: that's my other option, I wasn't aware of it when I first wrote my function and it'd involve a bit of refactoring to get it changed. I'll probably go down that route when I give up trying to get the .contains working. Intellectual challenge, or working out why I'm being a bit slow!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QString contains fails to find comma

    I see no problem with your syntax. so make a debug version an look at the values:
    Qt Code:
    1. qDebug() << passedList;
    2. qDebug() << i;
    3. qDebug() << passedList.value(i);
    4. qDebug() << passedList.value(i).contains(",");
    5. if(passedList.value(i).contains(","))
    6. {
    7. qDebug() << "replacing";
    8. passedList.value(i).replace(",", " ");
    9. }
    To copy to clipboard, switch view to plain text mode 
    What does it returns?

  3. The following user says thank you to Lykurg for this useful post:

    NicholasSmith (6th August 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QString contains fails to find comma

    at() and value() are const
    You can't directly change a list like this, try this:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2.  
    3. #include <QStringList>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. qDebug() << "Test replacing comma in a string list";
    11. qDebug() << "";
    12.  
    13. list << "a, b, c" << "d, e, f";
    14.  
    15. for (int c = 0; c < list.count(); ++c) {
    16. qDebug() << "Line" << c+1 << "=" << list.at(c);
    17. }
    18.  
    19. qDebug() << "";
    20. qDebug() << "And now without a comma:";
    21.  
    22. for (int c = 0; c < list.count(); ++c) {
    23. QString line = list.at(c);
    24. line.replace(',', ' ');
    25. list.replace(c, line);
    26. qDebug() << "Line" << c+1 << "=" << list.at(c);
    27. }
    28.  
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    It gives this output:
    Qt Code:
    1. Test replacing comma in a string list
    2.  
    3. Line 1 = "a, b, c"
    4. Line 2 = "d, e, f"
    5.  
    6. And now without a comma:
    7. Line 1 = "a b c"
    8. Line 2 = "d e f"
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to tbscope for this useful post:

    NicholasSmith (6th August 2010)

  6. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QString contains fails to find comma

    Quote Originally Posted by tbscope View Post
    at() and value() are const
    Ah, I messed it up. Once again. at() is returning a const reference, but value returning a value (which is not const for the returning value, this it what I meant). I will never keep that in mind...
    const T & at ( int i ) const
    T value ( int i ) const
    EDIT: Without a temporary QString you have to use the [] operator.

  7. The following user says thank you to Lykurg for this useful post:

    NicholasSmith (6th August 2010)

  8. #5
    Join Date
    Jan 2009
    Posts
    31
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QString contains fails to find comma

    Thanks chaps! I decided to just go down the temporary string route, and it works perfectly now.

  9. #6
    Join Date
    Jan 2009
    Posts
    31
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QString contains fails to find comma

    Ah hah, it is actually spotting it and triggering, it just doesn't seem to be replacing now.

Similar Threads

  1. QString find Whole word Only
    By bcteh_98 in forum Qt Programming
    Replies: 9
    Last Post: 7th December 2018, 08:47
  2. QString - find and replace text
    By graciano in forum Newbie
    Replies: 3
    Last Post: 24th January 2009, 20:35
  3. Replies: 14
    Last Post: 16th January 2009, 08:11
  4. Release build fails to find some resource images
    By MrGarbage in forum Installation and Deployment
    Replies: 3
    Last Post: 8th December 2007, 16:04
  5. Replies: 11
    Last Post: 11th October 2007, 16:34

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.