I don't understand, every part is passed to the callback, so you just do whatever you want with the part you select, then stop traversal.  You need something like:

struct Finder {
   QMailMessageContentType type;
   QString body;
   bool operator()(const QMailMessagePart &part) {
      if (part.contentType() == type) {
         body = part.body().data();
         return false;
      }
      return true;
   }
};

PartFinder finder;
finder.type = QMailMessageContentType("text/calendar");
msg.foreachPart(finder);
return finder.body;