其他分享
首页 > 其他分享> > QtQ之QMap的使用分析

QtQ之QMap的使用分析

作者:互联网

相关资料:

https://blog.csdn.net/markely/article/details/8055961            原文
https://blog.csdn.net/asd1147170607/article/details/105616292          原文
https://download.csdn.net/download/zhujianqiangqq/85798434          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 #include <QDebug>
 6 
 7 QT_BEGIN_NAMESPACE
 8 namespace Ui { class MainWindow; }
 9 QT_END_NAMESPACE
10 
11 class MainWindow : public QMainWindow
12 {
13     Q_OBJECT
14 
15 public:
16     MainWindow(QWidget *parent = nullptr);
17     ~MainWindow();
18 
19 private slots:
20     void on_pushButton_5_clicked();
21 
22     void on_pushButton_4_clicked();
23 
24     void on_pushButton_10_clicked();
25 
26     void on_pushButton_3_clicked();
27 
28     void on_pushButton_7_clicked();
29 
30     void on_pushButton_6_clicked();
31 
32     void on_pushButton_11_clicked();
33 
34     void on_pushButton_12_clicked();
35 
36     void on_pushButton_13_clicked();
37 
38     void on_pushButton_14_clicked();
39 
40     void on_pushButton_9_clicked();
41 
42     void on_pushButton_8_clicked();
43 
44     void on_pushButton_17_clicked();
45 
46     void on_pushButton_18_clicked();
47 
48     void on_pushButton_2_clicked();
49 
50     void on_pushButton_clicked();
51 
52     void on_pushButton_19_clicked();
53 
54     void on_pushButton_20_clicked();
55 
56     void on_pushButton_21_clicked();
57 
58     void on_pushButton_16_clicked();
59 
60     void on_pushButton_15_clicked();
61 
62     void on_pushButton_22_clicked();
63 
64     void on_pushButton_25_clicked();
65 
66     void on_pushButton_24_clicked();
67 
68     void on_pushButton_23_clicked();
69 
70 private:
71     Ui::MainWindow *ui;
72     QMap<QString, int> m_pMap;
73 };
74 #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之QMap的使用分析"));
 11 }
 12 
 13 MainWindow::~MainWindow()
 14 {
 15     delete ui;
 16 }
 17 
 18 void MainWindow::on_pushButton_5_clicked()
 19 {
 20     m_pMap.insert("key 2", 2);
 21     m_pMap.insert("key 0", 0);
 22     m_pMap.insert("key 1", 1);
 23 }
 24 
 25 void MainWindow::on_pushButton_4_clicked()
 26 {
 27     m_pMap["333"] = 333;
 28 }
 29 
 30 void MainWindow::on_pushButton_10_clicked()
 31 {
 32     m_pMap.insertMulti("three",4);
 33     m_pMap.insertMulti("three",5);
 34     m_pMap.insertMulti("three",6);
 35 }
 36 
 37 void MainWindow::on_pushButton_3_clicked()
 38 {
 39     if(!m_pMap.contains("333"))
 40         ui->textEdit->append("contains:true");
 41     else
 42         ui->textEdit->append("contains:false");
 43 }
 44 
 45 void MainWindow::on_pushButton_7_clicked()
 46 {
 47     ui->textEdit->append("map[333]:" + QString::number(m_pMap["333"]));
 48 }
 49 
 50 void MainWindow::on_pushButton_6_clicked()
 51 {
 52     QList<QString> skeys = m_pMap.keys();
 53     for(int i = 0; i< skeys.count(); i++)
 54     {
 55         ui->textEdit->append("skeys:" + skeys[i]);
 56     }
 57 }
 58 
 59 void MainWindow::on_pushButton_11_clicked()
 60 {
 61     QList<QString> skeys2 = m_pMap.keys(4);
 62     for(int i=0; i < skeys2.count(); i++)
 63     {
 64     ui->textEdit->append("skeys2:" + skeys2[i]);
 65     }
 66 }
 67 
 68 void MainWindow::on_pushButton_12_clicked()
 69 {
 70     QList<int> svalues = m_pMap.values();
 71     for(int i = 0; i < svalues.count(); i++)
 72     {
 73         ui->textEdit->append("svalues:" + QString::number(svalues[i]));
 74     }
 75 }
 76 
 77 void MainWindow::on_pushButton_13_clicked()
 78 {
 79     QList<int> svalues2 = m_pMap.values("three");
 80     for(int i=0; i<svalues2.count(); i++)
 81     {
 82         ui->textEdit->append("svalues2:" + QString::number(svalues2[i]));
 83     }
 84 }
 85 
 86 void MainWindow::on_pushButton_14_clicked()
 87 {
 88     QMapIterator<QString, int> it(m_pMap);
 89     while(it.hasNext())
 90     {
 91         it.next();
 92         ui->textEdit->append("QMapIterator:" + it.key() + " : " + QString::number(it.value()));
 93     }
 94 }
 95 
 96 void MainWindow::on_pushButton_9_clicked()
 97 {
 98     ui->textEdit->append("size:" + QString::number(m_pMap.size()));
 99 }
