回答編集履歴

3

補足

2020/11/03 09:41

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -1,3 +1,7 @@
1
+ setBackground には QColor を指定します。
2
+
3
+
4
+
1
5
  ```
2
6
 
3
7
  from PyQt5.QtCore import Qt
@@ -17,6 +21,10 @@
17
21
  item ではなく、widget に設定します。 [Stylesheet Example](https://doc.qt.io/qt-5/stylesheet-examples.html)
18
22
 
19
23
 
24
+
25
+ 宣言的な記述になるので、
26
+
27
+ 動的に&個別に色を変えたい場合には適してませんが、スタイルシートの利用例
20
28
 
21
29
  ```python
22
30
 

2

追記: QListWidget の選択時の色変更 (スタイルシート利用)

2020/11/03 09:41

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -16,7 +16,45 @@
16
16
 
17
17
  item ではなく、widget に設定します。 [Stylesheet Example](https://doc.qt.io/qt-5/stylesheet-examples.html)
18
18
 
19
+
20
+
21
+ ```python
22
+
23
+ import sys
24
+
25
+ from PyQt5.QtCore import Qt
26
+
27
+ from PyQt5.QtWidgets import QApplication, QListWidget
28
+
29
+
30
+
31
+ if __name__ == '__main__':
32
+
33
+ app = QApplication(sys.argv)
34
+
35
+ app.setStyleSheet("""
36
+
19
- ~~ ``※ スタイルに関しては QTableView と同じ。~~
37
+ QListWidget::item:selected {
38
+
39
+ background-color: red;
40
+
41
+ }
42
+
43
+ """)
44
+
45
+ win = QListWidget()
46
+
47
+ for item in ["A", "B", "C"]:
48
+
49
+ win.addItem(item)
50
+
51
+ win.show()
52
+
53
+
54
+
55
+ app.exec_()
56
+
57
+ ```
20
58
 
21
59
 
22
60
 

1

table ではなかったので訂正

2020/11/03 09:34

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -14,17 +14,9 @@
14
14
 
15
15
  "color: red;" はスタイルシートで設定する場合の表記で、
16
16
 
17
- item ではなく、widget に設定します。 [Stylesheet Example](https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qtableview)
17
+ item ではなく、widget に設定します。 [Stylesheet Example](https://doc.qt.io/qt-5/stylesheet-examples.html)
18
18
 
19
- ※ スタイルに関しては QTableView と同じ。
19
+ ~~ ``※ スタイルに関しては QTableView と同じ。~~
20
-
21
-
22
-
23
- ```
24
-
25
- self.tableWidget.setStyleSheet("selection-background-color: red;")
26
-
27
- ```
28
20
 
29
21
 
30
22