Qt-Qt之QHash的使用分析


相关资料:

https://blog.csdn.net/wsp_1138886114/article/details/123480932

https://download.csdn.net/download/zhujianqiangqq/85798799 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 #include 
 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_4_clicked();
21 
22     void on_pushButton_3_clicked();
23 
24     void on_pushButton_13_clicked();
25 
26     void on_pushButton_2_clicked();
27 
28     void on_pushButton_6_clicked();
29 
30     void on_pushButton_5_clicked();
31 
32     void on_pushButton_14_clicked();
33 
34     void on_pushButton_16_clicked();
35 
36     void on_pushButton_15_clicked();
37 
38     void on_pushButton_clicked();
39 
40     void on_pushButton_17_clicked();
41 
42     void on_pushButton_18_clicked();
43 
44     void on_pushButton_8_clicked();
45 
46     void on_pushButton_7_clicked();
47 
48     void on_pushButton_19_clicked();
49 
50     void on_pushButton_10_clicked();
51 
52     void on_pushButton_9_clicked();
53 
54     void on_pushButton_20_clicked();
55 
56     void on_pushButton_12_clicked();
57 
58     void on_pushButton_11_clicked();
59 
60     void on_pushButton_21_clicked();
61 
62 private:
63     Ui::MainWindow *ui;
64     QHashint> m_pHash;
65 };
66 #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     setWindowTitle(QStringLiteral("Qt之QHash的使用分析"));
 10 }
 11 
 12 MainWindow::~MainWindow()
 13 {
 14     delete ui;
 15 }
 16 
 17 void MainWindow::on_pushButton_4_clicked()
 18 {
 19     m_pHash.insert("key 2", 2);
 20     m_pHash.insert("key 0", 0);
 21     m_pHash.insert("key 1", 1);
 22 }
 23 
 24 void MainWindow::on_pushButton_3_clicked()
 25 {
 26     m_pHash["333"] = 333;
 27 }
 28 
 29 void MainWindow::on_pushButton_13_clicked()
 30 {
 31     m_pHash.insertMulti("three",4);
 32     m_pHash.insertMulti("three",5);
 33     m_pHash.insertMulti("three",6);
 34 }
 35 
 36 void MainWindow::on_pushButton_2_clicked()
 37 {
 38     if(!m_pHash.contains("333"))
 39         ui->textEdit->append("contains:true");
 40     else
 41         ui->textEdit->append("contains:false");
 42 }
 43 
 44 void MainWindow::on_pushButton_6_clicked()
 45 {
 46     ui->textEdit->append("m_pHash[333]:" + QString::number(m_pHash["333"]));
 47 }
 48 
 49 void MainWindow::on_pushButton_5_clicked()
 50 {
 51     QList skeys = m_pHash.keys();
 52     for(int i = 0; i< skeys.count(); i++)
 53     {
 54         ui->textEdit->append("skeys:" + skeys[i]);
 55     }
 56 }
 57 
 58 void MainWindow::on_pushButton_14_clicked()
 59 {
 60     QList skeys2 = m_pHash.keys(4);
 61     for(int i=0; i < skeys2.count(); i++)
 62     {
 63         ui->textEdit->append("skeys2:" + skeys2[i]);
 64     }
 65 }
 66 
 67 void MainWindow::on_pushButton_16_clicked()
 68 {
 69     QList<int> svalues = m_pHash.values();
 70     for(int i = 0; i < svalues.count(); i++)
 71     {
 72         ui->textEdit->append("svalues:" + QString::number(svalues[i]));
 73     }
 74 }
 75 
 76 void MainWindow::on_pushButton_15_clicked()
 77 {
 78     QList<int> svalues2 = m_pHash.values("three");
 79     for(int i=0; i)
 80     {
 81         ui->textEdit->append("svalues2:" + QString::number(svalues2[i]));
 82     }
 83 }
 84 
 85 void MainWindow::on_pushButton_clicked()
 86 {
 87     QHashIteratorint> it(m_pHash);
 88     while(it.hasNext())
 89     {
 90         it.next();
 91         ui->textEdit->append("QHashIterator:" + it.key() + " : " + QString::number(it.value()));
 92     }
 93 }
 94 
 95 void MainWindow::on_pushButton_17_clicked()
 96 {
 97     QHashint>::iterator it = m_pHash.find("333");
 98     ui->textEdit->append("find 333:" + QString::number(it.value()));
 99 }
