0

我得到了以下应用程序:

# -*- coding: utf8 -*-

import sys
import os
import signal

from PySide import 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()

        self.model = QtSql.QSqlRelationalTableModel(self)
        self.model.setTable('person')
        self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)

        service_id_column = self.model.fieldIndex('service_id')
        relation = QtSql.QSqlRelation("service", "id", "name")
        self.model.setRelation(service_id_column, relation)

        self.model.select()

        self.listing.setModel(self.model)

        self.listing.clicked.connect(self.on_listing_clicked)

        self.relation_model = self.model.relationModel(service_id_column)
        self.edit_service.setModel(self.relation_model)
        self.edit_service.setModelColumn(self.relation_model.fieldIndex("name"))

        self.mapper = QtGui.QDataWidgetMapper(self)
        self.mapper.setModel(self.model)

        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, service_id_column)

        self.edit_save.clicked.connect(self.on_edit_save_clicked)


    def on_edit_save_clicked(self):
        self.model.database().transaction()
        if self.model.submitAll():
            self.model.database().commit()
        else:
            self.model.database().rollback()
            QtGui.QMessageBox.warning(self, "Cached Table",
                        "The database reported an error: %s" % self.model.lastError().text())


    def on_listing_clicked(self, index):
        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 AUTOINCREMENT NOT NULL,
                                name VARCHAR(20),
                                phone VARCHAR(20),
                                service_id INT)
        """)
        query.exec_(u"""
            CREATE TABLE service (id INT PRIMARY KEY AUTOINCREMENT NOT NULL,
                                name VARCHAR(20))
        """)

        db.commit()

        return db


    def close(self):
        super(PhoneBook, self).close()
        self.db.close()


if __name__ == '__main__':

    signal.signal(signal.SIGINT, signal.SIG_DFL)

    app = QtGui.QApplication(sys.argv)

    phonebook = PhoneBook()
    phonebook.show()

    try:
        sys.exit(app.exec_())
    finally:
        phonebook.close()

它看起来像这样:

该应用程序的屏幕截图

当我单击edit_save按钮时,来自 的数据更改edit_*会反映在listingQtTreeView 中。但数据并未保存在数据库中。

我尝试了各种版本的on_edit_save_clicked不走运。

4

0 回答 0