在数据库中完成更新后,我想在我的应用程序中添加一项功能,然后pyqt5表中的数据应自动用搜索结果中的新数据刷新。
from PyQt5 import QtCore, QtGui, QtWidgets
import cx_Oracle
class Ui_MainWindow(QtWidgets.QWidget):
switch_update1 = QtCore.pyqtSignal()
def load_table(self):
connection = cx_Oracle.connect("con_str")
cur = connection.cursor()
cur.execute("select MED_ID,MED_BATCH_NO, MED_COMPANY, MED_MFG_DATE, MED_EXP_DATE, MED_TYPE, RECK_NO, SUPPLIER_NUMBER, SUPPLIER_NAME from med_master where upper(med_batch_no) like upper('%"+self.le_batch_no.text()+"%')")
results=cur.fetchall()
self.tab_medicines.setRowCount(len(results)+1)
self.tab_medicines.setColumnCount(10)
print("Loading Table")
"""header = QtWidgets.QHeaderView(1,self.tab_medicines)
header.setStyleSheet( "{spacing: 2px;\n"
"background:light blue;\n"
"color: black;\n"
"}"
)
self.tab_medicines.setHorizontalHeader(header)"""
#self.tab_medicines.setHorizontalHeaderLabels(('Id','Batch No','Company Name','MFG Date','EXP Date','Type','Reck#','Supplier No','Supplier Name','Action'))
self.tab_medicines.setItem(0,0,QtWidgets.QTableWidgetItem("Id"))
self.tab_medicines.setItem(0,1,QtWidgets.QTableWidgetItem("Batch No"))
self.tab_medicines.setItem(0,2,QtWidgets.QTableWidgetItem("Company Name"))
self.tab_medicines.setItem(0,3,QtWidgets.QTableWidgetItem("MFG Date"))
self.tab_medicines.setItem(0,4,QtWidgets.QTableWidgetItem("EXP Date"))
self.tab_medicines.setItem(0,5,QtWidgets.QTableWidgetItem("Type"))
self.tab_medicines.setItem(0,6,QtWidgets.QTableWidgetItem("Reck#"))
self.tab_medicines.setItem(0,7,QtWidgets.QTableWidgetItem("Supplier No"))
self.tab_medicines.setItem(0,8,QtWidgets.QTableWidgetItem("Supplier Name"))
self.tab_medicines.setItem(0,9,QtWidgets.QTableWidgetItem("Action"))
self.tab_medicines.item(0,0).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,1).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,2).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,3).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,4).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,5).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,6).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,7).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,8).setBackground(QtGui.QColor(98,156,213))
self.tab_medicines.item(0,9).setBackground(QtGui.QColor(98,156,213))
cur1 = connection.cursor()
cur1.execute("select MED_ID,MED_BATCH_NO, MED_COMPANY, MED_MFG_DATE, MED_EXP_DATE, MED_TYPE, RECK_NO, SUPPLIER_NUMBER, SUPPLIER_NAME from med_master where upper(med_batch_no) like upper('%"+self.le_batch_no.text()+"%')")
#sqlRow = cur1.fetchone()
#print (sqlRow)
row = 1
while True:
sqlRow = cur1.fetchone()
#print (sqlRow)
if sqlRow == None:
break ##stops while loop if there is no more lines in sql table
for col in range(0, 9): ##otherwise add row into tableWidget
#self.tab_medicines.setStyleSheet('background: blue')
self.upd = QtWidgets.QPushButton(self.centralwidget)
self.upd.setGeometry(QtCore.QRect(400, 450, 141, 31))
font = QtGui.QFont()
font.setBold(True)
font.setPointSize(9)
self.upd.setFont(font)
self.upd.setObjectName("upd")
self.upd.setText("Modify")
self.upd.setStyleSheet("QPushButton {background: '#7A8786';}");
self.tab_medicines.setItem(row, col, QtWidgets.QTableWidgetItem(str(sqlRow[col])))
self.tab_medicines.setCellWidget(row, 9, self.upd)
self.tab_medicines.item(row, col).setBackground(QtGui.QColor(224,224,224))
print (sqlRow[col])
#item1=self.tab_medicines.item(row, 0).text()
#item2= self.tab_medicines.item(row, 1).text()
#print(item1)
#print(item2)
index = QtCore.QPersistentModelIndex(self.tab_medicines.model().index(row, col))
self.upd.clicked.connect(lambda *args, index=index: self.switch_update(index))
#self.upd.clicked.connect(self.switch_update)
row += 1
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1300, 1300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setStyleSheet("QMainWindow {background: '#629CD5';}");
## Header Start
self.l_header = QtWidgets.QLabel(self.centralwidget)
self.l_header.setGeometry(QtCore.QRect(400, 80, 400, 100))
font = QtGui.QFont()
font.setPointSize(20)
font.setBold(True)
self.l_header.setFont(font)
self.l_header.setObjectName("l_header")
self.l_header.setStyleSheet('color: #800080')
## Header End
## Batch No Start
self.le_batch_no = QtWidgets.QLineEdit(self.centralwidget)
self.le_batch_no.setGeometry(QtCore.QRect(200, 200, 161, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.le_batch_no.setFont(font)
self.le_batch_no.setObjectName("le_batch_no")
self.l_batch_no = QtWidgets.QLabel(self.centralwidget)
self.l_batch_no.setGeometry(QtCore.QRect(20, 190, 151, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.l_batch_no.setFont(font)
self.l_batch_no.setObjectName("l_batch_no")
## Batch No End
## Medicine Name Start
self.le_med_name = QtWidgets.QLineEdit(self.centralwidget)
self.le_med_name.setGeometry(QtCore.QRect(200, 250, 250, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.le_med_name.setFont(font)
self.le_med_name.setObjectName("le_med_name")
self.l_med_name = QtWidgets.QLabel(self.centralwidget)
self.l_med_name.setGeometry(QtCore.QRect(20, 240, 151, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.l_med_name.setFont(font)
self.l_med_name.setObjectName("l_med_name")
## Medicine Name End
## Medicine Company Start
self.le_med_company = QtWidgets.QLineEdit(self.centralwidget)
self.le_med_company.setGeometry(QtCore.QRect(200, 300, 250, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.le_med_company.setFont(font)
self.le_med_company.setObjectName("le_med_company")
self.l_med_company = QtWidgets.QLabel(self.centralwidget)
self.l_med_company.setGeometry(QtCore.QRect(20, 290, 151, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.l_med_company.setFont(font)
self.l_med_company.setObjectName("l_med_company")
## Medicine Company End
## Medicine Expiry Date
self.le_exp_date = QtWidgets.QLineEdit(self.centralwidget)
self.le_exp_date.setGeometry(QtCore.QRect(200, 350, 161, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.le_exp_date.setFont(font)
self.le_exp_date.setObjectName("le_exp_date")
self.le_exp_date.installEventFilter(self)
self.l_exp_date = QtWidgets.QLabel(self.centralwidget)
self.l_exp_date.setGeometry(QtCore.QRect(20, 340, 151, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.l_exp_date.setFont(font)
self.l_exp_date.setObjectName("l_exp_date")
## Medicine Expiry Date
## Medicine Type
self.le_med_type = QtWidgets.QLineEdit(self.centralwidget)
self.le_med_type.setGeometry(QtCore.QRect(650, 200, 161, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.le_med_type.setFont(font)
self.le_med_type.setObjectName("le_med_type")
self.l_med_type = QtWidgets.QLabel(self.centralwidget)
self.l_med_type.setGeometry(QtCore.QRect(500, 190, 151, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.l_med_type.setFont(font)
self.l_med_type.setObjectName("l_med_type")
## Medicine Type
## Reck Number
self.le_reck_no = QtWidgets.QLineEdit(self.centralwidget)
self.le_reck_no.setGeometry(QtCore.QRect(650, 250, 161, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.le_reck_no.setFont(font)
self.le_reck_no.setObjectName("le_reck_no")
self.l_reck_no = QtWidgets.QLabel(self.centralwidget)
self.l_reck_no.setGeometry(QtCore.QRect(500, 240, 151, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.l_reck_no.setFont(font)
self.l_reck_no.setObjectName("l_reck_no")
## Reck Number
## Supplier No
self.le_supl_no = QtWidgets.QLineEdit(self.centralwidget)
self.le_supl_no.setGeometry(QtCore.QRect(650, 300, 161, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.le_supl_no.setFont(font)
self.le_supl_no.setObjectName("le_supl_no")
self.l_supl_no = QtWidgets.QLabel(self.centralwidget)
self.l_supl_no.setGeometry(QtCore.QRect(500, 290, 151, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.l_supl_no.setFont(font)
self.l_supl_no.setObjectName("l_supl_no")
## Supplier No
## Supplier Name
self.le_supl_name = QtWidgets.QLineEdit(self.centralwidget)
self.le_supl_name.setGeometry(QtCore.QRect(650, 350, 250, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.le_supl_name.setFont(font)
self.le_supl_name.setObjectName("le_supl_name")
self.l_supl_name = QtWidgets.QLabel(self.centralwidget)
self.l_supl_name.setGeometry(QtCore.QRect(500, 340, 151, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.l_supl_name.setFont(font)
self.l_supl_name.setObjectName("l_supl_name")
## Supplier Name
self.srch = QtWidgets.QPushButton(self.centralwidget)
self.srch.setGeometry(QtCore.QRect(400, 450, 141, 31))
font = QtGui.QFont()
font.setPointSize(10)
self.srch.setFont(font)
self.srch.setObjectName("srch")
self.tab_medicines = QtWidgets.QTableWidget(self.centralwidget)
self.tab_medicines.setGeometry(QtCore.QRect(0, 500, 1300,1300))
self.tab_medicines.setObjectName("tab_medicines")
self.tab_medicines.setColumnCount(0)
self.tab_medicines.setRowCount(0)
self.tab_medicines.setSelectionBehavior(self.tab_medicines.SelectRows)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.srch.clicked.connect(self.load_table)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.l_batch_no.setText(_translate("MainWindow", "Medicine Batch no*"))
self.l_med_name.setText(_translate("MainWindow", "Medicine Name"))
self.l_med_company.setText(_translate("MainWindow", "Medicine Company"))
self.l_exp_date.setText(_translate("MainWindow", "Expiry Date"))
self.l_med_type.setText(_translate("MainWindow", "Medicine Type"))
self.l_reck_no.setText(_translate("MainWindow", "Rack No"))
self.l_supl_no.setText(_translate("MainWindow", "Supplier No"))
self.l_supl_name.setText(_translate("MainWindow", "Supplier Name"))
self.l_header.setText(_translate("MainWindow", "Search Medicine"))
self.srch.setText(_translate("MainWindow", "Search"))
def switch_update(self,index):
self.table_row=index.row()
self.switch_update1.emit()
关闭“更新”窗口后,pyqt5表中的搜索结果应显示更新的值。