PDA

View Full Version : QList custom data format



sureshkvl
13th October 2010, 13:47
Hi,

I want to use like structure array for 100s of custom data in QList.
each custom data has name,age. I created a class for custom data. and use that class fr QList container.

I am using the following code, I am able to store it & retrive it. But i how do i search the QList if it is custom data?

thanks
suresh


class studentt
{
public:
QString name;
int class1;
}s[100];

void initclass()
{
for(int i =0;i<100;i++)
s[i].class1=(i+2)*9;
s[i].name="test";
}

int main(int argc,char *argv[])
{
initclass();
QList<studentt> aa;
for(int i =0;i<100;i++)
aa.append(s[i]);

studentt d;
for(int i=0;i<100;i++)
{
d=aa.at(i);
qDebug() <<"class1"<<d.class1;
}
/////The following line is not working - How do i use search if it is custom data
//int x=aa.indexOf(900);
//d=aa.at(x);
//qDebug() <<"class1"<<d.class1;
}

tbscope
13th October 2010, 14:26
Do you want to know if an item is of type "Student" or do you want to search for a specific student in the list?

If you implement the == operator in your student class, you can simply use QList::contains
Otherwise do something like this:


foreach(const Student &student, aa) {
if (student.name() == "John") {
// found the student
break;
}
}