Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:28:55+00:00 2026-06-15T23:28:55+00:00

I got a toy phonebook application used to learn PySide: import sys import os

  • 0

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:

Screenshot of phonebook toy app

My tables look like this:

screenshot of the phonebook db tables

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:28:56+00:00Added an answer on June 15, 2026 at 11:28 pm

    In code you have service varchar(20)), but in screenshot, service is a separate table (as it should be). But I see nom instead name on screenshot (QtSql.QSqlRelation("service", "id", "name")).

    So, if you’ll check your database schema and actualize it with code, it should work.

    @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 int)")    
    
        query.exec_(u"create table service(id int primary key, name varchar(20))")  
        db.commit()
        return db  # don't forget to return it, as you use it in close
    

    P.S. I have not checked “edit” and “add” logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this toy code, works fine, using MySQL var r = new SimpleRepository(DB,
Got a seg fault from my memcpy that gdb can't give me anything else
got some problems. Built an applet that has to be used step-by-step. After each
Got a problem with selecting message from topic by message id. Here’s the boiled
Got the following error when I tried to compile a C application in 64-bit
Got a complex SELECT query, from which I would like to insert all rows
I'm learning Swing class now and everything about it. I've got this toy program
Got it from php.net, but I am not sure is this how everybody destroy
Got a question for the font named Rockwell Std Light. I used in Photoshop
I'm trying to set up a toy application (which may turn in to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.