4.QList

 1 #include "mainwindow.h"
 2 #include <QApplication>
 3 #include <QLabel>
 4 #include <list>
 5 #include <QList>
 6 #include <QDebug>
 7 using namespace std;
 8 
 9 //STL list
10 int main1(int argc, char *argv[])
11 {
12     QApplication a(argc, argv);
13     //MainWindow w;
14     //w.show();
15     list<QLabel*> mylist;
16 
17     char *str[5]={"hello1","hello2","hello3","hello4","hello5"};
18     for(int i=0;i<5;i++)
19     {
20         QLabel *p = new QLabel(str[i]);
21         mylist.push_back(p);
22     }
23     int i=0;
24     for(auto X:mylist)
25     {
26         X->move(i++*100,i++*100);
27         X->show();
28     }
29 
30 
31     return a.exec();
32 }
33 
34 //QListIterator迭代器数据只能读不能写
35 int main2(int argc, char *argv[])
36 {
37     QApplication a(argc, argv);
38 //    QList<QLabel*> mylist;
39 
40 //    char *str[5]={"hello1","hello2","hello3","hello4","hello5"};
41 //    for(int i=0;i<5;i++)
42 //    {
43 //        QLabel *p = new QLabel(str[i]);
44 //        mylist.push_back(p);
45 //    }
46 //    int i=0;
47 //    for(auto X:mylist)
48 //    {
49 //        X->move(i++*100,i++*100);
50 //        X->show();
51 //    }
52 
53     QList<int> mylist;
54     mylist.push_back(1);
55     mylist<<1<<2<<3<<4;
56 
57     QListIterator<int> myi(mylist);
58     while(myi.hasNext())
59     {
60         qDebug()<<myi.next();
61     }
62 
63     return a.exec();
64 }
65 
66 // QMutableListIterator数据能读能写
67 int main(int argc, char *argv[])
68 {
69     QApplication a(argc, argv);
70 
71 
72     QList<int> mylist;
73     mylist.push_back(1);
74     mylist<<1<<2<<3<<4;
75 
76     QMutableListIterator<int> myi(mylist);
77     myi.toFront();
78     while(myi.hasNext())
79     {
80         int tmp = myi.next()*2;
81         myi.setValue(tmp);
82         qDebug()<<tmp;
83     }
84 
85  
86     return a.exec();
87 }
原文地址:https://www.cnblogs.com/xiaochi/p/8734303.html