Qt-Qt之QList使用


相关资料:

https://blog.csdn.net/ligare/article/details/122687947

https://blog.csdn.net/jpchen609/article/details/4371594

https://download.csdn.net/download/zhujianqiangqq/85641106      CSDN代码包下载

实例代码:

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     main.cpp \
20     mainwindow.cpp
21 
22 HEADERS += \
23     mainwindow.h
24 
25 FORMS += \
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include 
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include 
 5 
 6 QT_BEGIN_NAMESPACE
 7 namespace Ui { class MainWindow; }
 8 QT_END_NAMESPACE
 9 
10 class MainWindow : public QMainWindow
11 {
12     Q_OBJECT
13 
14 public:
15     MainWindow(QWidget *parent = nullptr);
16     ~MainWindow();
17 
18 private slots:
19     void on_pushButton_4_clicked();
20 
21     void on_pushButton_3_clicked();
22 
23     void on_pushButton_2_clicked();
24 
25     void on_pushButton_clicked();
26 
27     void on_pushButton_8_clicked();
28 
29     void on_pushButton_7_clicked();
30 
31     void on_pushButton_10_clicked();
32 
33     void on_pushButton_9_clicked();
34 
35     void on_pushButton_5_clicked();
36 
37     void on_pushButton_6_clicked();
38 
39     void on_pushButton_11_clicked();
40 
41     void on_pushButton_13_clicked();
42 
43     void on_pushButton_12_clicked();
44 
45     void on_pushButton_15_clicked();
46 
47     void on_pushButton_16_clicked();
48 
49     void on_pushButton_14_clicked();
50 
51     void on_pushButton_17_clicked();
52 
53     void on_pushButton_18_clicked();
54 
55     void on_pushButton_20_clicked();
56 
57     void on_pushButton_19_clicked();
58 
59     void on_pushButton_21_clicked();
60 
61     void on_pushButton_22_clicked();
62 
63     void on_pushButton_23_clicked();
64 
65     void on_pushButton_24_clicked();
66 
67     void on_pushButton_25_clicked();
68 
69     void on_pushButton_26_clicked();
70 
71     void on_pushButton_27_clicked();
72 
73 private:
74     Ui::MainWindow *ui;
75     QList m_pList;// 定义成员
76 };
77 #endif // MAINWINDOW_H

mainwindow.cpp

  1 #include "mainwindow.h"
  2 #include "ui_mainwindow.h"
  3 
  4 MainWindow::MainWindow(QWidget *parent)
  5     : QMainWindow(parent)
  6     , ui(new Ui::MainWindow)
  7 {
  8     ui->setupUi(this);
  9 
 10     setWindowTitle(QStringLiteral("Qt之QList使用"));
 11 }
 12 
 13 MainWindow::~MainWindow()
 14 {
 15     delete ui;
 16 }
 17 
 18 // 添加元素方式1
 19 void MainWindow::on_pushButton_4_clicked()
 20 {
 21     // 会添加到末尾
 22     m_pList << "1" << "2" << "3";
 23     m_pList << "4";
 24 
 25 }
 26 // 添加元素方式2
 27 void MainWindow::on_pushButton_3_clicked()
 28 {
 29     // 添加到末尾 不会覆盖原来的
 30     m_pList.append("5");
 31     m_pList.push_back("6");
 32 }
 33 // 添加到头部
 34 void MainWindow::on_pushButton_2_clicked()
 35 {
 36     // 添加到头部 不会覆盖原来的
 37     m_pList.prepend("0");
 38     m_pList.push_front("00");
 39 }
 40 // 添加元素方式4
 41 void MainWindow::on_pushButton_clicked()
 42 {
 43     // 在位置4插入元素
 44     m_pList.insert(4, "I4");
 45 }
 46 // 插入元素
 47 void MainWindow::on_pushButton_8_clicked()
 48 {
 49     m_pList.swap(1,3);// 交换位置1和位置3的元素
 50 }
 51 // 移动位置
 52 void MainWindow::on_pushButton_7_clicked()
 53 {
 54     m_pList.move(1,4);// 把第一个元素移到第四个元素,其他元素顺移
 55 }
 56 // 查找元素
 57 void MainWindow::on_pushButton_10_clicked()
 58 {
 59     // return该元素的下标值;若有第二个参数则表示查找第几个这个字符
 60     ui->textEdit->append(QString::number(m_pList.indexOf("mm")));
 61 }
 62 // 最后一个元素
 63 void MainWindow::on_pushButton_9_clicked()
 64 {
 65      QString str = m_pList.back();//返回最后一个元素 同list.last();
 66      ui->textEdit->append(str);
 67 }
 68 // 第一个元素1
 69 void MainWindow::on_pushButton_5_clicked()
 70 {
 71     QString str = m_pList.front();
 72     ui->textEdit->append(str);
 73 }
 74 // 第一个元素2
 75 void MainWindow::on_pushButton_6_clicked()
 76 {
 77     QString str = m_pList.first();
 78     ui->textEdit->append(str);
 79 }
 80 // 是否存在
 81 void MainWindow::on_pushButton_11_clicked()
 82 {
 83     bool nExist = m_pList.contains("23");//列表是否有某元素 成功返回true 否则false
 84     ui->textEdit->append(QString::number(nExist));
 85 }
 86 // 总元素
 87 void MainWindow::on_pushButton_13_clicked()
 88 {
 89     int nCount = m_pList.count();
 90     ui->textEdit->append(QString::number(nCount));
 91 }
 92 // 总元素2
 93 void MainWindow::on_pushButton_12_clicked()
 94 {
 95     int nCount = m_pList.length();
 96     ui->textEdit->append(QString::number(nCount));
 97 }
 98 // 相同元素
 99 void MainWindow::on_pushButton_15_clicked()
