Qt模型PPT課件.pptx_第1頁(yè)
Qt模型PPT課件.pptx_第2頁(yè)
Qt模型PPT課件.pptx_第3頁(yè)
Qt模型PPT課件.pptx_第4頁(yè)
Qt模型PPT課件.pptx_第5頁(yè)
已閱讀5頁(yè),還剩43頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第8章Qt5模型 視圖結(jié)構(gòu) 8 1概述 8 2模型 Model 8 3視圖 View 8 4代理 Delegate 2020 2 7 1 第8章Qt5模型 視圖結(jié)構(gòu) Qt的模型 視圖結(jié)構(gòu)分為三部分 模型 Model 視圖 View 和代理 Delegate 其中 模型與數(shù)據(jù)源通信 并為其他部件提供接口 而視圖從模型中獲得用來(lái)引用數(shù)據(jù)條目的模型索引 ModelIndex 在視圖中 代理負(fù)責(zé)繪制數(shù)據(jù)條目 當(dāng)編輯條目時(shí) 代理和模型直接進(jìn)行通信 模型 視圖 代理之間通過(guò)信號(hào)和槽進(jìn)行通信 如圖8 1所示 2020 2 7 2 8 1概述 8 1 1基本概念1 模型 Model InterView框架中的所有模型都基于抽象基類(lèi)QAbstractItemModel類(lèi) 此類(lèi)由QProxyModel QAbstractListModel QAbstractTableModel QAbstractProxyModel QDirModel QFileSystemModel QHelpContentModel和QStandardItemModel類(lèi)繼承 其中 QAbstractListModel類(lèi)和QAbstractTableModel類(lèi)是列表和表格模型的抽象基類(lèi) 如果需要實(shí)現(xiàn)列表或表格模型 則應(yīng)從這兩個(gè)類(lèi)繼承 完成QStringList存儲(chǔ)的QStringListModel繼承自QAbstractListModel類(lèi) 而與數(shù)據(jù)庫(kù)有關(guān)的QSqlQueryModel類(lèi)繼承自QAbstractTableModel類(lèi) QAbstractProxyModel類(lèi)是代理模型的抽象類(lèi) QDirModel類(lèi)是文件和目錄的存儲(chǔ)模型 2020 2 7 3 8 1 1基本概念 2 視圖 View InterView框架中的所有視圖都基于抽象基類(lèi)QAbstractItemView類(lèi) 此類(lèi)由QColumnView QHeaderView QListView QTableView和QTreeView類(lèi)繼承 其中 QListView類(lèi)由QUndoView類(lèi)和QListWidget類(lèi)繼承 QTableView類(lèi)由QTableWidget類(lèi)繼承 QTreeView類(lèi)由QTreeWidget類(lèi)繼承 而QListWidget類(lèi) QTableWidget類(lèi)和QTreeWidget類(lèi)實(shí)際上已經(jīng)包含了數(shù)據(jù) 是模型 視圖集成在一起的類(lèi) 3 代理 Delegate InterView框架中的所有代理都基于抽象基類(lèi)QAbstractItemDelegate類(lèi) 此類(lèi)由QItemDelegate和QStyledItemDelegate類(lèi)繼承 其中 QItemDelegate類(lèi)由表示數(shù)據(jù)庫(kù)中關(guān)系代理的QSqlRelationalDelegate類(lèi)繼承 2020 2 7 4 8 1 2 實(shí)例 模型 視圖類(lèi)使用 例 簡(jiǎn)單 CH801 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文件目錄瀏覽器 完成效果如圖8 2所示 2020 2 7 5 8 1 2 實(shí)例 模型 視圖類(lèi)使用 創(chuàng)建工程 DirModeEx pro 其源文件 main cpp 中的具體代碼 其中 a QDirModelmodel 新建一個(gè)QDirModel對(duì)象 為數(shù)據(jù)訪問(wèn)做準(zhǔn)備 QDirModel的創(chuàng)建還可以設(shè)置過(guò)濾器 即只有符合條件的文件或目錄才可被訪問(wèn) b tree setModel 為了實(shí)現(xiàn)雙擊QTreeView對(duì)象中的某個(gè)目錄時(shí) QListView對(duì)象和QTableView對(duì)象中顯示此選定目錄下的所有文件和目錄 需要連接QTreeView對(duì)象的doubleClicked 信號(hào)與QListView對(duì)象和QTableView對(duì)象的setRootIndex 槽函數(shù) 最后運(yùn)行結(jié)果如圖8 2所示 2020 2 7 6 8 2模型 Model 例 難度一般 CH802 通過(guò)實(shí)現(xiàn)將數(shù)值代碼轉(zhuǎn)換為文字的模型來(lái)介紹如何使用自定義模型 此模型中保存了不同軍種的各種武器 實(shí)現(xiàn)效果如圖8 3所示 2020 2 7 7 8 2模型 Model 1 ModelEx類(lèi)繼承自QAbstractTableModel類(lèi) 頭文件 modelex h 中的具體代碼如下 include include include includeclassModelEx publicQAbstractTableModel public explicitModelEx QObject parent 0 虛函數(shù)聲明 a virtualintrowCount constQModelIndex 2020 2 7 8 8 2模型 Model 2 源文件 modelex cpp 中的具體代碼如下 include modelex h ModelEx ModelEx QObject parent QAbstractTableModel parent armyMap 1 tr 空軍 armyMap 2 tr 海軍 armyMap 3 tr 陸軍 armyMap 4 tr 海軍陸戰(zhàn)隊(duì) weaponTypeMap 1 tr 轟炸機(jī) weaponTypeMap 2 tr 戰(zhàn)斗機(jī) weaponTypeMap 3 tr 航空母艦 weaponTypeMap 4 tr 驅(qū)逐艦 weaponTypeMap 5 tr 直升機(jī) weaponTypeMap 6 tr 坦克 weaponTypeMap 7 tr 兩棲攻擊艦 weaponTypeMap 8 tr 兩棲戰(zhàn)車(chē) populateModel 2020 2 7 9 8 2模型 Model populateModel 函數(shù)的具體實(shí)現(xiàn)代碼如下 voidModelEx populateModel header tr 軍種 tr 種類(lèi) tr 武器 army 1 2 3 4 2 4 3 1 weaponType 1 3 5 7 4 8 6 2 weapon tr B 2 tr 尼米茲級(jí) tr 阿帕奇 tr 黃蜂級(jí) tr 阿利伯克級(jí) tr AAAV tr M1A1 tr F 22 columnCount 函數(shù)中 模型的列固定為 3 所以直接返回 3 intModelEx columnCount constQModelIndex 2020 2 7 10 8 2模型 Model data 函數(shù)返回指定索引的數(shù)據(jù) 即將數(shù)值映射為文字 QVariantModelEx data constQModelIndex 2020 2 7 11 8 2模型 Model 表8 1列出了Item主要的角色及其描述 2020 2 7 12 8 2模型 Model headerData 函數(shù)返回固定的表頭數(shù)據(jù) 設(shè)置水平表頭的標(biāo)題 具體代碼如下 QVariantModelEx headerData intsection Qt Orientationorientation introle const if role Qt DisplayRole 2020 2 7 13 8 2模型 Model 3 在源文件 main cpp 中 將模型和視圖關(guān)聯(lián) 具體代碼如下 include include modelex h includeintmain intargc char argv QApplicationa argc argv ModelExmodelEx QTableViewview view setModel 4 運(yùn)行結(jié)果如圖8 3所示 2020 2 7 14 8 3視圖 View 例 難度中等 CH803 通過(guò)利用自定義的View 實(shí)現(xiàn)一個(gè)對(duì)TableModel的表格數(shù)據(jù)進(jìn)行顯示的柱狀統(tǒng)計(jì)圖例子 以此介紹如何應(yīng)用自定義的View 實(shí)現(xiàn)效果如圖8 4所示 2020 2 7 15 8 3視圖 View 1 完成主窗體 以便顯示View的內(nèi)容 MainWindow類(lèi)繼承自QMainWindow類(lèi) 作為主窗體 以下是頭文件 mainwindow h 的具體代碼 2 下面是源文件 mainwindow cpp 中的具體代碼 setupModel 函數(shù)新建一個(gè)Model 并設(shè)置表頭數(shù)據(jù) 其具體實(shí)現(xiàn)代碼如下 voidMainWindow setupModel model newQStandardItemModel 4 4 this model setHeaderData 0 Qt Horizontal tr 部門(mén) model setHeaderData 1 Qt Horizontal tr 男 model setHeaderData 2 Qt Horizontal tr 女 model setHeaderData 3 Qt Horizontal tr 退休 2020 2 7 16 8 3視圖 View setupView 函數(shù)的具體實(shí)現(xiàn)代碼如下 voidMainWindow setupView table newQTableView 新建一個(gè)QTableView對(duì)象table setModel model 為QTableView對(duì)象設(shè)置相同的ModelQItemSelectionModel selectionModel newQItemSelectionModel model a table setSelectionModel selectionModel connect selectionModel SIGNAL selectionChanged QItemSelection ItemSelection table SLOT selectionChanged QItemSelection QItemSelection b splitter newQSplitter splitter setOrientation Qt Vertical splitter addWidget table setCentralWidget splitter 2020 2 7 17 8 3視圖 View 3 此時(shí) 運(yùn)行效果如圖8 5所示 2020 2 7 18 8 3視圖 View 以上只是實(shí)現(xiàn)了簡(jiǎn)單的主窗體框架顯示 還沒(méi)有完成事件 具體實(shí)現(xiàn)步驟如下 1 在頭文件 mainwindow h 中添加代碼如下 public voidopenFile QString publicslots voidslotOpen 2 在源文件mainwindow cpp中添加代碼如下 include include include include其中 在createAction 函數(shù)中添加代碼如下 connect openAct SIGNAL triggered this SLOT slotOpen 2020 2 7 19 8 3視圖 View 槽函數(shù)slotOpen 完成打開(kāi)標(biāo)準(zhǔn)文件對(duì)話框 具體代碼如下 voidMainWindow slotOpen QStringname name QFileDialog getOpenFileName this 打開(kāi) histogramfiles txt if name isEmpty openFile name openFile 函數(shù)完成打開(kāi)所選的文件內(nèi)容 其具體實(shí)現(xiàn)代碼 2020 2 7 20 8 3視圖 View 新建一個(gè)文本文件 命名為 histogram txt 保存在項(xiàng)目D Qt CH8 CH803 build ViewEx Desktop Qt 5 8 0 MinGW 32bit Debug目錄下 文件內(nèi)容及打開(kāi)后的效果如圖8 6所示 2020 2 7 21 8 3視圖 View 以上完成了表格數(shù)據(jù)的加載 下面介紹柱狀統(tǒng)計(jì)圖的繪制 具體實(shí)現(xiàn)步驟如下 1 自定義HistogramView類(lèi)繼承自QAbstractItemView類(lèi) 用于對(duì)表格數(shù)據(jù)進(jìn)行柱狀圖顯示 下面是頭文件 histogramview h 的具體代碼 其中 a visualRect scrollTo indexAt moveCursor horizontalOffset verticalOffset isIndexHidden setSelection 和visualRegionForSelection QAbstractItemView類(lèi)中的純虛函數(shù) b QModelIndexindexAt constQPoint point const 當(dāng)鼠標(biāo)在視圖中單擊或位置發(fā)生改變時(shí)被觸發(fā) 它返回鼠標(biāo)所在點(diǎn)的QModelIndex值 c voidmousePressEvent QMouseEvent event 柱狀統(tǒng)計(jì)圖可以被鼠標(biāo)單擊選擇 選中后以不同的方式顯示 d voidselectionChanged constQItemSelection selected constQItemSelection deselected 當(dāng)數(shù)據(jù)項(xiàng)選擇發(fā)生變化時(shí) 此槽函數(shù)將響應(yīng) e voiddataChanged constQModelIndex topLeft constQModelIndex bottomRight 當(dāng)模型中的數(shù)據(jù)發(fā)生變更時(shí) 此槽函數(shù)將響應(yīng) f voidsetSelection constQRect rect QItemSelectionModel SelectionFlagsflags 將位于QRect內(nèi)的數(shù)據(jù)項(xiàng)按照SelectionFlags 描述被選擇的數(shù)據(jù)項(xiàng)以何種方式進(jìn)行更新 指定的方式進(jìn)行更新 g QItemSelectionModel selections 用于保存與視圖選擇項(xiàng)相關(guān)的內(nèi)容 h QListMRegionList 用于保存其中某一類(lèi)型柱狀圖的區(qū)域范圍 而每個(gè)區(qū)域是QList中的一個(gè)值 2020 2 7 22 8 3視圖 View 2 源文件 histogramview cpp 的具體代碼 其中 a QPainterpainter viewport 以viewport 作為繪圖設(shè)備新建一個(gè)QPainter對(duì)象 b if selections isSelected index else 使用不同畫(huà)刷顏色區(qū)別選中與未被選中的數(shù)據(jù)項(xiàng) c painter drawRect QRect posM y0 male 10 width male 10 根據(jù)當(dāng)前數(shù)據(jù)項(xiàng)的值按比例繪制一個(gè)方形表示此數(shù)據(jù)項(xiàng) d MRegionList insert row regionM 將此數(shù)據(jù)所占據(jù)的區(qū)域保存到MRegionList列表中 為后面的數(shù)據(jù)項(xiàng)選擇做準(zhǔn)備 e 從intposF x0 30語(yǔ)句到posF 50語(yǔ)句之間的代碼段 完成了表格第2列數(shù)據(jù)的柱狀統(tǒng)計(jì)圖的繪制 f 從intposS x0 40語(yǔ)句到posS 50語(yǔ)句之間的代碼段 完成了表格第3列數(shù)據(jù)的柱狀統(tǒng)計(jì)圖的繪制 2020 2 7 23 8 3視圖 View dataChanged 函數(shù)實(shí)現(xiàn)當(dāng)Model中的數(shù)據(jù)更改時(shí) 調(diào)用繪圖設(shè)備的update 函數(shù)進(jìn)行更新 反映數(shù)據(jù)的變化 具體實(shí)現(xiàn)代碼如下 voidHistogramView dataChanged constQModelIndex 2020 2 7 24 2020 2 7 25 8 3視圖 View 3 下面的工作就是完成對(duì)選擇項(xiàng)的更新 selectionChanged 函數(shù)中完成當(dāng)數(shù)據(jù)項(xiàng)發(fā)生變化時(shí)調(diào)用update 函數(shù) 重繪繪圖設(shè)備即可工作 此函數(shù)是將其他View中的操作引起的數(shù)據(jù)項(xiàng)選擇變化反映到自身View的顯示中 具體代碼如下 voidHistogramView selectionChanged constQItemSelection 2020 2 7 26 8 3視圖 View setSelection 函數(shù)的具體代碼如下 voidHistogramView setSelection constQRect 2020 2 7 27 8 3視圖 View indexAt 函數(shù)的具體內(nèi)容 由于本例未用到以下函數(shù)的功能 所以沒(méi)有實(shí)現(xiàn)具體內(nèi)容 但仍然要寫(xiě)出函數(shù)體的框架 代碼如下 QRectHistogramView visualRect constQModelIndex index const voidHistogramView scrollTo constQModelIndex index ScrollHint QModelIndexHistogramView moveCursor QAbstractItemView CursorActioncursorAction Qt KeyboardModifiersmodifiers intHistogramView horizontalOffset const intHistogramView verticalOffset const boolHistogramView isIndexHidden constQModelIndex index const QRegionHistogramView visualRegionForSelection constQItemSelection selection const 2020 2 7 28 8 3視圖 View itemRegion 函數(shù)的具體代碼如下 QRegionHistogramView itemRegion QModelIndexindex QRegionregion if index column 1 男region MRegionList index row if index column 2 女region FRegionList index row if index column 3 退休region SRegionList index row returnregion 2020 2 7 29 8 3視圖 View 4 在頭文件 mainwindow h 中添加代碼如下 include histogramview h private HistogramView histogram 5 在源文件 mainwindow cpp 中添加代碼 其中 setupView 函數(shù)的代碼修改 6 運(yùn)行結(jié)果如圖8 4所示 2020 2 7 30 8 4代理 Delegate 例 難度中等 CH804 利用Delegate設(shè)計(jì)表格中控件如圖8 7所示 2020 2 7 31 8 4代理 Delegate 實(shí)現(xiàn)步驟如下 1 首先 加載表格數(shù)據(jù) 以便后面的操作 源文件 main cpp 中的具體代碼 2 選擇 構(gòu)建 構(gòu)建項(xiàng)目 DateDelegate 菜單項(xiàng) 首先按照如圖8 8所示的格式編輯本例所用的數(shù)據(jù)文件 test txt 保存在項(xiàng)目D Qt CH8 CH804 build DateDelegate Desktop Qt 5 8 0 MinGW 32bit Debug目錄下 然后運(yùn)行程序 結(jié)果如圖8 7所示 2020 2 7 32 8 4代理 Delegate 3 在圖8 7中 使用手動(dòng)的方式實(shí)現(xiàn)對(duì)生日的錄入編輯 下面使用日歷編輯框QDateTimeEdit控件實(shí)現(xiàn)對(duì)生日的編輯 用自定義的Delegate來(lái)實(shí)現(xiàn) 4 DateDelegate繼承自QItemDelegate類(lèi) 頭文件 datedelegate h 中的具體代碼如下 includeclassDateDelegate publicQItemDelegate Q OBJECTpublic DateDelegate QObject parent 0 QWidget createEditor QWidget parent constQStyleOptionViewItem 2020 2 7 33 8 4代理 Delegate 5 源文件 datedelegate cpp 中的具體代碼如下 include datedelegate h includeDateDelegate DateDelegate QObject parent QItemDelegate parent createEditor 函數(shù)的具體實(shí)現(xiàn)代碼如下 QWidget DateDelegate createEditor QWidget parent constQStyleOptionViewItem 2020 2 7 34 8 4代理 Delegate setEditorData 函數(shù)的具體代碼如下 voidDateDelegate setEditorData QWidget editor constQModelIndex 設(shè)置控件的顯示數(shù)據(jù) 2020 2 7 35 8 4代理 Delegate setModelData 函數(shù)的具體代碼如下 voidDateDelegate setModelData QWidget editor QAbstractItemModel model constQModelIndex 2020 2 7 36 8 4代理 Delegate 6 在 main cpp 文件中添加如下代碼 include datedelegate h 在語(yǔ)句tableView setModel 7 此時(shí)運(yùn)行程序 雙擊第1行第2列 將顯示如圖8 9所示的日歷編輯框控件 2020 2 7 37 8 4代理 Delegate 下面使用下拉列表框QComboBox控件實(shí)現(xiàn)對(duì)職業(yè)類(lèi)型的輸入編輯 使用自定義的Delegate實(shí)現(xiàn) 1 ComboDelegate繼承自QItemDelegate類(lèi) 頭文件 combodelegate h 中的具體代碼如下 includeclassComboDelegate publicQItemDelegate Q OBJECTpublic ComboDelegate QObject parent 0 QWidget createEditor QWidget parent constQStyleOptionViewItem 2020 2 7 38 8 4代理 Delegate 2 源文件 combodelegate cpp 中的具體代碼如下 include combodelegate h includeComboDelegate ComboDelegate QObject parent QItemDelegate parent createEditor 函數(shù)中創(chuàng)建了一個(gè)QComboBox控件 并插入可顯示的條目 安裝事件過(guò)濾器 具體代碼如下 QWidget ComboDelegate createEditor QWidget parent constQStyleOptionViewItem 2020 2 7 39 8 4代理 Delegate setEditorData 函數(shù)中更新了Delegate控件中的數(shù)據(jù)顯示 具體代碼如下 voidComboDelegate setEditorData QWidget editor constQModelIndex 2020 2 7 40 8 4代理 Delegate updateEditorGeometry 函數(shù)的

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論