I got a toy phonebook application used to learn PySide:
import sys
import os
from PySide import QtCore, QtGui, QtSql
import phonebook_ui
CURRENT_FILE = os.path.abspath(__file__)
CURRENT_DIR = os.path.dirname(CURRENT_FILE)
DB_PATH = os.path.join(CURRENT_DIR, 'db.sqlite')
class PhoneBook(QtGui.QMainWindow, phonebook_ui.Ui_MainWindow):
def __init__(self):
super(PhoneBook, self).__init__()
self.setupUi(self)
self.db = self.create_connection()
# this is where the two tables should me linked automatically
self.model = QtSql.QSqlRelationalTableModel(self)
self.model.setTable('person')
self.model.setEditStrategy(QtSql.QSqlRelationalTableModel.OnManualSubmit)
foreign_key = self.model.fieldIndex('service')
self.model.setRelation(foreign_key,
QtSql.QSqlRelation("service", "id", "nom"))
self.model.select()
self.relation_model = self.model.relationModel(foreign_key)
self.edit_service.setModel(self.relation_model)
self.edit_service.setModelColumn(self.relation_model.fieldIndex("name"))
self.listing.setModel(self.model)
self.listing.setColumnHidden(0, True)
self.listing.setSelectionMode(QtGui.QAbstractItemView.SelectionMode.SingleSelection)
# this is where the model data should be automatically injected in the
# central table
self.mapper = QtGui.QDataWidgetMapper(self)
self.mapper.setModel(self.model)
self.mapper.setItemDelegate(QtSql.QSqlRelationalDelegate(self))
self.mapper.addMapping(self.edit_name, self.model.fieldIndex('name'))
self.mapper.addMapping(self.edit_phone, self.model.fieldIndex('phone'))
self.mapper.addMapping(self.edit_service, foreign_key)
self.listing.selectionChanged = self.on_selection_changed
def on_selection_changed(self, selected, deselected):
range = selected[0]
index = range.indexes()[0]
self.mapper.setCurrentIndex(index.row())
@classmethod
def create_connection(cls, bdd=DB_PATH):
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(bdd)
db.open()
query = QtSql.QSqlQuery()
query.exec_(u"create table person(id int primary key, name varchar(20), "
u"phone varchar(20), service varchar(20))")
db.commit()
return db
def close(self):
super(PhoneBook, self).close()
self.db.close()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
phonebook = PhoneBook()
phonebook.show()
sys.exit(app.exec_())
Here is a screenshot:

My tables look like this:

I managed to make it work once with no service field, but now that I added it, I introduced the notion of QSqlRelationalTableModel and failed to make it work.
What’s it’s supposed to do:
- fill the big white central table with a listing of people from the DB (fails, despite the model and mapper being setup and the db containing 2 entries)
- on click on any of the central table rows, fill the bottom form with the row data
- allow edit and save.
The DB is filled up with data and is readable from my tests in the python shell.
The code throw no errors, but nothing happens: the windows is displayed with nothing in there.
Can you point my mistakes here ?
I’ll be happy to provide any additional information you need
In code you have
service varchar(20)), but in screenshot, service is a separate table (as it should be). But I seenominsteadnameon screenshot (QtSql.QSqlRelation("service", "id", "name")).So, if you’ll check your database schema and actualize it with code, it should work.
P.S. I have not checked “edit” and “add” logic.