CollectibleDialog.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # CollectibleDialog.py
  2. import sqlite3
  3. from PyQt6.QtWidgets import QDialog, QVBoxLayout, QLabel, QLineEdit, QDialogButtonBox, QTreeWidgetItem, QCheckBox, \
  4. QMessageBox
  5. class CollectibleDialog(QDialog):
  6. def __init__(self, parent=None, title="添加收藏品", initial_code="", initial_name=""):
  7. super().__init__(parent)
  8. self.setWindowTitle(title)
  9. self.setGeometry(100, 100, 400, 150)
  10. layout = QVBoxLayout()
  11. # 收藏品编码输入框
  12. code_label = QLabel("收藏品编码")
  13. self.collectible_code_edit = QLineEdit()
  14. self.collectible_code_edit.setText(initial_code)
  15. layout.addWidget(code_label)
  16. layout.addWidget(self.collectible_code_edit)
  17. # 收藏品名称输入框
  18. name_label = QLabel("收藏品名称")
  19. self.collectible_name_edit = QLineEdit()
  20. self.collectible_name_edit.setText(initial_name)
  21. layout.addWidget(name_label)
  22. layout.addWidget(self.collectible_name_edit)
  23. button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
  24. button_box.accepted.connect(self.accept)
  25. button_box.rejected.connect(self.reject)
  26. layout.addWidget(button_box)
  27. self.setLayout(layout)
  28. @staticmethod
  29. def add_collectible(self):
  30. dialog = CollectibleDialog(self, "添加收藏品")
  31. if dialog.exec():
  32. collectible_code = dialog.collectible_code_edit.text()
  33. collectible_name = dialog.collectible_name_edit.text()
  34. if collectible_code and collectible_name:
  35. item = QTreeWidgetItem(self.collectible_tree_widget)
  36. item.setText(1, collectible_code)
  37. item.setText(2, collectible_name)
  38. checkbox = QCheckBox()
  39. checkbox.setChecked(False)
  40. self.collectible_tree_widget.setItemWidget(item, 0, checkbox)
  41. @staticmethod
  42. def delete_collectible(self):
  43. selected_items = self.collectible_tree_widget.selectedItems()
  44. for item in selected_items:
  45. (item.parent() or self.collectible_tree_widget).takeTopLevelItem(self.collectible_tree_widget.indexOfTopLevelItem(item))
  46. @staticmethod
  47. def edit_collectible(self):
  48. selected_items = self.collectible_tree_widget.selectedItems()
  49. if len(selected_items) != 1:
  50. QMessageBox.warning(self, "编辑收藏品", "请选择一个收藏品进行编辑")
  51. return
  52. item = selected_items[0]
  53. dialog = CollectibleDialog(self, "编辑收藏品", item.text(1), item.text(2))
  54. if dialog.exec():
  55. new_collectible_code = dialog.collectible_code_edit.text()
  56. new_collectible_name = dialog.collectible_name_edit.text()
  57. if new_collectible_code and new_collectible_name:
  58. item.setText(1, new_collectible_code)
  59. item.setText(2, new_collectible_name)
  60. @staticmethod
  61. def save_collectibles(self):
  62. # 连接到SQLite数据库
  63. conn = sqlite3.connect('buff.db')
  64. cursor = conn.cursor()
  65. # 清空表中的数据
  66. cursor.execute('DELETE FROM collectibles')
  67. # 获取树形控件中的所有项
  68. for i in range(self.collectible_tree_widget.topLevelItemCount()):
  69. item = self.collectible_tree_widget.topLevelItem(i)
  70. collectible_code = item.text(1)
  71. collectible_name = item.text(2)
  72. checkbox = self.collectible_tree_widget.itemWidget(item, 0)
  73. selected = checkbox.isChecked() # 获取复选框的状态
  74. # 插入数据到表中
  75. cursor.execute('''
  76. INSERT INTO collectibles (code, name,selected)
  77. VALUES (?, ?,?)
  78. ''', (collectible_code, collectible_name,selected))
  79. # 提交更改并关闭连接
  80. conn.commit()
  81. conn.close()
  82. QMessageBox.information(self, "保存收藏品信息", "收藏品信息已成功保存")