from PySide2 import QtCore, QtGui, QtWidgets class Report(QtWidgets.QDialog): def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) self.setWindowTitle("Report") self.tableWidget = QtWidgets.QTableWidget() self.tableWidget.verticalHeader().hide() self.tableWidget.setColumnCount(3) self.tableWidget.setHorizontalHeaderLabels(["Task", "Start time", "Duration"]) self.header = QtWidgets.QHeaderView(QtCore.Qt.Orientation.Horizontal) self.tableWidget.setHorizontalHeader(self.header) new_button = QtWidgets.QPushButton() new_button.setText("New item") new_button.setIcon(QtGui.QIcon.fromTheme("list-add")) new_button.pressed.connect(self.new_task) del_button = QtWidgets.QPushButton() del_button.setText("Delete item") del_button.setIcon(QtGui.QIcon.fromTheme("list-remove")) del_button.pressed.connect(self.del_task) ok_button = QtWidgets.QPushButton() ok_button.setText("OK") ok_button.setIcon(QtGui.QIcon.fromTheme("dialog-ok-apply")) ok_button.pressed.connect(self.accept) blayout = QtWidgets.QHBoxLayout() blayout.addWidget(new_button) blayout.addWidget(del_button) blayout.addWidget(ok_button) layout = QtWidgets.QVBoxLayout(self) layout.addWidget(self.tableWidget) layout.addLayout(blayout) self.setLayout(layout) def set_data(self, data): self.tableWidget.setRowCount(len(data)) for row, _ in enumerate(data): self.tableWidget.setItem(row, 0, QtWidgets.QTableWidgetItem(data[row][0])) self.tableWidget.setItem(row, 1, QtWidgets.QTableWidgetItem(str(data[row][1]))) self.tableWidget.setItem(row, 2, QtWidgets.QTableWidgetItem(str(data[row][2]))) self.tableWidget.resizeColumnsToContents() min_width = 0 for i in range(3): min_width += self.header.sectionSize(i) self.tableWidget.setMinimumWidth(min_width * 1.33) self.header.setSectionResizeMode(QtWidgets.QHeaderView.Stretch) @QtCore.Slot() def new_task(self): l = self.list.stringList() l.append("") self.list.setStringList(l) i = self.list.index(len(l)-1) self.tableView.setCurrentIndex(i) self.tableView.edit(i) @QtCore.Slot() def del_task(self): l = self.list.stringList() del l[self.tableView.currentIndex().row()] self.list.setStringList(l) @property def tasks(self): ret = self.list.stringList() return list(filter(None, ret)) # filter empty strings @tasks.setter def tasks(self, tasks): self.list.setStringList(tasks)