oopapidocs  2.0
customerlist.cpp
00001 
00002 #include "customerlist.h"
00003 
00004 #include <QVector>
00005 #include <QListIterator>
00006 #include <QDebug>
00007 
00008 void CustomerList::addCustomer(Customer* cust) {
00009     cust->setParent(this); // now it is managed by this object
00010 }
00011 
00012 //start id=qobjectcast
00013 /* The Qt3 version of this method demonstrates the use of
00014    QObject_cast. Qt4 makes it much easier to get all
00015    customers in a childlist. */
00016 QList<Customer*> CustomerList::getCustomers_old() {
00017     QList<Customer*> retval;
00018     QListIterator<QObject*> itr(children());
00019     while (itr.hasNext()) {
00020         Customer* cust = qobject_cast<Customer*>(itr.next());
00021         if (cust != 0)
00022             retval.append(cust);
00023     }
00024     return retval;
00025 }
00026 
00027 QList<Customer*> CustomerList::getCustomers() {
00028    qDebug() << "getCustomers()";
00029    return findChildren<Customer*>("Customer");
00030 }
00031 //end
00032 
00033 //start id=inherits
00034 
00035 /* A totally silly example that uses inherits,
00036    static_cast and iterators. */
00037 QStringList CustomerList::getDateListVerbose() {
00038     QStringList retval;
00039     QListIterator<QObject*> itr(children());
00040     while (itr.hasNext()) {
00041         QObject* qptr = itr.next();
00042         if (qptr->inherits("Customer")) {
00043             Customer* cust = static_cast<Customer*> (qptr);
00044             retval.append(QString("ID: %1 - StartDate: %2")
00045             .arg(cust->getId())
00046             .arg(cust->getDateEstablished().toString()));
00047         }
00048     }
00049     return retval;
00050 }
00051 
00052 /* This is the same example using foreach */
00053 QStringList CustomerList::getDateList() {
00054     QStringList retval;
00055     foreach (const Customer* cust, getCustomers()) {
00056         retval.append(QString("ID: %1 - StartDate: %2")
00057         .arg(cust->getId())
00058         .arg(cust->getDateEstablished().toString()));
00059     }
00060     return retval;
00061 }
00062 
00063 //end
 All Classes Namespaces Functions Enumerations