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