Results 1 to 7 of 7

Thread: Problem understanding the addressbook tutorial (part 3)

  1. #1
    Join Date
    Feb 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Problem understanding the addressbook tutorial (part 3)

    Hey everyone,

    I've started learning Qt a few days ago, having a background of mostly procedural, relatively low level, C programming, also tinkering a bit with Python and C++. Well, I'm stuck on the end of the part 3 of the addressbook tutorial, the implementation of the next and previous methods. This may have to do with my complete lack of iterators...
    My main problem is with this piece of code here:
    Qt Code:
    1. if (i == contacts.end()){
    2. nameLine->clear();
    3. addressText->clear();
    4. return;
    5. }
    To copy to clipboard, switch view to plain text mode 
    From my understanding, end() represents NULL in a traditional single linked list, it's not an actual member of the list, right? In this case, however, it represents the failure to find in the QMap a key matching the current name. Isn't this kind of useless? When would the current name not be on the addressbook?

    Cheers

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem understanding the addressbook tutorial (part 3)

    HM , not sure if i got your problem but...it actually has nothing to do with a container's data...
    The end() function of a container returns an iterator to the imaginary item one position past the last item in the container. end() marks an invalid position; it must never be dereferenced. It is typically used in a loop's break condition. If the list is empty, begin() equals end(), so we never execute the loop.
    Search for "Container Classes" in Qt Documentation for full info.

  3. #3
    Join Date
    Feb 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem understanding the addressbook tutorial (part 3)

    Hmm... Here's the code of the function in full:
    Qt Code:
    1. void AddressBook::previous()
    2. {
    3. QString name = nameLine->text();
    4. QMap<QString, QString>::iterator i = contacts.find(name);
    5.  
    6. if (i == contacts.end()) {
    7. nameLine->clear();
    8. addressText->clear();
    9. return;
    10. }
    11.  
    12. if (i == contacts.begin())
    13. i = contacts.end();
    14.  
    15. i--;
    16. nameLine->setText(i.key());
    17. addressText->setText(i.value());
    18. }
    To copy to clipboard, switch view to plain text mode 

    It seems to me that when the iterator is declared, and assigned to contacts.find(name), it will only get the contacts.end() "value" if the QString name isn't found in the QMap. However, how could this situation be possible?

  4. #4
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem understanding the addressbook tutorial (part 3)

    Don't quite understand your problem..."how could this situation be possible? " it's possible when QMap does not contain desired key-value pair. Maybe you think that contacts.end() returns a value ? It's not , it returns an iterator. So all that happens here is :

    1) From Qt Docs: "If the map contains no item with key key, the function returns end()." So if the current iterator (i) equals to the end()'s one : no entries were found,

    2) If something was found , iterator is moved to the last entry (the one before the end() )

  5. #5
    Join Date
    Feb 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem understanding the addressbook tutorial (part 3)

    But if name is a QString variable read from the contents of nameLine, and everything that is shown on the nameLine has to previously have been entered by the user, and thus saved in the QMap, it isn't actually possible that the string won't be found. And so the iterator will never equal contacts.end()...

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem understanding the addressbook tutorial (part 3)

    I think you are correct because the name box is read only when in the browsing view: the only place that value can have come from is the existing data and therefore you would expect it always to be found. The programming is defensive and will continue to function even if a future maintainer removes the read only attribute.

  7. #7
    Join Date
    Feb 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem understanding the addressbook tutorial (part 3)

    Hm, okay. That's what I thought it would be, but then it's not very effective defensive programming. If it clears the content of the input fields, on a subsequent press of the Next or Previous buttons, nothing would happen, it would just stay blank. Only when another entry was added to the map, and the user didn't fiddle with the input fields, would it regain normal operation. With the way everything is set up, if you suddenly make the input fields writable, many things would stop making sense

Similar Threads

  1. understanding problem of threads, mutexes and waitconditions
    By Spooky in forum General Programming
    Replies: 0
    Last Post: 28th April 2011, 11:23
  2. Replies: 3
    Last Post: 5th January 2011, 07:54
  3. Replies: 3
    Last Post: 18th July 2010, 13:53
  4. errors in addressbook tutorial?
    By Eolo in forum Newbie
    Replies: 5
    Last Post: 23rd April 2008, 21:39
  5. Qt Tutorial #1 Add-on Part II
    By ShaChris23 in forum Newbie
    Replies: 5
    Last Post: 25th April 2007, 19:00

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.