PDA

View Full Version : PyQT Database etc



peterjb31
24th March 2008, 17:48
Hi. I am using PyQT to create a database system as a part of a uni project based around a bakery. I am using the QT database access libraries as opposed to the built in python ones.

What I want to do is load values from one of the fields in the database as a list in a QListWidget so then when each value is click appropriate data can be placed into fields to the right of it.

Any help would be hugely appreciated.

jpn
24th March 2008, 20:17
Sorry, but what do you expect from us? What did YOU try so far? Have you seen the examples shipped with Qt/PyQt?

peterjb31
24th March 2008, 20:23
I haven't tried much to be honest. I was after ideas more than an implementation. From what I understand of QListWidget it works like an array, in which case the best idea i have so far is listing all values within the field and then adding them to the QListWidget. Then to find details on a specific bit doing a search through the database for that name, however this seems rather wasteful I was really after better understanding as to how QLIstWidget works.

jpn
24th March 2008, 20:45
Well, an SQL model + view is much better choice for showing data from database...

QSqlQueryModel
QSqlTableModel
QListView

peterjb31
27th March 2008, 11:38
Thanks, that makes a lot more sense that what I was trying to do. I am however still struggling to work out how to implement this. I read some documentation on QListView and the QSqlQueryModel here (http://www.riverbankcomputing.com/Docs/PyQt4/html/classes.html).

I don't understand how I create the model and then reference it. If anyone could either point me to an example of the code used (can't seem to find any for pyqt when I google) or alternatively post an example I would be extremely greatful.

peterjb31
2nd April 2008, 11:40
I have created a QSqlModel using the following code in Python:


self.model = QSqlQueryModel
self.model.setQuery("SELECT companyName FROM suppliers");

However when I try to run the code I get the following error:


TypeError: first argument of unbound method QSqlQueryModel.setQuery() must be a QSqlQueryModel instance


Would anyone be able to advise as to what the error is. Have I declared the QueryModel properly, if not how do I?

jpn
2nd April 2008, 13:28
Excuse me, I'm not a Python expert but shouldn't it be:

self.model = QSqlQueryModel()
?