Download - [C++ GUI Programming with Qt4] chap4
![Page 1: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/1.jpg)
Qt chap4
2011/02/23 - Browny
![Page 2: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/2.jpg)
Spreadsheet
![Page 3: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/3.jpg)
Outline• 4.1 The Central Widget• 4.2 Subclassing QTableWidget• 4.3 Loading and Saving• 4.4 Implementing the Edit Menu• 4.5 Implementing the Other Menu• 4.6 Subclassing QTableWidgetItem
![Page 4: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/4.jpg)
The central widget
![Page 5: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/5.jpg)
The central widget• 標準 Qt 窗口 widget
o QTableWidget, QTextEdit o功能如文件載入和儲存,必須在其他地方實現
(Qmain Window 的 subclass)• 自定義窗口 widget• 帶布局管理器的普通 widget• 切分窗口 (Qsplitter)• 多文檔介面
![Page 6: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/6.jpg)
main.cpp
![Page 7: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/7.jpg)
mainwindow.cpp
![Page 8: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/8.jpg)
Outline• 4.1 The Central Widget• 4.2 Subclassing QTableWidget• 4.3 Loading and Saving• 4.4 Implementing the Edit Menu• 4.5 Implementing the Other Menu• 4.6 Subclassing QTableWidgetItem
![Page 9: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/9.jpg)
QTableWidget, QTableWidgetItem
Item class : 純數據類,並非繼承自 QObject ,不能擁有 signal, slot ,也沒有使用 Q_OBJECT macro
![Page 10: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/10.jpg)
const member variable(int)
• Spreadsheet.h
![Page 11: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/11.jpg)
spreadsheet.cpp
允許矩行框選
QAbstractItemView
QTableView
setSelectionMode()
![Page 12: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/12.jpg)
Spreadsheet::clear()• QTableWidget 由多個子窗口 widget 所組成QAbstractItemView
QAbstractScrollArea
![Page 13: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/13.jpg)
Outline• 4.1 The Central Widget• 4.2 Subclassing QTableWidget• 4.3 Loading and Saving• 4.4 Implementing the Edit Menu• 4.5 Implementing the Other Menu• 4.6 Subclassing QTableWidgetItem
![Page 14: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/14.jpg)
QFile, QDataStream, (QTextStream)
各版本二進制表達方式不一定相同檔頭 ( 識別檔案 )
沙漏鼠標
![Page 15: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/15.jpg)
Outline• 4.1 The Central Widget• 4.2 Subclassing QTableWidget• 4.3 Loading and Saving• 4.4 Implementing the Edit Menu• 4.5 Implementing the Other Menu• 4.6 Subclassing QTableWidgetItem
![Page 16: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/16.jpg)
Edit Menu::copy( )
::paste( )
把字串用 ‘ \n’ 分行
![Page 17: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/17.jpg)
Outline• 4.1 The Central Widget• 4.2 Subclassing QTableWidget• 4.3 Loading and Saving• 4.4 Implementing the Edit Menu• 4.5 Implementing the Other Menu• 4.6 Subclassing QTableWidgetItem
![Page 18: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/18.jpg)
Other Menu• 4.5 一開頭講 spreadsheet 變更後的自動刷新,關於 setDirty() 機制可以留待
4.6 釐清
![Page 19: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/19.jpg)
Spreadsheet::sort()
把選取的資料存到Qlist<QStringList> 當中
仿函數 functor
根據 compare 決定如何排序
![Page 20: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/20.jpg)
SpreadsheetCompare• Override operator()• 可擁有屬性
![Page 21: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/21.jpg)
::operator()
參考 圖 3.14 下方, keys 和 ascending 用來決定以哪一欄為主要 / 次要 排序依據。
![Page 22: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/22.jpg)
Outline• 4.1 The Central Widget• 4.2 Subclassing QTableWidget• 4.3 Loading and Saving• 4.4 Implementing the Edit Menu• 4.5 Implementing the Other Menu• 4.6 Subclassing QTableWidgetItem
![Page 23: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/23.jpg)
setDirty() ?
![Page 24: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/24.jpg)
Spreadsheet::text()
![Page 25: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/25.jpg)
Where is Cell::text() ?• QTableWidgetItem provided
convenient function text() = data.toSrting()
![Page 26: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/26.jpg)
Cell::value()Got It !
把表達式轉乘計算結果,與介面無關,跳過 XD
![Page 27: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/27.jpg)
Conclusion• 複雜的繼承體系,也許先了解基礎抽象類比較有個底• 參考 Qt Class 大全
o http://trinity.pearsoncomputing.net/docs/qt4/classes.html
• 善用 IDE 的 Type Hierarchy 功能
![Page 28: [C++ GUI Programming with Qt4] chap4](https://reader036.vdocuments.net/reader036/viewer/2022081412/540749fc8d7f72d8088b4a5e/html5/thumbnails/28.jpg)
Thank You Q&A