锋哥原创的PyQt6图书管理系统视频教程:
定义initBookTypeListComboBox方法
def initBookTypeListComboBox(self):
"""
初始化下拉框数据
:return:
"""
bookTypeList = bookTypeDao.list("") # 获取所有图书类别信息
self.s_bookTypeComboBox.addItem("请选择图书类别...", -1)
for bookType in bookTypeList:
self.s_bookTypeComboBox.addItem(bookType[1], bookType[0])
构造方法里调用
bookDao里定义list方法:
def list(s_book: Book):
"""
根据条件查询图书信息
:return:返回查询的图书信息列表
"""
con = None
try:
con = dbUtil.getCon()
cursor = con.cursor()
sql = "SELECT t.id AS id,bookName,author,bookTypeName,sex,price,bookDesc FROM t_book AS t ,t_booktype AS tk WHERE t.`bookTypeId`=tk.`id`"
if s_book != None:
if s_book.bookName.strip() != '':
sql += " and t.bookName like '%" + s_book.bookName + "%'"
if s_book.author.strip() != '':
sql += " and t.author like '%" + s_book.author + "%'"
if s_book.bookTypeId != -1:
sql += " and t.bookTypeId = " + str(s_book.bookTypeId)
print(sql)
cursor.execute(sql)
return cursor.fetchall()
except Exception as e:
print(e)
con.rollback()
return None
finally:
dbUtil.closeCon(con)
bookManage.py里再定义initTable方法
def initTable(self):
"""
根据条件初始化数据表格
:return:
"""
bookName = self.s_nameInput.text()
author = self.s_authorInput.text()
bookTypeId = self.s_bookTypeComboBox.currentData()
s_book = Book(bookName, author, bookTypeId)
result = bookDao.list(s_book)
row = 0
if result:
row = len(result)
self.bookTable.setColumnCount(7)
self.bookTable.setRowCount(row)
self.bookTable.verticalHeader().setVisible(False) # 隐藏垂直标题 序号
self.bookTable.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers) # 禁止编辑单元格
self.bookTable.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
self.bookTable.setHorizontalHeaderLabels(
['编号', '图书名称', '图书作者', '图书类别', '作者性别', '图书价格', '图书描述'])
self.bookTable.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) # 列头自适应
self.bookTable.setSelectionBehavior(
QAbstractItemView.SelectionBehavior.SelectRows) # 设置选择行为,以行为单位
for i in range(row):
for j in range(7):
data = QTableWidgetItem(str(result[i][j]))
self.bookTable.setItem(i, j, data)
构造方法里调用
搜索按钮绑定事件
# 搜索按钮点击事件
self.s_searchBtn.clicked.connect(self.initTable)
测试: