




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、QT QTableView用法總結(jié)QTableView常用于實(shí)現(xiàn)數(shù)據(jù)的表格顯示。下面我們?nèi)绾伟床襟E實(shí)現(xiàn)學(xué)生信息表格:一 添加表頭 /準(zhǔn)備數(shù)據(jù)模型 QStandardItemModel *student_model = new QStandardItemModel(); student_model-setHorizontalHeaderItem(0, new QStandardItem(QObject:tr(Name); student_model-setHorizontalHeaderItem(1, new QStandardItem(QObject:tr(NO.); student_mode
2、l-setHorizontalHeaderItem(2, new QStandardItem(QObject:tr(Sex); student_model-setHorizontalHeaderItem(3, new QStandardItem(QObject:tr(Age); student_model-setHorizontalHeaderItem(4, new QStandardItem(QObject:tr(College); /利用setModel()方法將數(shù)據(jù)模型與QTableView綁定 ui-student_tableview-setModel(student_model);二
3、 設(shè)置表格屬性 /設(shè)置列寬不可變動,即不能通過鼠標(biāo)拖動增加列寬 ui-student_tableview-horizontalHeader()-setResizeMode(0, QHeaderView:Fixed); ui-student_tableview-horizontalHeader()-setResizeMode(1, QHeaderView:Fixed); ui-student_tableview-horizontalHeader()-setResizeMode(2, QHeaderView:Fixed); ui-student_tableview-horizontalHeader
4、()-setResizeMode(3, QHeaderView:Fixed); ui-student_tableview-horizontalHeader()-setResizeMode(4, QHeaderView:Fixed); /設(shè)置表格的各列的寬度值 ui-student_tableview-setColumnWidth(0,100); ui-student_tableview-setColumnWidth(1,100); ui-student_tableview-setColumnWidth(2,100); ui-student_tableview-setColumnWidth(3,
5、100); ui-student_tableview-setColumnWidth(4,100); /默認(rèn)顯示行頭,如果你覺得不美觀的話,我們可以將隱藏 ui-student_tableview-verticalHeader()-hide(); /設(shè)置選中時為整行選中 ui-student_tableview-setSelectionBehavior(QAbstractItemView:SelectRows); /設(shè)置表格的單元為只讀屬性,即不能編輯 ui-student_tableview-setEditTriggers(QAbstractItemView:NoEditTriggers);
6、/如果你用在QTableView中使用右鍵菜單,需啟用該屬性 ui-tstudent_tableview-setContextMenuPolicy(Qt:CustomContextMenu);三 動態(tài)添加行 在表格中添加行時,我們只需要在model中插入數(shù)據(jù)即可,一旦model中的數(shù)據(jù)發(fā)生變化,QTabelView顯示就會做相應(yīng)的變動 /在第一行添加學(xué)生張三的個人信息(setItem函數(shù)的第一個參數(shù)表示行號,第二個表示列號,第三個為要顯示的數(shù)據(jù)) student_model-setItem(0, 0, new QStandardItem(“張三); student_model-setItem(
7、0, 1, new QStandardItem(20120202); student_model-setItem(0, 2, new QStandardItem(男); student_model-setItem(0, 3, new QStandardItem(18); student_model-setItem(0, 4, new QStandardItem(土木學(xué)院);四 設(shè)置數(shù)據(jù)顯示的樣式 /設(shè)置單元格文本居中,張三的數(shù)據(jù)設(shè)置為居中顯示 student_model-item(0, 0)-setTextAlignment(Qt:AlignCenter); student_model-ite
8、m(0, 1)-setTextAlignment(Qt:AlignCenter); student_model-item(0, 2)-setTextAlignment(Qt:AlignCenter); student_model-item(0, 3)-setTextAlignment(Qt:AlignCenter); student_model-item(0, 4)-setTextAlignment(Qt:AlignCenter); /設(shè)置單元格文本顏色,張三的數(shù)據(jù)設(shè)置為紅色 student_model-item(0, 0)-setForeground(QBrush(QColor(255, 0
9、, 0); student_model-item(0, 1)-setForeground(QBrush(QColor(255, 0, 0); student_model-item(0, 2)-setForeground(QBrush(QColor(255, 0, 0); student_model-item(0, 3)-setForeground(QBrush(QColor(255, 0, 0); student_model-item(0, 4)-setForeground(QBrush(QColor(255, 0, 0); /將字體加粗 student_model-item(0, 0)-setFont( QFont( Times, 10, QFont:Black ) ); student_model-item(0, 1)-setFont( QFont( Times, 10, QFont:Black ) ); student_model-item(0, 2)-setFont( QFont( Times, 10, QFont:Black ) ); student_model-item(0, 3)-setFont( QFont( Times, 10, QF
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 完善流程合規(guī)性的工作策略計(jì)劃
- 倉庫服務(wù)質(zhì)量的提升路徑計(jì)劃
- 跨國公司投資決策的全球性財(cái)務(wù)分析案例
- 小學(xué)語文作文八十天環(huán)游世界讀后感1
- 新課標(biāo)天津?qū)S?024高考?xì)v史二輪復(fù)習(xí)專題提升訓(xùn)練17中外歷史人物評說
- 超聲圖像質(zhì)量分析與質(zhì)量控制策略
- 浙江2025年01月浙江省溫嶺市殯儀館2025年公開招考2名編制外工作人員筆試歷年典型考題(歷年真題考點(diǎn))解題思路附帶答案詳解
- 查鄉(xiāng)小學(xué)體育課教案
- 足浴店日常清潔消毒工作規(guī)范
- 運(yùn)動鍛煉在血液病治療中的作用
- 2025年黑龍江旅游職業(yè)技術(shù)學(xué)院單招職業(yè)技能測試題庫含答案
- 工藝技術(shù)人員工作總結(jié)
- 醫(yī)院護(hù)理人文關(guān)懷實(shí)踐規(guī)范專家共識課件
- DeepSeek在自然災(zāi)害預(yù)警中的潛力
- 《研學(xué)旅行課程設(shè)計(jì)》課件-研學(xué)課程設(shè)計(jì)計(jì)劃
- 中醫(yī)痹癥-課件
- 尿液結(jié)晶教學(xué)課件
- 繪本《你很特別》
- 茶葉揉捻機(jī)總體設(shè)計(jì)方案的擬定
- 蘇州大學(xué)應(yīng)用技術(shù)學(xué)院財(cái)務(wù)管理
- 粱、柱、板截面取值原則最終
評論
0/150
提交評論