Qt结构体里使用Qlist

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;
        }
    }
}

 

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值