QTableView樣式分多個區域
整體樣式
1 QTableView { 2 color: white; /*表格內文字顏色*/
3 gridline-color: black; /*表格內框顏色*/
4 background-color: rgb(108, 108, 108); /*表格內背景色*/
5 alternate-background-color: rgb(64, 64, 64); 6 selection-color: white; /*選中區域的文字顏色*/
7 selection-background-color: rgb(77, 77, 77); /*選中區域的背景色*/
8 border: 2px groove gray; 9 border-radius: 0px; 10 padding: 2px 4px; 11 }
奇偶行區分顏色樣式
1 cpp: 2 view->setAlternatingRowColors(true); 3
4 qss: 5 QTableView{ 6 background-color: rgb(250, 250, 115); 7 alternate-background-color: rgb(141, 163, 215); 8 }
水平/垂直表頭
設置表頭樣式
1 QHeaderView { 2 color: white; 3 font: bold 10pt; 4 background-color: rgb(108, 108, 108);/*設置表頭空白區域背景色*/
5 border: 0px solid rgb(144, 144, 144); 6 border:0px solid rgb(191,191,191); 7 border-left-color: rgba(255, 255, 255, 0); 8 border-top-color: rgba(255, 255, 255, 0); 9 border-radius:0px; 10 min-height:29px; 11 }
為垂直及水平表頭分別設置樣式
表頭為QHeaderView,水平表頭和垂直表頭都是QHeaderView,需要設置QTableView兩個表頭的ObjectName進行區分
1 view->horizontalHeader()->setObjectName("hHeader"); 2 view->verticalHeader()->setObjectName("vHeader");
樣式中根據不同對象名稱進行樣式設置
1 QHeaderView#hHeader::section { 2 background-color:darkcyan; 3 color: red;padding-left: 4px; 4 border: 1px solid #6c6c6c; 5 height:40; 6 } 7 QHeaderView#vHeader::section { 8 background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,stop:0 #616161, stop: 0.5 yellow,stop: 0.6 green, stop:1 cyan); 9 color: white;padding-left: 4px;border: 1px solid #6c6c6c; 10 width:30; 11 }
左上角空白區域
設置完QTableView頭部后,左上角區域為白色,需要為左上角區域設置樣式
1 /*QTableView 左上角樣式*/
2 QTableView QTableCornerButton::section { 3 /* background: red; 4 border: 2px outset red;*/
5 color: red; 6 background-color: rgb(64, 64, 64); 7 border: 5px solid #f6f7fa; 8 border-radius:0px; 9 border-color: rgb(64, 64, 64); 10 }
表頭內容區域
1 QHeaderView::section { 2 color: white; 3 background-color: rgb(64, 64, 64); 4 border: 5px solid #f6f7fa; 5 border-radius:0px; 6 border-color: rgb(64, 64, 64); 7 }
全部樣式代碼:
1 /*QTableView 左上角樣式*/
2 QTableView QTableCornerButton::section { 3 /* background: red; 4 border: 2px outset red;*/
5 color: red; 6 background-color: rgb(64, 64, 64); 7 border: 5px solid #f6f7fa; 8 border-radius:0px; 9 border-color: rgb(64, 64, 64); 10 } 11
12 QTableView { 13 color: white; /*表格內文字顏色*/
14 gridline-color: black; /*表格內框顏色*/
15 background-color: rgb(108, 108, 108); /*表格內背景色*/
16 alternate-background-color: rgb(64, 64, 64); 17 selection-color: white; /*選中區域的文字顏色*/
18 selection-background-color: rgb(77, 77, 77); /*選中區域的背景色*/
19 border: 2px groove gray; 20 border-radius: 0px; 21 padding: 2px 4px; 22 } 23
24 QHeaderView { 25 color: white; 26 font: bold 10pt; 27 background-color: rgb(108, 108, 108); 28 border: 0px solid rgb(144, 144, 144); 29 border:0px solid rgb(191,191,191); 30 border-left-color: rgba(255, 255, 255, 0); 31 border-top-color: rgba(255, 255, 255, 0); 32 border-radius:0px; 33 min-height:29px; 34 } 35
36 QHeaderView::section { 37 color: white; 38 background-color: rgb(64, 64, 64); 39 border: 5px solid #f6f7fa; 40 border-radius:0px; 41 border-color: rgb(64, 64, 64); 42 }