100 
101 void MainWindow::on_pushButton_8_clicked()
102 {
103     ui->textEdit->append("count:" + QString::number(m_pMap.count()));
104 }
105 
106 void MainWindow::on_pushButton_17_clicked()
107 {
108     ui->textEdit->append("isEmpty:" + QString::number(m_pMap.isEmpty()));
109 }
110 
111 void MainWindow::on_pushButton_18_clicked()
112 {
113     QMap<QString,int>::iterator it = m_pMap.find("333");
114     ui->textEdit->append("find 333:" + QString::number(it.value()));
115 }
116 
117 void MainWindow::on_pushButton_2_clicked()
118 {
119     QList <QString> suniqueKeys = m_pMap.uniqueKeys();
120     for(int i = 0; i < suniqueKeys.count(); i++)
121     {
122         ui->textEdit->append("uniqueKeys:" + suniqueKeys[i]);
123     }
124 }
125 
126 void MainWindow::on_pushButton_clicked()
127 {
128     int nfirst = m_pMap.first();
129     ui->textEdit->append("first:" + QString::number(nfirst));
130 }
131 
132 void MainWindow::on_pushButton_19_clicked()
133 {
134     int nlast = m_pMap.last();
135     ui->textEdit->append("last:" + QString::number(nlast));
136 }
137 
138 void MainWindow::on_pushButton_20_clicked()
139 {
140     QString firstKey = m_pMap.firstKey();
141     ui->textEdit->append("firstKey:" + firstKey);
142 }
143 
144 void MainWindow::on_pushButton_21_clicked()
145 {
146     QString lastKey = m_pMap.lastKey();
147     ui->textEdit->append("lastKey:" + lastKey);
148 }
149 // 用Java风格迭代器进行遍历
150 void MainWindow::on_pushButton_16_clicked()
151 {
152     QMapIterator<QString,int> i(m_pMap);
153     i.toFront();
154     while(i.hasNext())
155     {
156         ui->textEdit->append("hasNext:" + i.key() + " : " + QString::number(i.next().value()));
157     }
158     i.toBack();
159     while(i.hasPrevious())
160     {
161         ui->textEdit->append("hasPrevious:" + i.key() + " : " + QString::number(i.previous().value()));
162     }
163 
164 //    bool jjj = i.findNext(2);
165 //    if (jjj)
166 //    qDebug() << "jjj: " << i.key() << i.value();
167 
168 //    bool kkk  = i.findPrevious(2);
169 //    if (kkk )
170 //    qDebug() << "kkk : " << i.key() << i.value();
171 }
172 // 用STL风格迭代器进行遍历
173 void MainWindow::on_pushButton_15_clicked()
174 {
175     QMap<QString, int>::const_iterator constI;
176     for(constI = m_pMap.cbegin(); constI != m_pMap.cend(); constI++)
177         ui->textEdit->append("constI:" + constI.key() + " : " + QString::number(constI.value()));
178 
179     QMap<QString,int>::iterator itI;
180     for(itI = m_pMap.begin();itI != m_pMap.end();itI++)
181         ui->textEdit->append("itI:" + itI.key() + " : " + QString::number(itI.value()));
182 }
183 // 用foreach进行遍历
184 void MainWindow::on_pushButton_22_clicked()
185 {
186     foreach(const QString &str, m_pMap.keys())
187         ui->textEdit->append("foreach: " + str + " : " + QString::number(m_pMap.value(str)));
188     // 对于多值映射,可以使用两重foreach语句
189     foreach(const QString &str, m_pMap.uniqueKeys())
190     {
191         foreach(int i, m_pMap.values(str))
192             ui->textEdit->append("uniqueKeys: " + str + " : " + QString::number(i));
193     }
194 }
195 
196 void MainWindow::on_pushButton_25_clicked()
197 {
198     m_pMap.take("333");
199     ui->textEdit->append("take: " + QString::number(m_pMap.count()));
200 }
201 
202 void MainWindow::on_pushButton_24_clicked()
203 {
204     m_pMap.remove("key 2");
205     ui->textEdit->append("remove: " + QString::number(m_pMap.count()));
206 }
207 
208 void MainWindow::on_pushButton_23_clicked()
209 {
210     m_pMap.clear();
211     ui->textEdit->append("clear: " + QString::number(m_pMap.count()));
212 }
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>710</width>
 10     <height>313</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="QTextEdit" name="textEdit"/>
 22       </item>
 23       <item>
 24        <layout class="QHBoxLayout" name="horizontalLayout_4">
 25         <item>
 26          <widget class="QPushButton" name="pushButton_5">
 27           <property name="text">
 28            <string>增加元素方式1</string>
 29           </property>
 30          </widget>
 31         </item>
 32         <item>
 33          <widget class="QPushButton" name="pushButton_4">
 34           <property name="text">
 35            <string>增加元素方式2</string>
 36           </property>
 37          </widget>
 38         </item>
 39         <item>
 40          <widget class="QPushButton" name="pushButton_10">
 41           <property name="text">
 42            <string>增加元素方式3</string>
 43           </property>
 44          </widget>
 45         </item>
 46        </layout>
 47       </item>
 48       <item>
 49        <layout class="QHBoxLayout" name="horizontalLayout_2">
 50         <item>
 51          <widget class="QPushButton" name="pushButton_3">
 52           <property name="text">
 53            <string>是否已经包含</string>
 54           </property>
 55          </widget>
 56         </item>
 57         <item>
 58          <widget class="QPushButton" name="pushButton">
 59           <property name="text">
 60            <string>最小键对应的数据</string>
 61           </property>
 62          </widget>
 63         </item>
 64         <item>
 65          <widget class="QPushButton" name="pushButton_19">
 66           <property name="text">
 67            <string>最大键对应的数据</string>
 68           </property>
 69          </widget>
 70         </item>
 71         <item>
 72          <widget class="QPushButton" name="pushButton_20">
 73           <property name="text">
 74            <string>最小键</string>
 75           </property>
 76          </widget>
 77         </item>
 78         <item>
 79          <widget class="QPushButton" name="pushButton_21">
 80           <property name="text">
 81            <string>最大键</string>
 82           </property>
 83          </widget>
 84         </item>
 85        </layout>
 86       </item>
 87       <item>
 88        <layout class="QHBoxLayout" name="horizontalLayout_5">
 89         <item>
 90          <widget class="QPushButton" name="pushButton_7">
 91           <property name="text">
 92            <string>取值1</string>
 93           </property>
 94          </widget>
 95         </item>
 96         <item>
 97          <widget class="QPushButton" name="pushButton_6">
 98           <property name="text">
 99            <string>取Keys</string>
