创建表格
from PyQt5.QtWidgets import QTableView,QStyledItemDelegate, QComboBox,QTextEdit from PyQt5.QtCore import QAbstractTableModel,Qt, QModelIndex,QEvent,QRect,QSize,QVariant from PyQt5.QtGui import QFontMetrics class TaskTable(QTableWidget): def __init__(self,row_count,col_count): super().__init__(row_count,col_count) self.setHorizontalHeaderLabels(['Level', 'To do List','Progress','Sub Task','Duration','Comment','Category']) # self.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) self.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive) self.setColumnWidth(0,100) self.setColumnWidth(1,340) self.setColumnWidth(2,100) self.setColumnWidth(3,400) self.setColumnWidth(4,100) self.setColumnWidth(5,200) self.setColumnWidth(6,100) self.verticalHeader().hide() self.horizontalHeader().setStyleSheet("QHeaderView::section { background-color: lightblue; }") for row in range(row_count): item = QTableWidgetItem() item.setFlags(item.flags()|Qt.ItemIsUserCheckable) item.setCheckState(Qt.Unchecked) self.setItem(row, 3, item) items_in_progress = ['Not start', 'In progress', 'Done','Pending others','Done but keep'] items_in_cagetory = ['MOMA', 'SIPG', 'APILearning','Others','WorkRule'] combox_progress = self.addcomBox(items_in_progress,0) self.setCellWidget(row, 2, combox_progress) combox_progress = self.addcomBox(items_in_cagetory,3) self.setCellWidget(row, 6, combox_progress) def addcomBox(self,items,default_index): comboBox1 = QComboBox() comboBox1.addItems(items) comboBox1.setCurrentIndex(default_index) return comboBox1 # 设置默认值,例如 'Option A2' def getvalue(self,row,columnindex): cell = self.cellWidget(row, columnindex) if columnindex in [2, 6] else self.item(row, columnindex) if cell and isinstance(cell, QComboBox): return cell.currentText() elif cell: value = cell.text() try: return int(value) except ValueError: return value else: return ''
显示窗口
import sys from PyQt5.QtWidgets import * # QApplication,QMainWindow,QWidget,QLabel,QVBoxLayout from PyQt5.QtGui import QIcon from PyQt5.QtCore import Qt from GUI.task import TaskTable from GUI.date import DateLabel from GUI.control import controls from Back.db import initDB iconpath = 'refer\MainLogo.png' class MainWindow(QMainWindow): def __init__(self): try: super().__init__() self.initUI() except Exception as e: print(e) def initUI(self): try: self.setWindowTitle("JXY_Note") self.setWindowIcon(QIcon(iconpath)) central_widget = QWidget(self) # self.central_widget.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding) self.setCentralWidget(central_widget) # print(self.centralWidget().size()) layout = QVBoxLayout(central_widget) self.contrals = controls() self.table_date = DateLabel() self.taskdate = self.table_date.text() self.data = self.init_data(self.taskdate) self.table_view = TaskTable(self.data) layout.addWidget(self.table_date) layout.addWidget(self.table_view) print("add table") self.save_button = QPushButton('Save') self.save_button.clicked.connect(self.save_data) self.retrieve_button = QPushButton('Retrieve') self.retrieve_button.clicked.connect(self.retrieve_data) self.clear_button = QPushButton('Clear') self.clear_button.clicked.connect(self.clear_data) layout.addWidget(self.save_button) layout.addWidget(self.retrieve_button) layout.addWidget(self.clear_button) self.showMaximized() except Exception as e: print(e) def save_data(self): data = [] for row in range(self.table_view.rowCount()): row_data = [] if self.table_view.item(row,1): # state = self.table_view.item(row,3).checkState() # checkStatus = "1" if state == Qt.Checked else "0" checkStatus = "0" taskdate = self.table_date.text() for col in range(self.table_view.columnCount()): cell = self.table_view.getvalue(row,col) row_data.append(cell) row_data.insert(4,checkStatus) row_data.insert(9,taskdate) data.append(tuple(row_data)) print(data) self.contrals.save_data_task(data) QMessageBox.information(self, "Success","Data saved successfully!") def retrieve_data(self): taskdate = self.table_date.text() print(taskdate) rows = self.contrals.retrieve_data_task(taskdate) print(rows) self.initUI() for row_idx, (ids,level,to_do,progress,subtask,subtask_status,duration,comment,category,date,dependance) in enumerate(rows): self.table_view.setItem(row_idx,0,QTableWidgetItem(str(level))) self.table_view.setItem(row_idx,1,QTableWidgetItem(str(to_do))) progress_cellcombox = self.table_view.cellWidget(row_idx,2) progress_index = progress_cellcombox.findText(str(progress)) if progress_index >= 0: progress_cellcombox.setCurrentIndex(progress_index) else: progress_cellcombox.setCurrentIndex(0) check = QTableWidgetItem(subtask) if subtask_status == '0': check.setCheckState(Qt.Unchecked) self.table_view.setItem(row_idx, 3, check) if subtask_status == '1': check.setCheckState(Qt.Checked) self.table_view.setItem(row_idx, 3, check) self.table_view.setItem(row_idx,4,QTableWidgetItem(str(duration))) self.table_view.setItem(row_idx,5,QTableWidgetItem(str(comment))) category_combox = self.table_view.cellWidget(row_idx,6) category_index = category_combox.findText(str(category)) if category_index >= 0: category_combox.setCurrentIndex(category_index) else: category_combox.setCurrentIndex(0) def init_data(self,taskdate): rows = self.contrals.retrieve_data_task(taskdate) data = [] for row_idx, (ids,level,to_do,progress,subtask,subtask_status,duration,comment,category,date,dependance) in enumerate(rows): row = [level,to_do,progress,subtask,duration,comment,category] data.append(row) return data def clear_data(self): self.db = initDB() self.db.delete_data("tasks") self.retrieve_data() if __name__ == '__main__': app = QApplication(sys.argv) ex = MainWindow() ex.show() sys.exit(app.exec_())
代码不全,仅做练习使用
标签:self,Qtablewidget,PyQt5,progress,table,view,data,row From: https://www.cnblogs.com/woniuguoguo/p/18547490