100 
101 void MainWindow::on_pushButton_18_clicked()
102 {
103     QList  suniqueKeys = m_pHash.uniqueKeys();
104     for(int i = 0; i < suniqueKeys.count(); i++)
105     {
106         ui->textEdit->append("uniqueKeys:" + suniqueKeys[i]);
107     }
108 }
109 
110 void MainWindow::on_pushButton_8_clicked()
111 {
112     ui->textEdit->append("size:" + QString::number(m_pHash.size()));
113 }
114 
115 void MainWindow::on_pushButton_7_clicked()
116 {
117     ui->textEdit->append("count:" + QString::number(m_pHash.count()));
118 }
119 
120 void MainWindow::on_pushButton_19_clicked()
121 {
122     ui->textEdit->append("isEmpty:" + QString::number(m_pHash.isEmpty()));
123 }
124 // 用Java风格迭代器进行遍历
125 void MainWindow::on_pushButton_10_clicked()
126 {
127     QHashIteratorint> i(m_pHash);
128     i.toFront();
129     while(i.hasNext())
130     {
131         ui->textEdit->append("hasNext:" + i.key() + " : " + QString::number(i.next().value()));
132     }
133     i.toBack();
134     while(i.hasPrevious())
135     {
136         ui->textEdit->append("hasPrevious:" + i.key() + " : " + QString::number(i.previous().value()));
137     }
138     //    bool jjj = i.findNext(2);
139     //    if (jjj)
140     //    qDebug() << "jjj: " << i.key() << i.value();
141 
142     //    bool kkk  = i.findPrevious(2);
143     //    if (kkk )
144     //    qDebug() << "kkk : " << i.key() << i.value();
145 }
146 // 用STL风格迭代器进行遍历
147 void MainWindow::on_pushButton_9_clicked()
148 {
149     QHashint>::const_iterator constI;
150     for(constI = m_pHash.cbegin(); constI != m_pHash.cend(); constI++)
151         ui->textEdit->append("constI:" + constI.key() + " : " + QString::number(constI.value()));
152 
153     QHashint>::iterator itI;
154     for(itI = m_pHash.begin();itI != m_pHash.end();itI++)
155         ui->textEdit->append("itI:" + itI.key() + " : " + QString::number(itI.value()));
156 }
157 // 用foreach进行遍历
158 void MainWindow::on_pushButton_20_clicked()
159 {
160     foreach(const QString &str, m_pHash.keys())
161         ui->textEdit->append("foreach: " + str + " : " + QString::number(m_pHash.value(str)));
162     // 对于多值映射,可以使用两重foreach语句
163     foreach(const QString &str, m_pHash.uniqueKeys())
164     {
165         foreach(int i, m_pHash.values(str))
166             ui->textEdit->append("uniqueKeys: " + str + " : " + QString::number(i));
167     }
168 }
169 
170 void MainWindow::on_pushButton_12_clicked()
171 {
172     m_pHash.take("333");
173     ui->textEdit->append("take: " + QString::number(m_pHash.count()));
174 }
175 
176 void MainWindow::on_pushButton_11_clicked()
177 {
178     m_pHash.remove("key 2");
179     ui->textEdit->append("remove: " + QString::number(m_pHash.count()));
180 }
181 
182 void MainWindow::on_pushButton_21_clicked()
183 {
184     m_pHash.clear();
185     ui->textEdit->append("clear: " + QString::number(m_pHash.count()));
186 }

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     704
 10     281
 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="QTextEdit" name="textEdit"/>
 22       
 23       
 24        class="QHBoxLayout" name="horizontalLayout_3">
 25         
 26          class="QPushButton" name="pushButton_4">
 27           "text">
 28            <string>增加元素1string>
 29           
 30          
 31         
 32         
 33          class="QPushButton" name="pushButton_3">
 34           "text">
 35            <string>增加元素2string>
 36           
 37          
 38         
 39         
 40          class="QPushButton" name="pushButton_13">
 41           "text">
 42            <string>增加元素3string>
 43           
 44          
 45         
 46        
 47       
 48       
 49        class="QHBoxLayout" name="horizontalLayout_2">
 50         
 51          class="QPushButton" name="pushButton_2">
 52           "text">
 53            <string>是否已经包含string>
 54           
 55          
 56         
 57         
 58          "horizontalSpacer">
 59           "orientation">
 60            <enum>Qt::Horizontalenum>
 61           
 62           "sizeHint" stdset="0">
 63            
 64             40
 65             20
 66            
 67           
 68          
 69         
 70        
 71       
 72       
 73        class="QHBoxLayout" name="horizontalLayout_4">
 74         
 75          class="QPushButton" name="pushButton_6">
 76           "text">
 77            <string>取值string>
 78           
 79          
 80         
 81         
 82          class="QPushButton" name="pushButton_5">
 83           "text">
 84            <string>取Keysstring>
 85           
 86          
 87         
 88         
 89          class="QPushButton" name="pushButton_14">
 90           "text">
 91            <string>取Keys2string>
 92           
 93          
 94         
 95         
 96          class="QPushButton" name="pushButton_16">
 97           "text">
 98            <string>取valuesstring>
 99           
