PyQt5: specifying QSqlTableModel

Maurizio Berti maurizio.berti at gmail.com
Thu Jun 3 16:02:59 BST 2021


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.

File "activitytypes.py", line 27, in __init__
>      self.model = qts.QSqlTableModel(dbs.db)
> AttributeError: type object 'DBSetup' has no attribute 'db'
>

The "type object" error is when trying to access a class (a type); an
instance would have returned "AttributeError: 'DBSetup' object has no
attribute 'db'".
So, I'd say there are at least two issues:

1. you're importing the DBSetup class, but you're not creating an instance
(*);
2. the first argument of the QSqlTableModel constructor is the parent, and
unless you use the db keyword, you'll get another exception, since
QSqlDatabase is not a valid parent (it doesn't inherit from QObject);
consider that if you don't use multiple database connections, the database
argument is unnecessary, as Qt will always use the default connection if
not specified.

Maurizio

* be aware that the "imports as" syntax should be used with care: there are
very good reasons for which classes and variables use different letter
casings, and if you import a class changing it to a lowercase name you
might get easily confused, thinking that an object is an instance, while
it's not. Also always consider the pros and cons of using such renamings:
saving a few characters isn't always a major benefit, especially if you end
up having 4 modules that have almost the same names: qtw, qtg, qtc and qts.
Readability should *never* be underestimated.

-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210603/79d053a6/attachment.htm>


More information about the PyQt mailing list