feat: 按描述搜索功能

This commit is contained in:
iridiumR 2023-06-05 17:42:56 +08:00
parent 35d8cad28f
commit 539b6f868f
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8

View file

@ -15,12 +15,15 @@ class TransTab(TabPage):
# 创建顶部控件
transAddButton = QPushButton('Add')
transAddButton.clicked.connect(self.onTransAddClicked)
self.transSearchButton = QPushButton('Search')
self.transSearchButton.clicked.connect(self.onTransSearchClicked)
transModifyButton = QPushButton('Modify')
transModifyButton.clicked.connect(self.onTransModifyClicked)
# 创建控件布局
topLayout = QHBoxLayout()
topLayout.addWidget(transAddButton)
topLayout.addWidget(self.transSearchButton)
topLayout.addWidget(transModifyButton)
self.transTable = QTableWidget()
@ -238,7 +241,8 @@ class TransTab(TabPage):
self.dialogLayout.itemAt(5).widget().addItem(self.accountData[i][1])
def selected(self):
self.pg.execute("SELECT t_id, a_id, c_id, s_id, time, translate(amount::varchar,'$',''), meta->>'description' FROM transaction ORDER BY time DESC")
self.pg.execute("SELECT t_id, a_id, c_id, s_id, time, translate(amount::varchar,'$',''), meta->>'description' \
FROM transaction ORDER BY time DESC")
self.transData = self.pg.fetchall()
self.pg.execute("SELECT a_id, meta ->> 'name' FROM account ORDER BY a_id")
self.accountData = self.pg.fetchall()
@ -264,3 +268,66 @@ class TransTab(TabPage):
self.transTable.setItem(i, 2, QTableWidgetItem(self.categoryData[self.transData[i][2]-1][1]))
self.transTable.setItem(i, 3, QTableWidgetItem(str(self.transData[i][5])))
self.transTable.setItem(i, 4, QTableWidgetItem(self.transData[i][6]))
def onTransSearchClicked(self):
# 若已经搜索,则重置
if self.transSearchButton.text() == "Reset":
self.selected()
self.transSearchButton.setText("Search")
return
self.tDialog = QDialog(self)
self.tDialog.setWindowTitle('Search Transaction')
discLabel=QLabel("Discription:")
discLine=QLineEdit()
discLine.setText("")
cButton = QPushButton("Confirm")
aButton = QPushButton("Abort")
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(cButton)
buttonLayout.addWidget(aButton)
cButton.clicked.connect(self.tDialog.accept)
aButton.clicked.connect(self.tDialog.reject)
dialogLayout = QFormLayout()
dialogLayout.addRow(discLabel, discLine)
dialogLayout.addRow(buttonLayout)
dialogLayout.setSpacing(12)
dialogLayout.setContentsMargins(15, 15, 15, 15)
self.tDialog.setMinimumWidth(400)
self.tDialog.setLayout(dialogLayout)
if self.tDialog.exec() == QDialog.DialogCode.Accepted:
discription = discLine.text()
self.pg.execute("SELECT t_id, a_id, c_id, s_id, time, translate(amount::varchar,'$',''), meta->>'description'\
FROM transaction WHERE meta->>'description' LIKE %s ORDER BY time DESC",('%'+discription+'%',))
self.transData = self.pg.fetchall()
self.pg.execute("SELECT a_id, meta ->> 'name' FROM account ORDER BY a_id")
self.accountData = self.pg.fetchall()
self.pg.execute("SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
self.categoryData = self.pg.fetchall()
# 设置表格
self.transTable.clear()
self.transTable.setRowCount(len(self.transData))
self.transTable.setColumnCount(5) # 时间 账户 类别 金额 描述
self.transTable.setHorizontalHeaderLabels(['Time', 'Account', 'Category', 'Amount', 'Description'])
# 设置自动调整列宽
self.transTable.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
# 填充表格
for i in range(len(self.transData)):
self.transTable.setItem(i, 0, QTableWidgetItem(self.transData[i][4].strftime("%Y-%m-%d %H:%M:%S")))
self.transTable.setItem(i, 1, QTableWidgetItem(self.accountData[self.transData[i][1]-1][1]))
if self.transData[i][2] is None:
self.transTable.setItem(i, 2, QTableWidgetItem("None"))
else:
self.transTable.setItem(i, 2, QTableWidgetItem(self.categoryData[self.transData[i][2]-1][1]))
self.transTable.setItem(i, 3, QTableWidgetItem(str(self.transData[i][5])))
self.transTable.setItem(i, 4, QTableWidgetItem(self.transData[i][6]))
# 设置按钮文本为重置
self.transSearchButton.setText("Reset")