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