Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Connecting MySql Database to PySide/QT QTableView?
I've been racking my brain trying to figure this out and think I am missing something fundamental.
I have a UI made with QT Designer that I'm trying to connect to a MySQL database with PySide. I can already populate the database with Python, but can't figure out how to hook the QTableView object (name 'chart_tableview') into my database ('testdb').
How do I connect that to the Python script and how can I populate it with the database cells?
Hi and welcome to devnet,
Did you take a look at the "QtSql module":http://qt-project.org/doc/qt-4.8/qtsql.html ?
I have tried a few examples I could find online using QtSql, but couldn't get it to work right within Python. I'm new to using PySide & QT, so I feel like I'm missing something that's easy, but I couldn't find anything online that worked right...
Going through the QSQLDatabase page, it appears that to connect to the database with QT I'd use
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
bool ok = db.open();
how would I then link the QListView to a SQL query in that database?
Did you take a look at the module examples ?
Also check QSqlTableModel
Those two pages helped A LOT!
I haven't gotten it quite working, but I feel like I'm close. I put the following into the script:
@db = QtSql.QSqlDatabase.addDatabase("QMYSQL");
model = QtSql.QSqlTableModel()
model.removeColumn(0) # don't show the ID
model.setHeaderData(0, QtCore.Qt.Horizontal, "Header 1")
model.setHeaderData(1, QtCore.Qt.Horizontal, "Header 2")
view = QTableView()
I've gotten it down to the last error I believe:
@ model.setHeaderData(0, QtCore.Qt.Horizontal, "Name")
TypeError: 'PySide.QtSql.QSqlQueryModel.setHeaderData' called with wrong argument types:
PySide.QtSql.QSqlQueryModel.setHeaderData(int, Orientation, str)
PySide.QtSql.QSqlQueryModel.setHeaderData(int, PySide.QtCore.Qt.Orientation, QVariant, int = Qt.EditRole)@
If I comment out the setHeaderData lines, nothing populates in the desired QTableView (or anywhere for that matter). I'm still figuring out where to put the 'chart_listview' object name in the code.
Thank you for steering me in the right direction!
You should hide column 0 on the view, removing it in the model will remove the content of the database.
It seems that your string is not converted, either add the tr() call (it's always good to have your code ready for translation) or QLatin1String()
I couldn't find a PySide hideColumn() equivalent for QSqlTableModel, only for vanilla QT-- is it named something else? Either way, I've commented it out for now until I can get the db hooked into the GUI.
I got the error resolved from my last post-- so it's down to connecting the QSqlTableModel to the QT Designer file. It looks like to do that I'll need to use the findChild() function, correct?
I'll toil for a bit before I ask for more help-- either way I'll post what I figure out here.
It's not on the model, it's on the QTableView.
You have to code the connection yourself e.g. in your widget constructor