一起使用psycopg2和Qthreads(或只是postgresql和qthreads)并更新GUI

问题描述 投票:0回答:1

DB中的订单总是显示正确的订单,我只想在GUI中不断更新它。我一直试图让这个工作,但当我尝试运行一个cur语句时,应用程序崩溃了。我怀疑我需要一些帮助,不必每次都清除tableelement,但只更新已更改的项目(我假设这是一个聪明的东西,但我不确定)。如果我必须使用Qt的postgresql lib我会,但是你必须帮助我如何运行:)

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_())
python postgresql qt psycopg2 qthread
1个回答
1
投票

您在Qt的主循环启动之前关闭数据库连接,将所有与Postgres相关的内容移动到线程中。

class ThreadClass(QtCore.QThread):
    updateTable = pyqtSignal(dict)

    def __init__(self, parent=None):
        super().__init__()

    def run(self):
        conn = psycopg2.connect("dbname=motocompano_dev user=pg_admin password=pGsql_#--w3N_a0X!s7J-o1U+ 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()
© www.soinside.com 2019 - 2024. All rights reserved.