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

pyqt4, python, sqlalchemy, sqlite

Solution

Take a look at Camelot . It does much more:)

I happily found it when the frustration and angst, generated by Q*View and Q*Model experience forced me to start implementing my own ones on the basis of SqlAlchemy. And it was half implemented, when I found the instrument, that does much more, than I've even dream of, struggling with QSqlRelationalTableModel.

Problem

i'm starting to learn Qt for python and as i was wondering after reading this post : qt - pyqt QTableView not populating when changing databases. if there was a way to use SQLAlchemy sessions instead of (re-)opening a database connection as a Table Model with Qt's QTableView widget. Something that would work a little bit like that : ``` 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 problem. Thank you

Original source