100          
101         
102         
103          class="QPushButton" name="pushButton_15">
104           "text">
105            <string>取values2string>
106           
107          
108         
109         
110          class="QPushButton" name="pushButton">
111           "text">
112            <string>QMapIteratorstring>
113           
114          
115         
116         
117          class="QPushButton" name="pushButton_17">
118           "text">
119            <string>查找findstring>
120           
121          
122         
123         
124          class="QPushButton" name="pushButton_18">
125           "text">
126            <string>uniqueKeysstring>
127           
128          
129         
130        
131       
132       
133        class="QHBoxLayout" name="horizontalLayout_5">
134         
135          class="QPushButton" name="pushButton_8">
136           "text">
137            <string>总元素sizestring>
138           
139          
140         
141         
142          class="QPushButton" name="pushButton_7">
143           "text">
144            <string>总元素countstring>
145           
146          
147         
148         
149          class="QPushButton" name="pushButton_19">
150           "text">
151            <string>判空isEmptystring>
152           
153          
154         
155        
156       
157       
158        class="QHBoxLayout" name="horizontalLayout_6">
159         
160          class="QPushButton" name="pushButton_10">
161           "text">
162            <string>Java风格迭代器string>
163           
164          
165         
166         
167          class="QPushButton" name="pushButton_9">
168           "text">
169            <string>STL风格迭代器string>
170           
171          
172         
173         
174          class="QPushButton" name="pushButton_20">
175           "text">
176            <string>foreachstring>
177           
178          
179         
180        
181       
182       
183        class="QHBoxLayout" name="horizontalLayout_7">
184         
185          class="QPushButton" name="pushButton_12">
186           "text">
187            <string>取走takestring>
188           
189          
190         
191         
192          class="QPushButton" name="pushButton_11">
193           "text">
194            <string>移除removestring>
195           
196          
197         
198         
199          class="QPushButton" name="pushButton_21">
200           "text">
201            <string>清空clearstring>
202           
203          
204         
205        
206       
207      
208     
209    
210   
211  
212  
213  
214 

搜索

复制

相关