100 {
101     int nCount = m_pList.count("4");// 列表中有几个这样的元素
102     ui->textEdit->append(QString::number(nCount));
103 }
104 // 删除元素
105 void MainWindow::on_pushButton_16_clicked()
106 {
107     QString str = m_pList.takeAt(2); //删除第3个 并返回结果
108     ui->textEdit->append(str);
109 }
110 // 删除元素2
111 void MainWindow::on_pushButton_14_clicked()
112 {
113     m_pList.removeAt(3);
114 }
115 // 修改元素值
116 void MainWindow::on_pushButton_17_clicked()
117 {
118     m_pList.replace(2,"bc");// 参数1 元素下标,参数2 修改的结果值
119 }
120 // 修改元素值2
121 void MainWindow::on_pushButton_18_clicked()
122 {
123     m_pList[2] = "opopo";
124 }
125 // 所引遍历元素
126 void MainWindow::on_pushButton_20_clicked()
127 {
128     QString str = "%1  %2  %3";
129     for(int i = 0; i < m_pList.size(); ++i)
130     {
131         // at()操作比操作符[]更快,因为它不需要深度复制
132         ui->textEdit->append(str.arg(m_pList[i]).arg(m_pList.at(i)).arg(m_pList.value(i)));
133     }
134 }
135 // 迭代遍历元素
136 void MainWindow::on_pushButton_19_clicked()
137 {
138     QString str = "it  %1";
139     QList::iterator it;
140     for(it = m_pList.begin(); it != m_pList.end(); ++it)
141     {
142         ui->textEdit->append(str.arg((*it)));
143     }
144 }
145 // 清空
146 void MainWindow::on_pushButton_21_clicked()
147 {
148     m_pList.clear();
149 }
150 // 迭代删除
151 void MainWindow::on_pushButton_22_clicked()
152 {
153     QString str = "it  %1";
154     QList::iterator it;
155     for(it = m_pList.begin(); it != m_pList.end(); ++it)
156     {
157         if((*it)== "00")
158         {
159             it = m_pList.erase(it);// 删除从起始位置到结束位置的元素
160         }
161         ui->textEdit->append(str.arg((*it)));
162     }
163 }
164 // 迭代区间删除
165 void MainWindow::on_pushButton_23_clicked()
166 {
167     QList::iterator it;
168     it = m_pList.begin();
169     it = m_pList.erase(it, it+3);
170 }
171 // 删除多个
172 void MainWindow::on_pushButton_24_clicked()
173 {
174     m_pList.removeAll("2");
175 }
176 // 删除最前值
177 void MainWindow::on_pushButton_25_clicked()
178 {
179     m_pList.removeFirst();
180 }
181 // 删除最后值
182 void MainWindow::on_pushButton_26_clicked()
183 {
184     m_pList.removeLast();
185 }
186 // 删除一个
187 void MainWindow::on_pushButton_27_clicked()
188 {
189     m_pList.removeOne("2");
190 }

