The orderbook in DB always shows the right orderbook, i just want to continually update it in a GUI. I've been trying to get this to work but when i try to run a cur statement the app just crashes. I suspect i need some help to not have to clear the tableelement each time too, but only update items that have changed(i'm assuming this is a smart thing but im not sure). If i have to use Qt's postgresql lib i will, but then you have to help me how to get that running :)
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal, QThread
import psycopg2
import sys
import time
conn = psycopg2.connect("dbname=myDb user=kris")
cur = conn.cursor()
class Ui_MainWindow(QThread):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(50, 50, 711, 481))
self.tableWidget.setRowCount(3)
self.tableWidget.setColumnCount(4)
self.tableWidget.setHorizontalHeaderLabels(['Sum', 'Total', 'Size', 'Bid'])
self.tableWidget.setObjectName("tableWidget")
self.btnBuy = QtWidgets.QPushButton(self.centralwidget)
self.btnBuy.setGeometry(QtCore.QRect(50, 10, 91, 33))
self.btnBuy.setObjectName("btnBuy")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 29))
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)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.btnBuy.setText(_translate("MainWindow", "BUY"))
class MainUIClass(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__()
self.setupUi(self)
self.threadclass = ThreadClass()
self.threadclass.start()
self.threadclass.updateTable.connect(self.updateTable)
def updateTable(self, val):
print(val)
if 'setRowCount' in val:
self.tableWidget.setRowCount(val['setRowCount'])
elif 'insertRow' in val:
self.tableWidget.insertRow(val['insertRow'])
elif 'setItem' in val:
rowNum = val['setItem'][0]
colNum = val['setItem'][1]
data = val['setItem'][2]
self.tableWidget.setItem(rowNum, colNum+2, QtWidgets.QTableWidgetItem(str(data)))
class ThreadClass(QtCore.QThread):
updateTable = pyqtSignal(dict)
def __init__(self, parent=None):
super().__init__()
def run(self):
cur.execute("select quantity, rate from orderbook where type = %s and market = %s order by rate asc limit 10;", (0, "BTC-ADA"))
orderbook = cur.fetchall()
conn.commit()
for rowNum, rowData in enumerate(orderbook):
self.updateTable.emit({"insertRow": 0})
for colNum, data in enumerate(rowData):
self.updateTable.emit({"setItem": [rowNum, colNum, data]})
if __name__ == "__main__":
a = QtWidgets.QApplication(sys.argv)
app = MainUIClass()
app.show()
#a.exec_()
cur.close()
conn.close()
sys.exit(a.exec_())
You are closing the database connection before Qt's main loop is started, move all Postgres related stuff into the thread.
class ThreadClass(QtCore.QThread):
updateTable = pyqtSignal(dict)
def __init__(self, parent=None):
super().__init__()
def run(self):
conn = psycopg2.connect("dbname=XXX user=YYYY password=ZZZ host=localhost port=5555")
cur = conn.cursor()
cur.execute("select * from events")
orderbook = cur.fetchall()
for rowNum, rowData in enumerate(orderbook):
self.updateTable.emit({"insertRow": 0})
for colNum, data in enumerate(rowData):
self.updateTable.emit({"setItem": [rowNum, colNum, data]})
cur.close()
conn.close()