其他分享
首页 > 其他分享> > Qt-Qt之QHash的使用分析

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
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_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<QString, int> m_pHash;
65 };
66 #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     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<QString> 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<QString> 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<svalues2.count(); i++)
 80     {
 81         ui->textEdit->append("svalues2:" + QString::number(svalues2[i]));
 82     }
 83 }
 84 
 85 void MainWindow::on_pushButton_clicked()
 86 {
 87     QHashIterator<QString, 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<QString,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 <QString> 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<QString,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<QString, 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<QString,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 }
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>704</width>
 10     <height>281</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_3">
 25         <item>
 26          <widget class="QPushButton" name="pushButton_4">
 27           <property name="text">
 28            <string>增加元素1</string>
 29           </property>
 30          </widget>
 31         </item>
 32         <item>
 33          <widget class="QPushButton" name="pushButton_3">
 34           <property name="text">
 35            <string>增加元素2</string>
 36           </property>
 37          </widget>
 38         </item>
 39         <item>
 40          <widget class="QPushButton" name="pushButton_13">
 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_2">
 52           <property name="text">
 53            <string>是否已经包含</string>
 54           </property>
 55          </widget>
 56         </item>
 57         <item>
 58          <spacer name="horizontalSpacer">
 59           <property name="orientation">
 60            <enum>Qt::Horizontal</enum>
 61           </property>
 62           <property name="sizeHint" stdset="0">
 63            <size>
 64             <width>40</width>
 65             <height>20</height>
 66            </size>
 67           </property>
 68          </spacer>
 69         </item>
 70        </layout>
 71       </item>
 72       <item>
 73        <layout class="QHBoxLayout" name="horizontalLayout_4">
 74         <item>
 75          <widget class="QPushButton" name="pushButton_6">
 76           <property name="text">
 77            <string>取值</string>
 78           </property>
 79          </widget>
 80         </item>
 81         <item>
 82          <widget class="QPushButton" name="pushButton_5">
 83           <property name="text">
 84            <string>取Keys</string>
 85           </property>
 86          </widget>
 87         </item>
 88         <item>
 89          <widget class="QPushButton" name="pushButton_14">
 90           <property name="text">
 91            <string>取Keys2</string>
 92           </property>
 93          </widget>
 94         </item>
 95         <item>
 96          <widget class="QPushButton" name="pushButton_16">
 97           <property name="text">
 98            <string>取values</string>
 99           </property>
100          </widget>
101         </item>
102         <item>
103          <widget class="QPushButton" name="pushButton_15">
104           <property name="text">
105            <string>取values2</string>
106           </property>
107          </widget>
108         </item>
109         <item>
110          <widget class="QPushButton" name="pushButton">
111           <property name="text">
112            <string>QMapIterator</string>
113           </property>
114          </widget>
115         </item>
116         <item>
117          <widget class="QPushButton" name="pushButton_17">
118           <property name="text">
119            <string>查找find</string>
120           </property>
121          </widget>
122         </item>
123         <item>
124          <widget class="QPushButton" name="pushButton_18">
125           <property name="text">
126            <string>uniqueKeys</string>
127           </property>
128          </widget>
129         </item>
130        </layout>
131       </item>
132       <item>
133        <layout class="QHBoxLayout" name="horizontalLayout_5">
134         <item>
135          <widget class="QPushButton" name="pushButton_8">
136           <property name="text">
137            <string>总元素size</string>
138           </property>
139          </widget>
140         </item>
141         <item>
142          <widget class="QPushButton" name="pushButton_7">
143           <property name="text">
144            <string>总元素count</string>
145           </property>
146          </widget>
147         </item>
148         <item>
149          <widget class="QPushButton" name="pushButton_19">
150           <property name="text">
151            <string>判空isEmpty</string>
152           </property>
153          </widget>
154         </item>
155        </layout>
156       </item>
157       <item>
158        <layout class="QHBoxLayout" name="horizontalLayout_6">
159         <item>
160          <widget class="QPushButton" name="pushButton_10">
161           <property name="text">
162            <string>Java风格迭代器</string>
163           </property>
164          </widget>
165         </item>
166         <item>
167          <widget class="QPushButton" name="pushButton_9">
168           <property name="text">
169            <string>STL风格迭代器</string>
170           </property>
171          </widget>
172         </item>
173         <item>
174          <widget class="QPushButton" name="pushButton_20">
175           <property name="text">
176            <string>foreach</string>
177           </property>
178          </widget>
179         </item>
180        </layout>
181       </item>
182       <item>
183        <layout class="QHBoxLayout" name="horizontalLayout_7">
184         <item>
185          <widget class="QPushButton" name="pushButton_12">
186           <property name="text">
187            <string>取走take</string>
188           </property>
189          </widget>
190         </item>
191         <item>
192          <widget class="QPushButton" name="pushButton_11">
193           <property name="text">
194            <string>移除remove</string>
195           </property>
196          </widget>
197         </item>
198         <item>
199          <widget class="QPushButton" name="pushButton_21">
200           <property name="text">
201            <string>清空clear</string>
202           </property>
203          </widget>
204         </item>
205        </layout>
206       </item>
207      </layout>
208     </item>
209    </layout>
210   </widget>
211  </widget>
212  <resources/>
213  <connections/>
214 </ui>
View Code

 

 

搜索

复制

<iframe></iframe>

标签:分析,clicked,Qt,void,MainWindow,ui,pHash,QHash,pushButton
来源: https://www.cnblogs.com/FKdelphi/p/16416115.html