-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path02_table_spreadsheet_custom.py
More file actions
133 lines (106 loc) · 5.13 KB
/
Copy path02_table_spreadsheet_custom.py
File metadata and controls
133 lines (106 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#
# MIT License
#
# Copyright (c) 2023-2026 Erriez
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Source: https://github.com/Erriez/pyside6-getting-started
#
from PySide6.QtWidgets import (QApplication, QWidget, QTableWidget, QTableWidgetItem, QVBoxLayout, QLabel)
from PySide6.QtGui import QFont, QBrush, QColor, QPixmap
from PySide6.QtCore import Qt, QSize
from pathlib import Path
import os
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
# Path to find images directory
self.path = Path(__file__).resolve().parent
# Set window size and title
self.resize(400, 250)
self.setWindowTitle("SpreadSheet example")
# https://doc.qt.io/qtforpython/PySide6/QtWidgets/QTableWidget.html
# Create table widget
self.table = QTableWidget(6, 3)
# Set column names
self.table.setHorizontalHeaderLabels(["Name", "Age", "Gender"])
self.table.horizontalHeader().setStyleSheet(f"background-color: lightblue;")
self.table.verticalHeader().setStyleSheet(f"background-color: lightgreen;")
# Fill table with data
self.table.setItem(0, 0, QTableWidgetItem("Jan"))
self.table.setItem(0, 1, QTableWidgetItem("53"))
self.table.setItem(0, 2, QTableWidgetItem("Male"))
self.table.setItem(1, 0, QTableWidgetItem("Henk"))
self.table.setItem(1, 1, QTableWidgetItem("45"))
self.table.setItem(1, 2, QTableWidgetItem("Male"))
self.table.setItem(2, 0, QTableWidgetItem("Linda"))
self.table.setItem(2, 1, QTableWidgetItem("18"))
self.table.setItem(2, 2, QTableWidgetItem("Female"))
self.table.setItem(3, 0, QTableWidgetItem("Kees"))
self.table.setItem(3, 1, QTableWidgetItem("72"))
self.table.setItem(3, 2, QTableWidgetItem("Male"))
self.table.setItem(4, 0, QTableWidgetItem("Lotte"))
self.table.setItem(4, 1, QTableWidgetItem("39"))
self.table.setItem(4, 2, QTableWidgetItem("Female"))
# ---------------------------
# Set cell with different font and colors
font = QFont("Arial", 14) # Create font with size
font.setBold(True) # Make font bold
item_name = QTableWidgetItem("Anne")
item_name.setFont(font) # Set cell font
item_name.setTextAlignment(Qt.AlignCenter) # Set cell text alignment
item_name.setForeground(QBrush(QColor(Qt.yellow))) # Set cell foreground color
item_name.setBackground(QBrush(QColor(Qt.red))) # Set cell background color
item_name.setFlags(item_name.flags() ^ Qt.ItemIsEditable) # Disable cell editing
# Create table cell picture
self.label_widget = QLabel()
self.label_widget.setPixmap(
QPixmap(os.path.join(self.path, '../images/web.png')).scaled(QSize(20, 20), Qt.KeepAspectRatio))
# Add data to table row
self.table.setItem(5, 0, item_name)
self.table.setItem(5, 1, QTableWidgetItem("28"))
self.table.setCellWidget(5, 2, self.label_widget)
# ---------------------------
# Set table column width
self.table.setColumnWidth(0, 150)
# Make table sortable by clicking on the header
self.table.setSortingEnabled(True)
# Add table to layout, add layout to widget
self.vBox = QVBoxLayout()
self.vBox.addWidget(self.table)
self.setLayout(self.vBox)
# Add signals
self.table.cellClicked.connect(self.on_cell_click)
self.table.itemChanged.connect(self.on_item_changed)
self.table.itemSelectionChanged.connect(self.on_item_selection_changed)
def on_cell_click(self, row, column):
print(f"Clicked {row} {column}")
def on_item_changed(self, item):
print(f"Item {item.row(), item.column()} changed to {item.text()}")
def on_item_selection_changed(self):
print(f"Item selection changed {self.table.currentRow(), self.table.currentColumn()}")
def main():
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()