mainwindow.ui

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 "4.0">
  3  <class>MainWindowclass>
  4  class="QMainWindow" name="MainWindow">
  5   "geometry">
  6    
  7     0
  8     0
  9     727
 10     433
 11    
 12   
 13   "windowTitle">
 14    <string>MainWindowstring>
 15   
 16   class="QWidget" name="centralwidget">
 17    class="QHBoxLayout" name="horizontalLayout">
 18     
 19      class="QVBoxLayout" name="verticalLayout">
 20       
 21        class="QLabel" name="label">
 22         "text">
 23          <string><html><head/><body><p>特点:支持随机访问,基于索引,中间插入或移除项速度快速</p></body></html>string>
 24         
 25        
 26       
 27       
 28        class="QLabel" name="label_2">
 29         "text">
 30          <string><html><head/><body><p>注意:访问QList时,value(int i)查不到此值时会返回一个默认值0,at(int i)则会引起崩溃。</p></body></html>string>
 31         
 32        
 33       
 34       
 35        class="QLabel" name="label_3">
 36         "text">
 37          <string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
 38 <html><head><meta name="qrichtext" content="1" /><style type="text/css"&gt;
 39 p, li { white-space: pre-wrap; }
 40 </style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;"&gt;
 41 <p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">源文:https://blog.csdn.net/ligare/article/details/122687947</p></body></html>
 42         
 43        
 44       
 45       
 46        class="QLabel" name="label_4">
 47         "text">
 48          <string><html><head/><body><p>原文:https://blog.csdn.net/jpchen609/article/details/4371594</p></body></html>
 49         
 50        
 51       
 52       
 53        class="QTextEdit" name="textEdit"/>
 54       
 55       
 56        class="QHBoxLayout" name="horizontalLayout_2">
 57         
 58          class="QPushButton" name="pushButton_4">
 59           "toolTipDuration">
 60            -14
 61           
 62           "text">
 63            <string>添加元素方式1string>
 64           
 65          
 66         
 67         
 68          class="QPushButton" name="pushButton_3">
 69           "text">
 70            <string>添加元素方式2string>
 71           
 72          
 73         
 74         
 75          class="QPushButton" name="pushButton_2">
 76           "text">
 77            <string>添加到头部string>
 78           
 79          
 80         
 81         
 82          class="QPushButton" name="pushButton">
 83           "text">
 84            <string>插入元素string>
 85           
 86          
 87         
 88         
 89          "horizontalSpacer_2">
 90           "orientation">
 91            <enum>Qt::Horizontalenum>
 92           
 93           "sizeHint" stdset="0">
 94            
 95             40
 96             20
 97            
 98           
 99          
