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
    评论
可以通过重载结构体的比较运算符来判断结构体原始数据和修改后数据是否有变动。如果结构体中包含 `QList` 结构体,我们可以递归比较 `QList` 中的元素是否相同。以下是一个示例代码: ```c++ #include <QDebug> #include <QList> struct MyStruct { int a; char b; double c; QList<MyStruct> list; bool operator==(const MyStruct& other) const { if (a != other.a || b != other.b || c != other.c || list.size() != other.list.size()) { return false; } for (int i = 0; i < list.size(); i++) { if (list[i] != other.list[i]) { return false; } } return true; } }; int main() { MyStruct original = { 1, 'a', 2.5, { {1, 'a', 2.5}, {2, 'b', 3.5} } }; MyStruct modified = { 2, 'b', 3.5, { {3, 'c', 4.5}, {4, 'd', 5.5} } }; qDebug() << "Original and modified are equal: " << (original == modified); modified = { 1, 'a', 2.5, { {1, 'a', 2.5}, {2, 'b', 3.5} } }; qDebug() << "Original and modified are equal: " << (original == modified); original.list[0].c = 4.5; qDebug() << "Original and modified are equal: " << (original == modified); return 0; } ``` 在上面的示例代码中,我们定义了一个名为 `MyStruct` 的结构体,并重载了比较运算符 `operator==`。在 `MyStruct` 结构体中,我们包含了一个名为 `list` 的 `QList<MyStruct>`。在 `operator==` 中,我们首先比较 `MyStruct` 结构体的成员变量 `a`、`b` 和 `c` 是否相等。然后,我们递归比较 `list` 中的元素是否相同。如果 `MyStruct` 结构体原始数据和修改后数据不同,或者 `list` 中的元素数量不同,我们返回 `false`。否则,我们继续比较 `list` 中的元素是否相同。如果所有的比较都没有问题,我们返回 `true`。 在 `main` 函数中,我们首先定义了一个名为 `original` 的结构体并初始化它。然后,我们定义了一个名为 `modified` 的结构体,并将其值修改为与 `original` 结构体不同。我们使用 `qDebug` 打印出 `original` 和 `modified` 是否相等的结果。由于 `modified` 结构体与 `original` 结构体不相等,因此第一次打印的结果为 `false`。然后,我们将 `modified` 结构体的值改回与 `original` 结构体相同,并再次打印两者是否相等的结果,此时结果为 `true`。最后,我们将 `original` 结构体的 `list` 中第一个元素的 `c` 成员变量的值修改为不同的值,并再次打印两者是否相等的结果,此时结果为 `false`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值