[PyQt] QSqlRelationalModel and setFilter

Simone simozack at yahoo.it
Wed Feb 27 16:56:35 GMT 2008


I'm experimenting problem with QSqlRelationalModel and the setFilter method.

I don't speak (write) English very well, so I made this snippet that 
shows the problem.

The database has 3 tables ('tipo_documento', 'tipi' and 'causale'). The 
table "tipo_documento" has the foreign key reference to the table 'tipi' 
and 'causale' on their id field (many to one relationship with both the 
table "tipi" and the table "causale").

from PyQt4.QtSql import *
from PyQt4.QtCore import *

db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db')
db.open()
model = QSqlRelationalTableModel()

model.setTable("tipo_documento")
model.setRelation(2, QSqlRelation("causale", 'id', 'descrizione'))
model.setRelation(3, QSqlRelation("tipi", 'id', 'descrizione'))
model.setFilter(QString('descrizione like "%1"').arg('%'))
model.reset()
model.select()
print model.rowCount()

The result of the last print is '0'. But if I comment the rows 
containing model.setRelation(), the result of this query is 1 as 
expected. Note that the filter is in a field that hasn't any external 
relations with other table.
The relations, also, are right because in the rest of the program I'm 
writing, I have a QTableView that shows correctly the description of the 
foreing key.

I'm using Python 2.5.2 (but this happens also with 2.5.1), PyQt4 version 
4.3.3 on WinXp SP2.

Thank you very much,
Simone
Chiacchiera con i tuoi amici in tempo reale! 
 http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com 



More information about the PyQt mailing list