How to display data from psql database in my GUI made in PyQt5?

时间:2019-01-09 21:41:47

标签: python-3.x pyqt desktop-application psql

I have watched some tutorials how to make desktop app in python with PSQL database.

Now i know how to create GUI in PyQt5 but I dont know how to connect data from PSQL database to my GUI.

For example I want to show data from table in QTableWidget but I cant find right answers on internet.

from PyQt5 import QtWidgets, QtSql, QtCore
from PyQt5.QtWidgets import QTableView
from mydesign import Ui_MainWindow
import sys
def loadData(self):
     db = QtSql.QSqlDatabase.addDatabase("QPSQL")
     db.setHostName('localhost') 
     db.setPort(5432) 
     db.setDatabaseName('tbp_baza') 
     db.setUserName('postgres') 
     db.setPassword('admin123')
     dbliste = QtSql.QSqlDatabase.drivers() 

     print(dbliste)

     if not db.open():
        QtWidgets.QMessageBox.critical(None, "Cannot open database",
                             "Unable to establish a database connection.\n"

                             "Click Cancel to exit.", QtWidgets.QMessageBox.Cancel)
        return False

class mywindow(QtWidgets.QMainWindow):

def __init__(self):

    super(mywindow, self).__init__()
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    model =  QtSql.QSqlQueryModel()
    model.setQuery("SELECT ime,prezime FROM korisnica")
    model.setHeaderData(0, QtCore.Qt.Horizontal, "Ime")
    model.setHeaderData(1, QtCore.Qt.Horizontal, "Prezime")
    view = QtWidgets.QTableView()
    view.setModel(model)
    view.show()

app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())

mydesign.py

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(570, 350, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 0, 47, 13))
        self.label.setObjectName("label")
        self.tableView = QtWidgets.QTableView(self.centralwidget)
        self.tableView.setGeometry(QtCore.QRect(20, 30, 256, 192))
        self.tableView.setObjectName("tableView")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        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.pushButton.setText(_translate("MainWindow", "PushButton"))
        self.label.setText(_translate("MainWindow", "TextLabel"))

0 个答案:

没有答案