Results 1 to 4 of 4

Thread: How to iterate a QStringList from a specific position its end()

  1. #1
    Join Date
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to iterate a QStringList from a specific position its end()

    Hi there,

    how to iterate QStringList from a specific posistion to its end. Both examples don't work:

    Qt Code:
    1. QStringList stringlist;
    2.  
    3. for (int i = 4; !stringlist[i].isNull(); i++){}
    4.  
    5. for (QStringList::iterator it = stringlist.at(4); it != stringlist.end(); ++it) {}
    To copy to clipboard, switch view to plain text mode 
    How its down right?

    Hi there,

    how to iterate QStringList from a specific posistion to its end. Both examples don't work:

    Qt Code:
    1. QStringList stringlist;
    2.  
    3. for (int i = 4; !stringlist[i].isNull(); i++){}
    4.  
    5. for (QStringList::iterator it = stringlist.at(4); it != stringlist.end(); ++it) {}
    To copy to clipboard, switch view to plain text mode 
    How its down right?

    Hi there,

    how to iterate QStringList from a specific posistion to its end. Both examples don't work:

    Qt Code:
    1. QStringList stringlist;
    2.  
    3. for (int i = 4; !stringlist[i].isNull(); i++){}
    4.  
    5. for (QStringList::iterator it = stringlist.at(4); it != stringlist.end(); ++it) {}
    To copy to clipboard, switch view to plain text mode 
    How its down right?


    Added after 25 minutes:


    Just found a solution for myself:

    Qt Code:
    1. QStringList stringlist;
    2.  
    3. for (QStringList::iterator it = stringlist.begin()+4; it != stringlist.end(); ++it)
    4. {
    5. do something;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Is this the easiest way to do this?
    Last edited by mireiner; 24th April 2016 at 17:00.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to iterate a QStringList from a specific position its end()

    Is this the easiest way to do this?
    It is one way to do it. In your examples, "stringlist.at()" does not return an iterator, it returns the QString at the location you give as an argument and won't compile. "stringlist[i]" also returns the QString at "i", but in your code there is nothing to prevent the index from going off the end of the list. It will compile but will probably blow up at runtime.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to iterate a QStringList from a specific position its end()

    Quote Originally Posted by d_stranz View Post
    It will compile but will probably blow up at runtime.
    Exactly, unless come other code guarantees that the last element in the list is a null string.

    Why not check if the current value of "i" is still valid?

    Cheers,
    _

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to iterate a QStringList from a specific position its end()

    Something like this :
    Qt Code:
    1. QStringList stringlist;
    2.  
    3. for (int i = 4; i < stringlist.size(); i++){}
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 25th July 2013, 07:27
  2. How to show dockWidget at specific position in Qt?
    By gurmeetsingh in forum Qt Programming
    Replies: 0
    Last Post: 26th July 2012, 08:12
  3. qwebframe - scrolling to a specific position
    By boast in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2010, 03:20
  4. position of an item in a QStringList
    By jaca in forum Newbie
    Replies: 4
    Last Post: 29th May 2008, 06:49
  5. positioning a window at a specific screen position
    By nupul in forum Qt Programming
    Replies: 3
    Last Post: 29th March 2006, 21:21

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.