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. [Solved] Iterator over custom class
Forum Updated to NodeBB v4.3 + New Features

[Solved] Iterator over custom class

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k 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.
  • O Offline
    O Offline
    osfesa
    wrote on 10 Apr 2014, 12:34 last edited by
    #1

    Hi all.

    I'd to know how to iterate over a custom class. With Qt or STL.

    The code is more or less shown below:
    @
    class Messages{
    public:
    Messages(){}
    QList<QString> mymessages;
    };

    class PointList{
    public:
    PointList(){}
    QList<int> mypointlist;
    Messages mymessages;
    int somenumber;
    };

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    PointList mypl;
    
    for(int i = 0; i<10; i++){
        mypl.mypointlist.append(i);
        mypl.mymessages.mymessages.append(QString::number(i));
    }
    
    QList<int>::const_iterator i;
    for (i = mypl.begin(); i != mypl.end(); ++i)
        qDebug() << *i;
    
    return a.exec&#40;&#41;;
    

    }@

    As you can see, I want to iterate over a QList wrapped in a class. To use iterator how I must implement begin, end and operator++, operator!=.
    Is there any example on Qt documentation (I haven't found jet) about this situation?
    Looking at Qlist source code, is really complicated to do. Too much work for something that can be solved passing the reference.

    Finally, to iterate over Messages class I must pass the reference of the QList to main. No other way.

    I know is easier passing reference of QList<int> mypointlist; to main and iterate. But I think isn't a good practice, is it?

    What do you recommend me?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 10 Apr 2014, 21:44 last edited by
      #2

      If a class (like PointList) holds a couple of things (points and messages) then it's not really a point list, is it?

      If you want to easily iterate over them using only mypl variable this is one way(among many) to do it:
      @
      class Messages{
      public:
      Messages(){}
      QStringList mymessages;

      typedef QStringList::const_iterator const_iterator;
      const_iterator cbegin() const {
                return mymessages.cbegin(); }
      const_iterator cend() const {
                return mymessages.cend(); }
      

      };

      class PointList{
      public:
      PointList(){}
      QList<int> mypointlist;
      Messages mymessages;
      int somenumber;

      typedef QList<int>::const_iterator const_iterator;
      const_iterator cbegin() const {
                return mypointlist.cbegin(); }
      const_iterator cend() const {
                return mypointlist.cend(); }
      
      Messages::const_iterator messagesCbegin() const {
                return mymessages.cbegin(); }
      Messages::const_iterator messagesCend() const {
                return mymessages.cend(); }
      

      };

      //and then...
      PointList::const_iterator i = mypl.cbegin();
      for (; i != mypl.cend(); ++i)
      qDebug() << *i;

      Messages::const_iterator j = mypl.messagesCbegin();
      for (; j != mypl.messagesCend(); ++j)
      qDebug() << *j;
      @

      1 Reply Last reply
      0
      • O Offline
        O Offline
        osfesa
        wrote on 11 Apr 2014, 08:14 last edited by
        #3

        Thanks Chris for you answer.

        I called PointList, but the real name isn't PointList. Look as a List of points with more information not related with the list.

        Anyway, thanks for you answer.

        I found more results searching as "custom iterator" on Google.
        In "this link":https://sites.google.com/site/andreatagliasacchi/blog/customforeachiteratorsinqt show how to make a custom "foreach".

        Also I foundt different solutions using stl, all more complicated than yours.
        I'll try with a in/out iterator as well.

        Thanks.

        1 Reply Last reply
        0

        1/3

        10 Apr 2014, 12:34

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved