class ScoreModel: def __init__(self, fil: str = None, parent=None): self.model = QSqlRelationalTableModel(parent) self.model.setTable('Score') self.model.setRelation( 9, QSqlRelation('TypeInfo', 'type_id', 'type_name')) self.model.setHeaderData(9, Qt.Horizontal, 'type') if fil is not None: self.model.setFilter(fil) self.model.select() print(self.model)
def match_valasztas(self): matches = QSqlRelationalTableModel(db=db) matches.setTable("match_settings") matches.setSort(8, Qt.DescendingOrder) matches.select() self.merkozesek.clear() for i in range(matches.rowCount()): self.merkozesek.addItem(str(matches.record(i).value(1)) + "\t" + str(matches.record(i).value(2)) + "\t" + str(int(matches.record(i).value(3)) + matches.record(i).value(6)) + "\t" + str(int(matches.record(i).value(3)) + matches.record(i).value(7)) + "\t" + str(matches.record(i).value(4)) + "\t" + str(matches.record(i).value(5)) + "\t" + str(matches.record(i).value(8))[:16] + "\t" + str(matches.record(i).value(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()
class TmplModel: def __init__(self, parent=None): # dirpath = os.path.dirname(os.path.abspath(__file__)) # self.db = QSqlDatabase.addDatabase('QSQLITE') # self.db.setDatabaseName(os.path.join(dirpath, 'grad.db')) # self.db.open() self.model = QSqlRelationalTableModel(parent) self.model.setTable('Template') self.model.setRelation(3, QSqlRelation('Teacher', 'tid', 'name')) self.model.setHeaderData(3, Qt.Horizontal, 'teacher') self.model.setRelation( 4, QSqlRelation('TypeInfo', 'type_id', 'type_name')) self.model.setHeaderData(4, Qt.Horizontal, 'type') self.model.select() def refresh(self): self.model.select()
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.table = QTableView() self.model = QSqlRelationalTableModel(db=db) self.table.setModel(self.model) # tag::setRelation[] self.model.setTable("Track") self.model.setRelation(2, QSqlRelation("Album", "AlbumId", "Title")) self.model.setRelation(3, QSqlRelation("MediaType", "MediaTypeId", "Name")) self.model.setRelation(4, QSqlRelation("Genre", "GenreId", "Name")) self.model.select() # end::setRelation[] self.setMinimumSize(QSize(1024, 600)) self.setCentralWidget(self.table)