PyQt5: specifying QSqlTableModel
Rich Shepard
rshepard at appl-ecosys.com
Thu Jun 3 18:26:54 BST 2021
On Thu, 3 Jun 2021, Maurizio Berti wrote:
> The database_setup.py is incomplete, so it's a bit difficult to be sure
> here, but, going on a hunch and given the error, "dbs" is the class, while
> db is an *instance* attribute.
Maurizio,
Yes, I find myself wrapped around the axle here as it's all new to me.
Taking your advice I've modified both datasource.py and activitytypes.py and
I'm back to having a blank window opened but not having the pdb prompt
returned:
$ python activitytypes.py
> /home/rshepard/development/business_tracker/activitytypes.py(7)<module>()
-> from PyQt5 import QtWidgets as qtw
(Pdb) c
> /home/rshepard/development/business_tracker/datasource.py(7)<module>()
-> from PyQt5 import QtWidgets as qtw
(Pdb) c
> /home/rshepard/development/business_tracker/datasource.py(19)__init__()
-> if not db.open():
(Pdb) c
> /home/rshepard/development/business_tracker/activitytypes.py(32)__init__()
-> self.table = qtw.QTableView()
(Pdb) c
The revised files are attached.
Thanks,
Rich
-------------- next part --------------
import sys
import logging
# bug fix:
from pdb import Pdb; Pdb(skip=['importlib*']).set_trace()
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtSql as qts
class DBSetup(qts.QSqlDatabase):
""" Set up database connection """
def __init__(self, *args, **kwargs):
super().__init__()
db = qts.QSqlDatabase.addDatabase('QPSQL')
db.setDatabaseName('bustrac')
logging.info('found database')
breakpoint()
if not db.open():
error = 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.critical(
None, 'DB Integrity Error'
'Missing tables, please repair DB: '
f'{missing_tables}')
sys.exit(1)
-------------- next part --------------
import sys
import logging
# bug fix:
from pdb import Pdb; Pdb(skip=['importlib*']).set_trace()
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc
from PyQt5 import QtSql as qts
from datasource import DBSetup
logging.basicConfig(level=logging.DEBUG, filename='activitytypes.log')
#logging.debug('running DBSetup')
#dbs.db
class ATWindow(qtw.QMainWindow):
def __init__(self):
super().__init__()
# Model/View here.
act_tbl = DBSetup()
logging.debug('Defining model/view')
self.model = qts.QSqlTableModel()
self.model.setTable('activitytypes')
self.model.select()
breakpoint()
self.table = qtw.QTableView()
self.table.setModel(self.model)
self.setMinimumSize(qtc.QSize(800, 600))
self.setCentralWidget(self.table)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = ATWindow()
window.show()
#sys.exit(app.exec())
app.exec_()
logging.debug("End of Program")
More information about the PyQt
mailing list