Using PyQt4 - QTableView with SQLAlchemy using QSqlTableModel (or not)

I am starting to learn Qt for python and, as I was interested after reading this post: qt-pyqt QTableView does not populate when changing databases. If there was a way to use SQLAlchemy sessions instead of (re) opening a database connection as a table model with a Qt QTableView widget.

Something that will work something like this:

databasePath = "base.sqlite" # used for production

engine = create_engine('sqlite:///' + databasePath, echo=True)

# initializing session :
Session = sessionmaker(bind=engine)
session = Session()

# Set up the user interface from Designer.
self.setupUi(self)

self.model = QSqlTableModel(self)
self.model.setTable("records")

self.model.setSort(FILEORDER, Qt.AscendingOrder)

self.model.setHeaderData(ID, Qt.Horizontal, QVariant("ID"))
self.model.setHeaderData(NAME, Qt.Horizontal, QVariant("Name"))
self.model.select()

self.tableView.setModel(self.model)

Any help would be greatly appreciated, as well as new ways to think about this issue.

thank

+4
source share
1 answer

Take a look at Camelot . He does a lot more :)

, , Q * View Q * Model, SqlAlchemy. , , , , QSqlRelationalTableModel.

+4

All Articles