# CollectibleDialog.py import sqlite3 from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QLineEdit, QDialogButtonBox, QTreeWidgetItem, QCheckBox, \ QMessageBox import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s: %(message)s') logger = logging.getLogger(__name__) class CollectibleDialog(QDialog): def __init__(self, parent=None, title="添加收藏品", initial_code="", initial_name=""): super().__init__(parent) self.setWindowTitle(title) self.setGeometry(100, 100, 400, 150) layout = QVBoxLayout() # 收藏品编码输入框 code_label = QLabel("收藏品编码") self.collectible_code_edit = QLineEdit() self.collectible_code_edit.setText(initial_code) layout.addWidget(code_label) layout.addWidget(self.collectible_code_edit) # 收藏品名称输入框 name_label = QLabel("收藏品名称") self.collectible_name_edit = QLineEdit() self.collectible_name_edit.setText(initial_name) layout.addWidget(name_label) layout.addWidget(self.collectible_name_edit) button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) button_box.accepted.connect(self.accept) button_box.rejected.connect(self.reject) layout.addWidget(button_box) self.setLayout(layout) @staticmethod def add_collectible(self): dialog = CollectibleDialog(self, "添加收藏品") if dialog.exec(): collectible_code = dialog.collectible_code_edit.text() collectible_name = dialog.collectible_name_edit.text() if collectible_code and collectible_name: item = QTreeWidgetItem(self.collectible_tree_widget) item.setText(1, collectible_code) item.setText(2, collectible_name) checkbox = QCheckBox() checkbox.setChecked(False) self.collectible_tree_widget.setItemWidget(item, 0, checkbox) @staticmethod def delete_collectible(self): selected_items = self.collectible_tree_widget.selectedItems() for item in selected_items: (item.parent() or self.collectible_tree_widget).takeTopLevelItem(self.collectible_tree_widget.indexOfTopLevelItem(item)) @staticmethod def edit_collectible(self): selected_items = self.collectible_tree_widget.selectedItems() if len(selected_items) != 1: QMessageBox.warning(self, "编辑收藏品", "请选择一个收藏品进行编辑") return item = selected_items[0] dialog = CollectibleDialog(self, "编辑收藏品", item.text(1), item.text(2)) if dialog.exec(): new_collectible_code = dialog.collectible_code_edit.text() new_collectible_name = dialog.collectible_name_edit.text() if new_collectible_code and new_collectible_name: item.setText(1, new_collectible_code) item.setText(2, new_collectible_name) @staticmethod def save_collectibles(self): # 连接到SQLite数据库 conn = sqlite3.connect('buff.db') cursor = conn.cursor() # 清空表中的数据 cursor.execute('DELETE FROM collectibles') # 获取树形控件中的所有项 for i in range(self.collectible_tree_widget.topLevelItemCount()): item = self.collectible_tree_widget.topLevelItem(i) collectible_code = item.text(1) collectible_name = item.text(2) checkbox = self.collectible_tree_widget.itemWidget(item, 0) selected = checkbox.isChecked() # 获取复选框的状态 # 插入数据到表中 cursor.execute(''' INSERT INTO collectibles (code, name,selected) VALUES (?, ?,?) ''', (collectible_code, collectible_name,selected)) # 提交更改并关闭连接 conn.commit() conn.close() QMessageBox.information(self, "保存收藏品信息", "收藏品信息已成功保存")