Example #1
0
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

        #Initialize db
        createdb.init_db()

        model = QSqlRelationalTableModel(self.bookTable)
        model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        model.setTable("books")

        # Remember the indexes of the columns:
        author_idx = model.fieldIndex("author")
        genre_idx = model.fieldIndex("genre")

        # Set the relations to the other database tables:
        model.setRelation(author_idx, QSqlRelation("authors", "id", "name"))
        model.setRelation(genre_idx, QSqlRelation("genres", "id", "name"))

        # Set the localized header captions:
        model.setHeaderData(author_idx, Qt.Horizontal, self.tr("Author Name"))
        model.setHeaderData(genre_idx, Qt.Horizontal, self.tr("Genre"))
        model.setHeaderData(model.fieldIndex("title"), Qt.Horizontal, self.tr("Title"))
        model.setHeaderData(model.fieldIndex("year"), Qt.Horizontal, self.tr("Year"))
        model.setHeaderData(model.fieldIndex("rating"), Qt.Horizontal, self.tr("Rating"))

        if not model.select():
            print(model.lastError())

        # Set the model and hide the ID column:
        self.bookTable.setModel(model)
        self.bookTable.setItemDelegate(BookDelegate(self.bookTable))
        self.bookTable.setColumnHidden(model.fieldIndex("id"), True)
        self.bookTable.setSelectionMode(QAbstractItemView.SingleSelection)

        # Initialize the Author combo box:
        self.authorEdit.setModel(model.relationModel(author_idx))
        self.authorEdit.setModelColumn(model.relationModel(author_idx).fieldIndex("name"))

        self.genreEdit.setModel(model.relationModel(genre_idx))
        self.genreEdit.setModelColumn(model.relationModel(genre_idx).fieldIndex("name"))

        # Lock and prohibit resizing of the width of the rating column:
        self.bookTable.horizontalHeader().setSectionResizeMode(model.fieldIndex("rating"),
            QHeaderView.ResizeToContents)

        mapper = QDataWidgetMapper(self)
        mapper.setModel(model)
        mapper.setItemDelegate(BookDelegate(self))
        mapper.addMapping(self.titleEdit, model.fieldIndex("title"))
        mapper.addMapping(self.yearEdit, model.fieldIndex("year"))
        mapper.addMapping(self.authorEdit, author_idx)
        mapper.addMapping(self.genreEdit, genre_idx)
        mapper.addMapping(self.ratingEdit, model.fieldIndex("rating"))

        selection_model = self.bookTable.selectionModel()
        selection_model.currentRowChanged.connect(mapper.setCurrentModelIndex)

        self.bookTable.setCurrentIndex(model.index(0, 0))
        self.create_menubar()
Example #2
0
def ConfigureDataMappers(model, mappings, delegate):
    mapper = QDataWidgetMapper(model)
    mapper.setModel(model)
    mapper.setSubmitPolicy(QDataWidgetMapper.AutoSubmit)
    mapper.setItemDelegate(delegate(mapper))
    for mapping in mappings:
        if hasattr(mapping[map_idx.WIDGET], "isCustom"):
            mapping[map_idx.WIDGET].init_db(model.database())
            mapping[map_idx.WIDGET].changed.connect(mapper.submit)
        # if no USER property we should use QByteArray().setRawData("account_id", 10)) here
        mapper.addMapping(mapping[map_idx.WIDGET],
                          model.fieldIndex(mapping[map_idx.DB_NAME]))
        # adjust width of QDateTimeEdits to show full date-time string
        if isinstance(mapping[map_idx.WIDGET], QDateTimeEdit):
            mapping[map_idx.WIDGET].setFixedWidth(mapping[
                map_idx.WIDGET].fontMetrics().width("00/00/0000 00:00:00") *
                                                  1.25)
    return mapper