Created
December 15, 2021 11:33
-
-
Save aboellinger/1e80f97afa299a6dc7c9c167b8ab1592 to your computer and use it in GitHub Desktop.
Minimal Sqlite display w/ PySide2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Minimal proof of concept for database content display with Qt | |
Dataset available at: https://github.com/lerocha/chinook-database | |
""" | |
import sys | |
from qtpy import QtWidgets, QtSql | |
from qtpy.QtCore import Qt | |
if __name__ == "__main__": | |
""" | |
UI tests | |
""" | |
app = QtWidgets.QApplication(sys.argv) | |
# Connect to the existing database | |
db = QtSql.QSqlDatabase.addDatabase("QSQLITE") | |
db.setDatabaseName('U:/samples/chinook.sqlite') | |
ok = db.open() | |
# This is just for debug purpose | |
q = QtSql.QSqlQuery("SELECT * FROM 'Track' LIMIT 1") | |
while q.next(): | |
print(q.record()) | |
# Create and set up the | |
model = QtSql.QSqlRelationalTableModel(db=db) | |
model.setTable("Track") | |
album_idx =model.fieldIndex("AlbumId") | |
# Set the relations to the other database tables: | |
model.setRelation(album_idx, QtSql.QSqlRelation("Album", "AlbumId", "Title")) | |
# Set the localized header captions: | |
model.setHeaderData(model.fieldIndex("Name"), Qt.Horizontal, "Name*") | |
model.setHeaderData(album_idx, Qt.Horizontal, "Album Title*") | |
model.select() | |
view = QtWidgets.QTableView() | |
view.setModel(model) | |
view.setItemDelegate(QtSql.QSqlRelationalDelegate(view)) | |
view.show() | |
returncode = app.exec_() | |
sys.exit(returncode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment