QList作为结构体的一个成员,当使用该结构体定义一个变量时,会创建一个空白的QList。
struct Score
{
int mach;
int chinese;
};
struct Student
{
int age;
QString name;
QList<Score> score; //创建一个Student变量时将创建一个空白list
};
//-----
{
QList<Student> students; //创建一个空白list
Student s;
s.age = 1;
s.name = "tom";
Score s1;
s1.chinese = 100;
s1.mach = 100;
s.score.append(s1);
Score s2;
s2.chinese = 200;
s2.mach = 200;
s.score.append(s2);
students.append(s);
Student t;
t.age = 2;
t.name = "jerry";
Score t1;
t1.chinese = 100;
t1.mach = 100;
t.score.append(t1);
Score t2;
t2.chinese = 200;
t2.mach = 200;
t.score.append(t2);
students.append(t);
//测试
for (int i = 0; i < students.count(); i++)
{
qDebug() << students.at(i).name << "\t" << students.at(i).age;
for (int j = 0; j < students.at(i).score.count(); j++)
{
qDebug() << "chinese:" << students.at(i).score.at(j).chinese
<< "\tmath:" << students.at(i).score.at(j).mach;
}
}
if (!students.isEmpty())
{
qDebug() << students.constFirst().name;
if (!students.first().score.empty())
{
qDebug() << students.first().score.constFirst().chinese;
}
}
}