Debugging PyQt5 module
Rich Shepard
rshepard at appl-ecosys.com
Thu May 27 18:05:33 BST 2021
I'm writing a business tracking application for my own use. I've used the
database with the psql shell for several years and decided it's the
application for me to learn PyQt5.
The application has three lookup tables and I'm using them to learn the
model/view pattern and SQL access.
The actitytypes table used in the attached code has these values:
Phone
Email
Fax
Meeting
Conference
Referral
Called me
Other
When I run the module (python3-3.7.2) a blank window displays; no content at
all. I need to learn why.
How do I debug (as distinct from a unittest) a PyQt5 application?
TIA,
Rich
-------------- next part --------------
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
from PyQt5 import QtSql as qts
class MainWindow(qtw.QMainWindow):
def __init__(self):
super().__init__()
# Set up database connection
self.db = qts.QSqlDatabase.addDatabase('QPSQL')
self.db.setDatabaseName('bustrac')
if not self.db.open():
error = self.db.lastError().text()
qtw.QMessageBox.critical(
None, 'DB Connection Error',
'Could not open database file: '
f'{error}')
sys.exit(1)
tables = set('activities','activitytypes','industrytypes',
'locations','organizations','organizations_org_nbr_seq',
'people','people_person_nbr_seq','projects','statustypes')
# Check that all database tables exist
required_tables = {'activities','activitytypes','industrytypes',
'locations','organizations','organizations_org_nbr_seq',
'people','people_person_nbr_seq','projects','statustypes'}
tables = self.db.tables()
missing_tables = required_tables - set(tables)
if missing_tables:
qtw.QMessageBox.critica(
None, 'DB Integrity Error'
'Missing tables, please repair DB: '
f'{missing_tables}')
sys.exit(1)
# Model/View here.
self.table = qtw.QTableView()
self.model = qts.QSqlTableModel(db=self.db) # for single table
self.table.setModel(self.model)
self.model.setTable('activitytypes')
self.model.select()
self.setFixedSize(800, 600)
self.setCentralWidget(self.table)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = MainWindow()
window.show()
#sys.exit(app.exec())
app.exec_()
More information about the PyQt
mailing list