ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Qt-Qt之QList使用

2022-06-14 19:04:21  阅读:172  来源: 互联网

标签:gt clicked Qt pushButton MainWindow lt 使用 QList void


相关资料:

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
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 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 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 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<QString> m_pList;// 定义成员
76 };
77 #endif // MAINWINDOW_H
View Code

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<QString>::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<QString>::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<QString>::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 }
View Code

mainwindow.ui

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

 

 

搜索

复制

<iframe height="240" width="320"></iframe>

标签:gt,clicked,Qt,pushButton,MainWindow,lt,使用,QList,void
来源: https://www.cnblogs.com/FKdelphi/p/16375918.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有