QMapIterator声明几个列表的问题

发布于 2024-09-11 14:05:00 字数 792 浏览 6 评论 0原文

我有 3 种类型的列表,我正在尝试根据 attribute_type 打印它们。 因此根据 attribute_type 选择迭代器。 问题是,例如,如果 attribute_type == "pis",则执行 if 条件,但完成后它返回到上减法: QMapIterator it(pit.item_parameters),所以这样迭代器无效...... 我应该如何正确申报?

QMapIterator<QString, IP> it(pit.item_parameters);

if (attribute_type == "ips") {
    QMapIterator<QString, IP> it(pit.item_parameters);
    if (pit.item_parameters.isEmpty()) { return; }
}
else if (attribute_type == "variables") {
    QMapIterator<QString, Variable> it(pit.variables);
    if (pit.variables.isEmpty()) { return; }
}
else if (attribute_type == "pis") {
    QMapIterator<QString, PI> it(pit.pis);
    if (pit.pis.isEmpty()) { return; }
}

while(it.hasNext())
{
    it.next();
    text += it.key();
    text += it.value().type;

}

i'm having 3 types of lists, and i'm trying to print them according to attribute_type.
so according attribute_type the iterator is chosen.
the problem is if for example attribute_type == "pis", the if condition is executed but when finished it returns to the upper decleration: QMapIterator it(pit.item_parameters), so this way the iterator is not valid...
how should i declare it properly ?

QMapIterator<QString, IP> it(pit.item_parameters);

if (attribute_type == "ips") {
    QMapIterator<QString, IP> it(pit.item_parameters);
    if (pit.item_parameters.isEmpty()) { return; }
}
else if (attribute_type == "variables") {
    QMapIterator<QString, Variable> it(pit.variables);
    if (pit.variables.isEmpty()) { return; }
}
else if (attribute_type == "pis") {
    QMapIterator<QString, PI> it(pit.pis);
    if (pit.pis.isEmpty()) { return; }
}

while(it.hasNext())
{
    it.next();
    text += it.key();
    text += it.value().type;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2024-09-18 14:05:00

如果您的类型 IPPIVariable 派生自具有 type 的同一类 ParentClass > 作为公共成员,你可以这样声明:

QMapIterator<QString, ParentClass> it;

If your types IP, PI and Variable derive from the same class ParentClass having type as public member, you can declare it that way:

QMapIterator<QString, ParentClass> it;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文