100         
101        
102       
103       
104        class="QHBoxLayout" name="horizontalLayout_3">
105         
106          class="QPushButton" name="pushButton_8">
107           "text">
108            <string>交换位置string>
109           
110          
111         
112         
113          class="QPushButton" name="pushButton_7">
114           "text">
115            <string>移动位置string>
116           
117          
118         
119         
120          "horizontalSpacer">
121           "orientation">
122            <enum>Qt::Horizontalenum>
123           
124           "sizeType">
125            <enum>QSizePolicy::Expandingenum>
126           
127           "sizeHint" stdset="0">
128            
129             40
130             20
131            
132           
133          
134         
135        
136       
137       
138        class="QHBoxLayout" name="horizontalLayout_5">
139         
140          class="QPushButton" name="pushButton_10">
141           "text">
142            <string>查找元素string>
143           
144          
145         
146         
147          class="QPushButton" name="pushButton_9">
148           "text">
149            <string>最后一个元素string>
150           
151          
152         
153         
154          class="QPushButton" name="pushButton_5">
155           "text">
156            <string>第一个元素string>
157           
158          
159         
160         
161          class="QPushButton" name="pushButton_6">
162           "text">
163            <string>第一个元素2string>
164           
165          
166         
167         
168          class="QPushButton" name="pushButton_11">
169           "text">
170            <string>是否存在string>
171           
172          
173         
174         
175          "horizontalSpacer_3">
176           "orientation">
177            <enum>Qt::Horizontalenum>
178           
179           "sizeHint" stdset="0">
180            
181             40
182             20
183            
184           
185          
186         
187        
188       
189       
190        class="QHBoxLayout" name="horizontalLayout_9">
191         
192          class="QPushButton" name="pushButton_13">
193           "text">
194            <string>总元素string>
195           
196          
197         
198         
199          class="QPushButton" name="pushButton_12">
200           "text">
201            <string>总元素2string>
202           
203          
204         
205         
206          class="QPushButton" name="pushButton_15">
207           "text">
208            <string>相同元素string>
209           
210          
211         
212         
213          "horizontalSpacer_4">
214           "orientation">
215            <enum>Qt::Horizontalenum>
216           
217           "sizeHint" stdset="0">
218            
219             40
220             20
221            
222           
223          
224         
225        
226       
227       
228        class="QHBoxLayout" name="horizontalLayout_11">
229         
230          class="QPushButton" name="pushButton_16">
231           "text">
232            <string>删除元素string>
233           
234          
235         
236         
237          class="QPushButton" name="pushButton_14">
238           "text">
239            <string>删除元素2string>
240           
241          
242         
243         
244          class="QPushButton" name="pushButton_27">
245           "text">
246            <string>删除一个string>
247           
248          
249         
250         
251          class="QPushButton" name="pushButton_24">
252           "text">
253            <string>删除多个string>
254           
255          
256         
257         
258          class="QPushButton" name="pushButton_25">
259           "text">
260            <string>删除最前值string>
261           
262          
263         
264         
265          class="QPushButton" name="pushButton_26">
266           "text">
267            <string>删除最后值string>
268           
269          
270         
271         
272          class="QPushButton" name="pushButton_21">
273           "text">
274            <string>清空string>
275           
276          
277         
278         
279          class="QPushButton" name="pushButton_22">
280           "text">
281            <string>迭代删除string>
282           
283          
284         
285         
286          class="QPushButton" name="pushButton_23">
287           "text">
288            <string>迭代区间删除string>
289           
290          
291         
292         
293          "horizontalSpacer_5">
294           "orientation">
295            <enum>Qt::Horizontalenum>
296           
297           "sizeHint" stdset="0">
298            
299             40
300             20
301            
302           
303          
304         
305        
306       
307       
308        class="QHBoxLayout" name="horizontalLayout_13">
309         
310          class="QPushButton" name="pushButton_17">
311           "text">
312            <string>修改元素值string>
313           
314          
315         
316         
317          class="QPushButton" name="pushButton_18">
318           "text">
319            <string>修改元素值2string>
320           
321          
322         
323         
324          "horizontalSpacer_6">
325           "orientation">
326            <enum>Qt::Horizontalenum>
327           
328           "sizeHint" stdset="0">
329            
330             40
331             20
332            
333           
334          
335         
336        
337       
338       
339        class="QHBoxLayout" name="horizontalLayout_17">
340         
341          class="QPushButton" name="pushButton_20">
342           "text">
343            <string>所引遍历元素string>
344           
345          
346         
347         
348          class="QPushButton" name="pushButton_19">
349           "text">
350            <string>迭代遍历元素string>
351           
352          
353         
354         
355          "horizontalSpacer_7">
356           "orientation">
357            <enum>Qt::Horizontalenum>
358           
359           "sizeHint" stdset="0">
360            
361             40
362             20
363            
364           
365          
366         
367        
368       
369      
370     
371    
372   
373  
374  
375  
376 

搜索

复制

相关