[C++ GUI Programming with Qt4] chap4

Preview:

DESCRIPTION

 

Citation preview

Qt chap4

2011/02/23 - Browny

Spreadsheet

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

The central widget

The central widget• 標準 Qt 窗口 widget

o QTableWidget, QTextEdit o功能如文件載入和儲存,必須在其他地方實現

(Qmain Window 的 subclass)• 自定義窗口 widget• 帶布局管理器的普通 widget• 切分窗口 (Qsplitter)• 多文檔介面

main.cpp

mainwindow.cpp

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

QTableWidget, QTableWidgetItem

Item class : 純數據類,並非繼承自 QObject ,不能擁有 signal, slot ,也沒有使用 Q_OBJECT macro

const member variable(int)

• Spreadsheet.h

spreadsheet.cpp

允許矩行框選

QAbstractItemView

QTableView

setSelectionMode()

Spreadsheet::clear()• QTableWidget 由多個子窗口 widget 所組成QAbstractItemView

QAbstractScrollArea

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

QFile, QDataStream, (QTextStream)

各版本二進制表達方式不一定相同檔頭 ( 識別檔案 )

沙漏鼠標

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

Edit Menu::copy( )

::paste( )

把字串用 ‘ \n’ 分行

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

Other Menu• 4.5 一開頭講 spreadsheet 變更後的自動刷新,關於 setDirty() 機制可以留待

4.6 釐清

Spreadsheet::sort()

把選取的資料存到Qlist<QStringList> 當中

仿函數 functor

根據 compare 決定如何排序

SpreadsheetCompare• Override operator()• 可擁有屬性

::operator()

參考 圖 3.14 下方, keys 和 ascending 用來決定以哪一欄為主要 / 次要 排序依據。

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

setDirty() ?

Spreadsheet::text()

Where is Cell::text() ?• QTableWidgetItem provided

convenient function text() = data.toSrting()

Cell::value()Got It !

把表達式轉乘計算結果,與介面無關,跳過 XD

Conclusion• 複雜的繼承體系,也許先了解基礎抽象類比較有個底• 參考 Qt Class 大全

o http://trinity.pearsoncomputing.net/docs/qt4/classes.html

• 善用 IDE 的 Type Hierarchy 功能

Thank You Q&A