一、QMap深度解析
1、QMap是一个以升序键顺序存储键值对的数据结构
(1)QMap原型为class QMap<K, T>模板
(2)、QMap中的键值对根据key进行了排序
(3)、QMap中的key类型必须重载operator < (小于操作符)
2、QMap使用实例一
3、QMap使用实例二
4、QMap的注意事项
(1)、通过key获取Value时
A、当key存在,返回对应的Value
B、当key不存在,返回值类型所对应的“零”值
(2)、插入键值对时
A、当key存在,更新Value的值
B、当key不存在,插入新的键值对
1 #include <QtCore/QCoreApplication>
2 #include <QDebug>
3 #include <QMap>
4 #include<QMapIterator>//迭代器
5
6 int main(int argc, char *argv[])
7 {
8 QCoreApplication a(argc, argv);
9
10 QMap<QString, int> map;
11 map["key 2"] = 2;
12 map["key 0"] = 0;
13 map.insert("key 1", 1);
14
15 //map.insert("key 1", 4);//前面已有key 1,所有这里会更新key的键值为4
16
17 QList<QString> list = map.keys();
18
19 for(int i=0; i<list.count(); i++)
20 {
21 qDebug() << list[i];//打印排序好的key 0、key 1、key 2
22 }
23
24 QList<int> vlist = map.values();
25
26 for(int i=0; i<vlist.count(); i++)
27 {
28 qDebug() << vlist[i];//打印0、1、2
29 }
30
31
32
33 QMapIterator<QString,int> it(map);//it指向第一个元素之前的位置
34 while(it.hasNext())
35 {
36 it.next();
37 qDebug() << it.key() << ":" << it.value() ;
38 }
39
40
41 return a.exec();
42 }
二、QHash深度解析
1、QHash是Qt中的哈希数据结构
(1)、QHash原型为class QHash<K, T>模板
(2)、QHash中的键值对在内部无序排列
(3)、QHash中的Key类型必须重载operator ==
(4)、QHash中的Key对象必须重载全局哈希函数(qHash())
2、QHash使用示例
1 #include <QtCore/QCoreApplication>
2 #include <QDebug>
3 #include <QHash>
4
5 int main(int argc, char *argv[])
6 {
7 QCoreApplication a(argc, argv);
8
9 QHash<QString, int> map;
10 map["key 2"] = 2;
11 map["key 0"] = 0;
12 map.insert("key 1", 1);
13
14 //map.insert("key 1", 4);//前面已有key 1,所有这里会更新key的键值为4
15
16 QList<QString> list = map.keys();
17
18 for(int i=0; i<list.count(); i++)
19 {
20 qDebug() << list[i];//打印排序好的key 0、key 1、key 2
21 }
22
23 QList<int> vlist = map.values();
24
25 for(int i=0; i<vlist.count(); i++)
26 {
27 qDebug() << vlist[i];//打印0、1、2
28 }
29
30
31
32 QHash<QString,int>::const_iterator i;
33
34 for(i=map.constBegin(); i!=map.constEnd(); ++i)
35 {
36 qDebug() << i.key() << ":" << i.value() ;
37 }
38
39 return a.exec();
40 }
三、QMap和QHash的对比分析
1、QMap和QHash的接口相同,可直接替换使用
(1)、QHash的查找速度明显快于QMap
(2)、QHash占用的存储空间明显多于QMap
(3)、QHash以任意的方式存储元素
(4)、QMap以Key顺序存储元素
(5)、QHash的键类型必须提供operator == () 和 qHash(key)函数
(6)、QMap的键类型必须提供operator <
1 QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title)
2 {
3 QString ret = "";
4 QFileDialog fd;
5 QStringList filters;
6 QMap<QString, QString> map;
7
8 const char* fileArray[][2]=
9 {
10
11 {"Text(*.txt)", ".txt"},
12 {"All Files(*.*)", "*" },
13 {NULL, NULL}
14
15 };
16
17 for(int i=0; fileArray[i][0] != NULL; i++)
18 {
19 filters.append(fileArray[i][0]);
20 map.insert(fileArray[i][0], fileArray[i][1]);
21 }
22
23 fd.setWindowTitle(title);
24 fd.setAcceptMode(mode);
25
26
27 fd.setNameFilters(filters);
28
29 if(mode==QFileDialog::AcceptOpen)
30 {
31 fd.setFileMode(QFileDialog::ExistingFile);
32 }
33
34 if(fd.exec()==QFileDialog::Accepted)
35 {
36 ret = fd.selectedFiles()[0];
37
38 QString posix = map[fd.selectedNameFilter()];//把下拉中选中的后缀对应键值取出
39
40 if(posix != "*" && !ret.endsWith(posix))
41 {
42 ret += posix;
43 }
44 }
45
46 return ret;
47 }
48 #ifndef MAINWINDOW_H
49 #define MAINWINDOW_H
50 #include <QMenuBar>
51 #include <QMenu>
52 #include <QAction>
53 #include <QString>
54 #include <QtGui/QMainWindow>
55 #include <QToolBar>
56 #include <QIcon>
57 #include <QSize>
58 #include <QStatusBar>
59 #include <QLabel>
60 #include <QPlainTextEdit>
61 #include <QFileDialog>
62 class MainWindow : public QMainWindow
63 {
64 Q_OBJECT
65 private:
66 QPlainTextEdit mainEdit;
67 QLabel statusLabel;
68 QString m_filePath;//记得在构造函数里初始化
69 bool m_isTextChanged;//构造函数里初始化为false
70
71 MainWindow(QWidget *parent = 0);
72 MainWindow(const MainWindow& obj);
73 MainWindow* operator = (const MainWindow& obj);
74 bool construct();
75
76 bool initMenuBar();//菜单栏
77 bool initToolBar();//工具栏
78 bool initStatusBar();//状态栏
79 bool initinitMainEditor();//编辑窗口
80
81 bool initFileMenu(QMenuBar* mb);//文件菜单
82 bool initEditMenu(QMenuBar* mb);//编辑菜单
83 bool initFormatMenu(QMenuBar* mb);//格式菜单
84 bool initViewMenu(QMenuBar* mb);//视图菜单
85 bool initHelpMenu(QMenuBar* mb);//帮助菜单
86
87 bool initFileToolItem(QToolBar* tb);//工具选项
88 bool initEditToolItem(QToolBar* tb);
89 bool initFormatToolItem(QToolBar* tb);
90 bool initViewToolItem(QToolBar* tb);
91
92
93 bool makeAction(QAction*& action,QMenu* menu, QString text, int key);//菜单项
94 bool makeAction(QAction*& action,QToolBar* tb, QString tip, QString icon);
95
96 QString showFileDialog(QFileDialog::AcceptMode mode, QString title);//文件对话框
97 void showErrorMessage(QString message);//错误消息对话框
98 int showQuesstionMessage(QString message);//问题消息对话框
99 QString saveCurrentData(QString path = "");
100 void preEditorChanged();
101
102 private slots:
103 void onFileNew();
104 void onFileOpen();
105 void onFlieSave();
106 void onFileSaveAs();
107 void onTextChanged();
108 public:
109 static MainWindow* NewInstance();
110 ~MainWindow();
111 };
112
113 #endif // MAINWINDOW_H
114 #include "MainWindow.h"
115 #include <QDebug>
116
117 MainWindow::MainWindow(QWidget *parent)
118 : QMainWindow(parent), statusLabel(this)
119 {
120 m_filePath = "";
121 m_isTextChanged = false;
122 setWindowTitle("NotePad-[New]");
123 }
124
125 bool MainWindow::construct()
126 {
127 bool ret = true;
128 ret = ret && initMenuBar();
129 ret = ret && initToolBar();
130 ret = ret && initStatusBar();
131 ret = ret && initinitMainEditor();
132 return ret;
133 }
134 MainWindow* MainWindow::NewInstance()
135 {
136 MainWindow* ret = new MainWindow();
137
138 if((ret==NULL) || (!ret->construct()))
139 {
140 delete ret;
141 ret = NULL;
142 }
143
144 return ret;
145 }
146 bool MainWindow::initMenuBar()//菜单栏
147 {
148 bool ret = true;
149
150 QMenuBar* mb = menuBar();//一定要注意是menuBar(),这是普通成员函数,不是构造函数
151
152 ret = ret && initFileMenu(mb);//传一个参数是为了在initFileMenu()函数将menu加入菜单栏
153 ret = ret && initEditMenu(mb);
154 ret = ret && initFormatMenu(mb);
155 ret = ret && initViewMenu(mb);
156 ret = ret && initHelpMenu(mb);
157
158 return ret;
159
160 }
161
162 bool MainWindow::initToolBar()//工具栏
163 {
164 bool ret = true;
165
166 QToolBar* tb = addToolBar("Tool Bar");
167 //tb->setMovable(false);
168 //tb->setFloatable(false);
169 tb->setIconSize(QSize(16,16));
170
171 ret = ret && initFileToolItem(tb);
172 tb->addSeparator();
173 ret = ret && initEditToolItem(tb);
174 tb->addSeparator();
175 ret = ret && initFormatToolItem(tb);
176 tb->addSeparator();
177 ret = ret && initViewToolItem(tb);
178
179 return ret;
180 }
181
182 bool MainWindow::initStatusBar()//状态栏
183 {
184 bool ret = true;
185
186 QStatusBar* sb = statusBar();
187
188 QLabel* label = new QLabel("Made By LGC");
189
190 if(label != NULL)
191 {
192 statusLabel.setMinimumWidth(200);
193 statusLabel.setAlignment(Qt::AlignHCenter);
194 statusLabel.setText("Ln:1 Col:1");
195
196
197 label->setMinimumWidth(200);
198 label->setAlignment(Qt::AlignHCenter);
199
200 sb->addPermanentWidget(new QLabel());//单纯加入分隔符
201 sb->addPermanentWidget(&statusLabel);
202 sb->addPermanentWidget(label);
203 }
204 else
205 {
206 ret = false;
207 }
208 return ret;
209 }
210 bool MainWindow::initinitMainEditor()//编辑窗口
211 {
212 bool ret = true;
213
214 mainEdit.setParent(this);
215 setCentralWidget(&mainEdit);
216
217 connect(&mainEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
218
219 return ret;
220 }
221
222 /************************************************文件菜单********************************************************/
223 bool MainWindow::initFileMenu(QMenuBar* mb)
224 {
225 bool ret = true;
226
227 QMenu* menu = new QMenu("File(&F)");//创建文件菜单,(&F)是为了可以Alt+F打开
228 ret = (menu != NULL);
229 if(ret)
230 {
231 QAction* action = NULL;
232
233 //New
234 ret = ret && makeAction(action, menu, "New(&N)",Qt::CTRL + Qt::Key_N);
235 if(ret)
236 {
237 connect(action, SIGNAL(triggered()), this, SLOT(onFileNew()));
238 menu->addAction(action);
239 }
240
241 menu->addSeparator();
242
243 //Open
244 ret = ret && makeAction(action, menu,"Open(&O)...",Qt::CTRL + Qt::Key_O);
245 if(ret)
246 {
247 connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen()));
248 menu->addAction(action);
249 }
250
251 menu->addSeparator();
252
253 //Save
254 ret = ret && makeAction(action, menu,"Save(&S)",Qt::CTRL + Qt::Key_S);
255 if(ret)
256 {
257 connect(action, SIGNAL(triggered()), this ,SLOT(onFlieSave()));
258 menu->addAction(action);
259 }
260
261 menu->addSeparator();
262
263 //Save As
264 ret = ret && makeAction(action, menu, "Save As(&A)...",0);
265 if(ret)
266 {
267 connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs()));
268 menu->addAction(action);
269
270 }
271
272 menu->addSeparator();
273
274 //print
275 ret = ret && makeAction(action, menu, "Print(&P)...",Qt::CTRL + Qt::Key_P);
276 if(ret)
277 {
278 menu->addAction(action);
279 }
280
281 menu->addSeparator();
282
283 //Exit
284 ret = ret && makeAction(action, menu,"Exit(&X)",0);
285 if(ret)
286 {
287 menu->addAction(action);//将菜单项加入到菜单
288 }
289
290 }
291 if(ret)
292 {
293 mb->addMenu(menu);//将菜单加入到菜单栏
294 }
295 else
296 {
297 delete mb;
298 }
299 return ret;
300 }
301
302 /************************************************编辑菜单********************************************************/
303 bool MainWindow::initEditMenu(QMenuBar* mb)
304 {
305 bool ret = true;
306
307 QMenu* menu = new QMenu("Edit(&E)");
308 ret = (menu != NULL);
309 if(ret)
310 {
311 QAction* action = NULL;
312
313 //Undo
314 ret = ret && makeAction(action, menu,"Undo(&U)",Qt::CTRL + Qt::Key_Z);
315 if(ret)
316 {
317 menu->addAction(action);
318 }
319
320 menu->addSeparator();
321
322 //Redo
323 ret = ret && makeAction(action, menu,"Redo(&R)...",Qt::CTRL + Qt::Key_Y);
324 if(ret)
325 {
326 menu->addAction(action);
327 }
328
329 menu->addSeparator();
330
331 //Cut
332 ret = ret && makeAction(action, menu,"Cut(&T)",Qt::CTRL + Qt::Key_X);
333 if(ret)
334 {
335 menu->addAction(action);
336 }
337
338 menu->addSeparator();
339
340 //Copy
341 ret = ret && makeAction(action, menu,"Copy(&C)...",Qt::CTRL + Qt::Key_C);
342 if(ret)
343 {
344 menu->addAction(action);
345 }
346
347 menu->addSeparator();
348
349 //Pase
350 ret = ret && makeAction(action, menu,"Pase(&P)...",Qt::CTRL + Qt::Key_V);
351 if(ret)
352 {
353 menu->addAction(action);
354 }
355
356 menu->addSeparator();
357
358 //Delete
359 ret = ret && makeAction(action, menu, "Delete(&L)",Qt::Key_Delete);
360 if(ret)
361 {
362 menu->addAction(action);
363 }
364
365 menu->addSeparator();
366
367 //Find
368 ret = ret && makeAction(action, menu,"Find(&F)...",Qt::CTRL + Qt::Key_F);
369 if(ret)
370 {
371 menu->addAction(action);
372 }
373
374 menu->addSeparator();
375
376 //Replace
377 ret = ret && makeAction(action, menu,"Replace(&R)...",Qt::CTRL + Qt::Key_H);
378 if(ret)
379 {
380 menu->addAction(action);
381 }
382
383 menu->addSeparator();
384
385 //Goto
386 ret = ret && makeAction(action, menu,"Goto(&G)",Qt::CTRL + Qt::Key_G);
387 if(ret)
388 {
389 menu->addAction(action);
390 }
391
392 menu->addSeparator();
393
394 //Select All
395 ret = ret && makeAction(action, menu, "Select All(&A)",Qt::CTRL + Qt::Key_A);
396 if(ret)
397 {
398 menu->addAction(action);
399 }
400
401 }
402 if(ret)
403 {
404 mb->addMenu(menu);
405 }
406 else
407 {
408 delete mb;
409 }
410 return ret;
411 }
412
413 /************************************************格式菜单********************************************************/
414 bool MainWindow::initFormatMenu(QMenuBar* mb)
415 {
416 bool ret = true;
417
418 QMenu* menu = new QMenu("Format(&O)");
419 ret = (menu != NULL);
420 if(ret)
421 {
422 QAction* action = NULL;
423
424 //Auto Wrap
425 ret = ret && makeAction(action, menu,"Auto Wrap(&W)",0);
426 if(ret)
427 {
428 menu->addAction(action);
429 }
430
431 menu->addSeparator();
432
433 //Font
434 ret = ret && makeAction(action, menu,"Font(&F)...",0);
435 if(ret)
436 {
437 menu->addAction(action);
438 }
439
440 }
441 if(ret)
442 {
443 mb->addMenu(menu);
444 }
445 else
446 {
447 delete mb;
448 }
449 return ret;
450 }
451
452 /************************************************视图菜单********************************************************/
453 bool MainWindow::initViewMenu(QMenuBar* mb)
454 {
455 bool ret = true;
456
457 QMenu* menu = new QMenu("View(&V)");
458 ret = (menu != NULL);
459 if(ret)
460 {
461 QAction* action = NULL;
462
463 //Tool Bar
464 ret = ret && makeAction(action, menu,"Tool Bar(&T)",0);
465 if(ret)
466 {
467 menu->addAction(action);
468 }
469
470 menu->addSeparator();
471
472 //Status Bar
473 ret = ret && makeAction(action, menu,"Status Bar(&S)",0);
474 if(ret)
475 {
476 menu->addAction(action);
477 }
478
479 }
480 if(ret)
481 {
482 mb->addMenu(menu);
483 }
484 else
485 {
486 delete mb;
487 }
488 return ret;
489 }
490
491 /************************************************帮助菜单********************************************************/
492 bool MainWindow::initHelpMenu(QMenuBar* mb)
493 {
494 bool ret = true;
495
496 QMenu* menu = new QMenu("Help(&H)");
497 ret = (menu != NULL);
498 if(ret)
499 {
500 QAction* action = NULL;
501
502 //User Manual
503 ret = ret && makeAction(action, menu,"User Manual",0);
504 if(ret)
505 {
506 menu->addAction(action);
507 }
508
509 menu->addSeparator();
510
511 //About NotePad
512 ret = ret && makeAction(action, menu,"About NotePad...",0);
513 if(ret)
514 {
515 menu->addAction(action);
516 }
517
518 }
519 if(ret)
520 {
521 mb->addMenu(menu);
522 }
523 else
524 {
525 delete mb;
526 }
527 return ret;
528 }
529 /*****************************************工具************************************************************/
530 bool MainWindow::initFileToolItem(QToolBar* tb)
531 {
532 bool ret = true;
533 QAction* action = NULL;
534
535 ret = ret && makeAction(action, tb, "New", ":/Res/pic/new.png");
536 if(ret)
537 {
538 connect(action, SIGNAL(triggered()), this, SLOT(onFileNew()));
539 tb->addAction(action);
540
541 }
542
543 ret = ret && makeAction(action, tb,"Open", ":/Res/pic/open.png");
544 if(ret)
545 {
546 connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen()));
547 tb->addAction(action);
548 }
549
550 ret = ret && makeAction(action, tb,"Save", ":/Res/pic/save.png");
551 if(ret)
552 {
553 connect(action, SIGNAL(triggered()), this ,SLOT(onFlieSave()));
554 tb->addAction(action);
555 }
556
557 ret = ret && makeAction(action, tb,"Save As", ":/Res/pic/saveas.png");
558 if(ret)
559 {
560 connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs()));
561 tb->addAction(action);
562 }
563 ret = ret && makeAction(action, tb,"Print", ":/Res/pic/print.png");
564 if(ret)
565 {
566 tb->addAction(action);
567 }
568 return ret;
569
570 }
571 bool MainWindow::initEditToolItem(QToolBar* tb)
572 {
573 bool ret = true;
574 QAction* action = NULL;
575
576 ret = ret && makeAction(action, tb,"Undo", ":/Res/pic/undo.png");
577 if(ret)
578 {
579 tb->addAction(action);
580 }
581 ret = ret && makeAction(action, tb,"Redo", ":/Res/pic/redo.png");
582 if(ret)
583 {
584 tb->addAction(action);
585 }
586
587 ret = ret && makeAction(action, tb, "Cut", ":/Res/pic/cut.png");
588 if(ret)
589 {
590 tb->addAction(action);
591 }
592
593 ret = ret && makeAction(action, tb,"Copy", ":/Res/pic/copy.png");
594 if(ret)
595 {
596 tb->addAction(action);
597 }
598
599 ret = ret && makeAction(action, tb,"Paste", ":/Res/pic/paste.png");
600 if(ret)
601 {
602 tb->addAction(action);
603 }
604
605 ret = ret && makeAction(action, tb,"Find", ":/Res/pic/find.png");
606 if(ret)
607 {
608 tb->addAction(action);
609 }
610 ret = ret && makeAction(action, tb,"Replace", ":/Res/pic/replace.png");
611 if(ret)
612 {
613 tb->addAction(action);
614 }
615 ret = ret && makeAction(action, tb,"Goto", ":/Res/pic/goto.png");
616 if(ret)
617 {
618 tb->addAction(action);
619 }
620
621 return ret;
622 }
623 bool MainWindow::initFormatToolItem(QToolBar* tb)
624 {
625 bool ret = true;
626 QAction* action = NULL;
627
628 ret = ret && makeAction(action, tb, "Auto Wrap", ":/Res/pic/wrap.png");
629 if(ret)
630 {
631 tb->addAction(action);
632 }
633 ret = ret && makeAction(action, tb,"Font", ":/Res/pic/font.png");
634 if(ret)
635 {
636 tb->addAction(action);
637 }
638
639 return ret;
640 }
641 bool MainWindow::initViewToolItem(QToolBar* tb)
642 {
643 bool ret = true;
644 QAction* action = NULL;
645
646 ret = ret && makeAction(action, tb,"Tool Bar", ":/Res/pic/tool.png");
647 if(ret)
648 {
649 tb->addAction(action);
650 }
651 ret = ret && makeAction(action, tb,"Status Bar", ":/Res/pic/status.png");
652 if(ret)
653 {
654 tb->addAction(action);
655 }
656
657 return ret;
658 }
659
660
661 bool MainWindow::makeAction(QAction*& action,QMenu* menu, QString text, int key)//菜单项
662 {
663 bool ret = true;
664 action = new QAction(text, menu);
665 if(action != NULL)
666 {
667 action->setShortcut(QKeySequence(key));//创建快捷键
668 }
669 else
670 {
671 ret = false;
672 }
673
674 return ret;
675 }
676 bool MainWindow::makeAction(QAction*& action,QToolBar* tb, QString tip, QString icon)
677 {
678 bool ret = true;
679 action = new QAction("", tb);
680 if(action != NULL)
681 {
682 action->setToolTip(tip);
683 action->setIcon(QIcon(icon));
684 }
685 else
686 {
687 ret = false;
688 }
689 return ret;
690 }
691 MainWindow::~MainWindow()
692 {
693
694 }
695 #include <QFileDialog>
696 #include <QStringList>
697 #include <QFile>
698 #include <QDebug>
699 #include <QMessageBox>
700 #include "MainWindow.h"
701 #include <QMap>
702
703 QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title)
704 {
705 QString ret = "";
706 QFileDialog fd;
707 QStringList filters;
708 QMap<QString, QString> map;
709
710 const char* fileArray[][2]=
711 {
712
713 {"Text(*.txt)", ".txt"},
714 {"All Files(*.*)", "*" },
715 {NULL, NULL}
716
717 };
718
719 for(int i=0; fileArray[i][0] != NULL; i++)
720 {
721 filters.append(fileArray[i][0]);
722 map.insert(fileArray[i][0], fileArray[i][1]);
723 }
724
725 fd.setWindowTitle(title);
726 fd.setAcceptMode(mode);
727
728
729 fd.setNameFilters(filters);
730
731 if(mode==QFileDialog::AcceptOpen)
732 {
733 fd.setFileMode(QFileDialog::ExistingFile);
734 }
735
736 if(fd.exec()==QFileDialog::Accepted)
737 {
738 ret = fd.selectedFiles()[0];
739
740 QString posix = map[fd.selectedNameFilter()];//把下拉中选中的后缀对应键值取出
741
742 if(posix != "*" && !ret.endsWith(posix))
743 {
744 ret += posix;
745 }
746 }
747
748 return ret;
749 }
750
751 void MainWindow::showErrorMessage(QString message)
752 {
753 QMessageBox mb(this);
754
755 mb.setWindowTitle("Quession");
756 mb.setText(message);
757 mb.setIcon(QMessageBox::Critical);
758 mb.setStandardButtons(QMessageBox::Ok);
759
760 mb.exec();
761 }
762
763 int MainWindow::showQuesstionMessage(QString message)
764 {
765 QMessageBox mb(this);
766
767 mb.setWindowTitle("Error");
768 mb.setText(message);
769 mb.setIcon(QMessageBox::Question);
770 mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
771
772 return mb.exec();
773 }
774
775 QString MainWindow::saveCurrentData(QString path)
776 {
777 QString ret = path;
778 if(ret == "")
779 {
780 ret = showFileDialog(QFileDialog::AcceptSave, "Save");
781
782 }
783
784 if(ret != "")
785 {
786 QFile file(ret);
787 if(file.open(QIODevice::WriteOnly | QIODevice::Text))
788 {
789 QTextStream out(&file);
790
791 out << QString(mainEdit.toPlainText());
792
793 file.close();
794
795 setWindowTitle("NotePad - [" + ret + "]");
796
797 m_isTextChanged = false;//保存后修改状态值
798 }
799 else
800 {
801 showErrorMessage(QString("Open file Error!\n\n") + "\"" + m_filePath + "\"");
802 ret = "";
803 }
804 }
805
806 return ret;
807
808 }
809 void MainWindow::preEditorChanged()
810 {
811 if(m_isTextChanged)
812 {
813 int r = showQuesstionMessage("Do you want to Save?");
814 switch (r)
815 {
816 case QMessageBox::Yes:
817 saveCurrentData(m_filePath);
818 break;
819 case QMessageBox::No:
820 m_isTextChanged = false;
821 break;
822 case QMessageBox::Cancel:
823 break;
824
825 }
826
827 }
828 }
829 void MainWindow::onFileNew()
830 {
831 preEditorChanged();
832 if(!m_isTextChanged)
833 {
834 mainEdit.clear();
835
836 m_filePath = "";
837
838 m_isTextChanged = false;
839
840 setWindowTitle("NotePad-[New]");
841 }
842 }
843 void MainWindow::onFileOpen()
844 {
845
846 preEditorChanged();
847
848 if(!m_isTextChanged)
849 {
850 QString path = showFileDialog(QFileDialog::AcceptOpen, "open");
851
852 if(path != "")
853 {
854 QFile file(path);
855 if(file.open(QIODevice::ReadOnly | QIODevice::Text))
856 {
857 mainEdit.setPlainText(QString(file.readAll()));
858
859 file.close();
860
861 m_filePath = path;//报存当前文件路径
862
863 setWindowTitle("NotePad - [" + m_filePath + "]");
864 }
865 else
866 {
867 showErrorMessage(QString("Open file Error!\n\n") + "\"" + m_filePath + "\"");
868 }
869 }
870 }
871
872 }
873
874 void MainWindow::onFlieSave()
875 {
876 QString path = saveCurrentData(m_filePath);
877
878 if(path != "")
879 {
880 m_filePath = path;
881 }
882 }
883
884 void MainWindow::onFileSaveAs()
885 {
886 QString path = saveCurrentData();//使用默认参数
887
888 if(path != "")
889 {
890 m_filePath = path;
891 }
892
893 }
894 void MainWindow::onTextChanged()
895 {
896 if(!m_isTextChanged)
897 {
898 setWindowTitle("*" + windowTitle());
899 }
900 m_isTextChanged = true;
901
902 }
903 #include <QtGui/QApplication>
904 #include "MainWindow.h"
905 #include <QTextCodec>
906
907
908 int main(int argc, char *argv[])
909 {
910 QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK")); //路径名支持中文
911 QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK")); //QString支持中文
912 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK")); //string支持中文
913 QApplication a(argc, argv);
914 MainWindow* w = MainWindow::NewInstance();
915 int ret = -1;
916 if(w != NULL)
917 {
918 w->show();
919 ret = a.exec();
920 }
921
922 delete w;
923 return ret;
924 }
四、小结
(1)、Qt中提供了用于存储键值的类模板
(2)、QHash与QMap遵循了相同的使用接口
(3)、QHash的查找速度快于QMap
(4)、QMap需要的内存少于QHash
(5)、QHash对于Key类型的要求高于QMap