here's my starting position:
- I've created an interface file using QtDesigner.
- In the interface file I have a model-based QTableView.
- I'm trying to build a model for that view, following the structure of the official PyQt documentation at https://doc.qt.io/qt-5/qsqltablemodel.html#details The model will be accessing a database.
- Am getting the following error:
Traceback (most recent call last):
File "D:**\main.py", line> 89, in window = MainWindow()File "D:**\main.py", line 28, in init self.set_individual_clients_model()
File "D:**\main.py", line 48, in set_individual_clients_model self.ui.tableView.setModel(self.model)
TypeError: 'PySide2.QtWidgets.QTableView.setModel' called with wrong argument types: PySide2.QtWidgets.QTableView.setModel(QSqlTableModel)
Supported signatures:
PySide2.QtWidgets.QTableView.setModel(PySide2.QtCore.QAbstractItemModel)
When I try to run, I get the above error.
Here's my code:
from ui_interface import *
from PyQt5.QtCore import Qt
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5.QtWidgets import QTableView, QMessageBox
settings = QSettings()
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.model = QSqlTableModel()
loadJsonStyle(self, self.ui)
self.show()
QAppSettings.updateAppSettings(self)
self.set_individual_clients_model()
# INDIVIDUAL CLIENTS PAGE
def set_individual_clients_model(self):
# set up the model
self.model.setTable("ind_clients")
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.model.select()
self.model.setHeaderData(0, Qt.Horizontal, "ID")
self.model.setHeaderData(1, Qt.Horizontal, "First Name")
self.model.setHeaderData(2, Qt.Horizontal, "Last Name")
self.model.setHeaderData(3, Qt.Horizontal, "Date of Birth")
self.model.setHeaderData(4, Qt.Horizontal, "Doc Number")
self.model.setHeaderData(5, Qt.Horizontal, "Doc Type")
self.model.setHeaderData(6, Qt.Horizontal, "Doc Text")
self.model.setHeaderData(7, Qt.Horizontal, "Country")
# set the view
self.ui.tableView.setModel(self.model)
self.ui.tableView.resizeColumnsToContents()
self.ui.tableView.view()
return
def create_connection():
conn = QSqlDatabase.addDatabase("QPSQL")
conn.setDatabaseName("alaric")
user = os.environ.get("DB_USER")
password = os.environ.get("DB_PASSWORD")
if not conn.open(user, password):
QMessageBox.critical(
None,
"Database Error!",
"Database Error: %s" % conn.lastError().databaseText()
)
return False
return True
if __name__ == "__main__":
app = QApplication(sys.argv)
create_connection()
window = MainWindow()
window.show()
sys.exit(app.exec_())
Any help with in successfully connecting the view and the model would be greatly appreciated.