100           </property>
101          </widget>
102         </item>
103         <item>
104          <widget class="QPushButton" name="pushButton_11">
105           <property name="text">
106            <string>取Keys2</string>
107           </property>
108          </widget>
109         </item>
110         <item>
111          <widget class="QPushButton" name="pushButton_12">
112           <property name="text">
113            <string>取values</string>
114           </property>
115          </widget>
116         </item>
117         <item>
118          <widget class="QPushButton" name="pushButton_13">
119           <property name="text">
120            <string>取values2</string>
121           </property>
122          </widget>
123         </item>
124         <item>
125          <widget class="QPushButton" name="pushButton_14">
126           <property name="text">
127            <string>QMapIterator</string>
128           </property>
129          </widget>
130         </item>
131         <item>
132          <widget class="QPushButton" name="pushButton_18">
133           <property name="text">
134            <string>查找find</string>
135           </property>
136          </widget>
137         </item>
138         <item>
139          <widget class="QPushButton" name="pushButton_2">
140           <property name="text">
141            <string>uniqueKeys</string>
142           </property>
143          </widget>
144         </item>
145        </layout>
146       </item>
147       <item>
148        <layout class="QHBoxLayout" name="horizontalLayout_6">
149         <item>
150          <widget class="QPushButton" name="pushButton_9">
151           <property name="text">
152            <string>总元素size</string>
153           </property>
154          </widget>
155         </item>
156         <item>
157          <widget class="QPushButton" name="pushButton_8">
158           <property name="text">
159            <string>总元素count</string>
160           </property>
161          </widget>
162         </item>
163         <item>
164          <widget class="QPushButton" name="pushButton_17">
165           <property name="text">
166            <string>判空isEmpty</string>
167           </property>
168          </widget>
169         </item>
170        </layout>
171       </item>
172       <item>
173        <layout class="QHBoxLayout" name="horizontalLayout_7">
174         <item>
175          <widget class="QPushButton" name="pushButton_16">
176           <property name="text">
177            <string>Java风格迭代器</string>
178           </property>
179          </widget>
180         </item>
181         <item>
182          <widget class="QPushButton" name="pushButton_15">
183           <property name="text">
184            <string>STL风格迭代器</string>
185           </property>
186          </widget>
187         </item>
188         <item>
189          <widget class="QPushButton" name="pushButton_22">
190           <property name="text">
191            <string>foreach</string>
192           </property>
193          </widget>
194         </item>
195        </layout>
196       </item>
197       <item>
198        <layout class="QHBoxLayout" name="horizontalLayout_8">
199         <item>
200          <widget class="QPushButton" name="pushButton_25">
201           <property name="text">
202            <string>取走take</string>
203           </property>
204          </widget>
205         </item>
206         <item>
207          <widget class="QPushButton" name="pushButton_24">
208           <property name="text">
209            <string>移除remove</string>
210           </property>
211          </widget>
212         </item>
213         <item>
214          <widget class="QPushButton" name="pushButton_23">
215           <property name="text">
216            <string>清空clear</string>
217           </property>
218          </widget>
219         </item>
220        </layout>
221       </item>
222      </layout>
223     </item>
224    </layout>
225   </widget>
226  </widget>
227  <resources/>
228  <connections/>
229 </ui>
View Code

 

 

 

搜索

复制

<iframe></iframe>

标签:分析,clicked,QMap,QtQ,pushButton,MainWindow,ui,pMap,void
来源: https://www.cnblogs.com/FKdelphi/p/16415864.html