PyQT6, SQL-Light - show table data in QTableView - what am I missing?
-
wrote on 1 Sept 2022, 10:32 last edited by
Hello,
Python 3.9.1
windows 11,
PyQt6I created a database and a table using SQLiteStudio.
The following Python code successfully connects to the database:
import sys from PyQt6 import QtGui, QtWidgets from PyQt6.QtGui import QGuiApplication, QIcon from PyQt6.QtWidgets import QWidget, QApplication, QLabel, QComboBox, QLineEdit, QPushButton, QFormLayout, QTableWidget, QTableView from PyQt6.QtCore import * from PyQt6.QtSql import QSql, QSqlDatabase, QSqlRelation, QSqlRelationalTableModel import csv class FensterKlasse(QWidget): def __init__(self): super().__init__() self.table_model = QSqlRelationalTableModel(self) self.table_model.setTable("telefon_verwaltung") self.table_model.setRelation(1, QSqlRelation('vorname', 'nachname', 'telefonnummer')) self.GUI() def GUI(self): self.setWindowTitle("PyQt6 Telefonnummer-Verwaltung mit Datenbank") qtRectangle = self.frameGeometry() centerPoint = QtGui.QGuiApplication.primaryScreen().availableGeometry().center() qtRectangle.moveCenter(centerPoint) self.move(qtRectangle.topLeft()) self.tabellengrid = QTableView() self.tabellengrid.setModel(self.table_model) formLayout = QFormLayout() formLayout.addRow(self.tabellengrid) self.setLayout(formLayout) def programm_beeden(self): QApplication.instance().quit() db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName("telefonnummern_verwaltung.db") app = QApplication([]) app.setStyleSheet(""" QPushButton:focus { background-color: yellow } QLineEdit:focus { background-color: yellow } QComboBox:focus { background-color: yellow } """) fenster = FensterKlasse() if __name__ == '__main__': fenster.show() sys.exit(app.exec())
My problem: The table is displayed but the data in the table is not. What is still missing?
-
Hello,
Python 3.9.1
windows 11,
PyQt6I created a database and a table using SQLiteStudio.
The following Python code successfully connects to the database:
import sys from PyQt6 import QtGui, QtWidgets from PyQt6.QtGui import QGuiApplication, QIcon from PyQt6.QtWidgets import QWidget, QApplication, QLabel, QComboBox, QLineEdit, QPushButton, QFormLayout, QTableWidget, QTableView from PyQt6.QtCore import * from PyQt6.QtSql import QSql, QSqlDatabase, QSqlRelation, QSqlRelationalTableModel import csv class FensterKlasse(QWidget): def __init__(self): super().__init__() self.table_model = QSqlRelationalTableModel(self) self.table_model.setTable("telefon_verwaltung") self.table_model.setRelation(1, QSqlRelation('vorname', 'nachname', 'telefonnummer')) self.GUI() def GUI(self): self.setWindowTitle("PyQt6 Telefonnummer-Verwaltung mit Datenbank") qtRectangle = self.frameGeometry() centerPoint = QtGui.QGuiApplication.primaryScreen().availableGeometry().center() qtRectangle.moveCenter(centerPoint) self.move(qtRectangle.topLeft()) self.tabellengrid = QTableView() self.tabellengrid.setModel(self.table_model) formLayout = QFormLayout() formLayout.addRow(self.tabellengrid) self.setLayout(formLayout) def programm_beeden(self): QApplication.instance().quit() db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName("telefonnummern_verwaltung.db") app = QApplication([]) app.setStyleSheet(""" QPushButton:focus { background-color: yellow } QLineEdit:focus { background-color: yellow } QComboBox:focus { background-color: yellow } """) fenster = FensterKlasse() if __name__ == '__main__': fenster.show() sys.exit(app.exec())
My problem: The table is displayed but the data in the table is not. What is still missing?
wrote on 1 Sept 2022, 11:30 last edited by@PythonQTMarlem
Start by getting it working with aQSqlTableModel
for either of the two tables (test for both). Then change to theQSqlRelationalTableModel
, if you have anything wrong there it could lead to no rows.On a separate matter: I would create the
QApplication
before theQSqlDatabase
, and you are not supposed to have a permanent/globaldb = QSqlDatabase.addDatabase(...)
left around, see https://doc.qt.io/qtforpython/PySide6/QtSql/QSqlDatabase.html#detailed-descriptionWarning
It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase , it should be accessed with database() . If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.
-
wrote on 1 Sept 2022, 11:47 last edited by
Thank You. I start to learn with a Youtube-Video.
Do you know a good guide to learn Python Qt and SQL Light? -
wrote on 1 Sept 2022, 12:06 last edited by
It works:
self.table_model = QSqlTableModel() self.table_model.setTable("telefon_verwaltung") self.table_model.select() self.GUI()
The select method was missing.
-
It works:
self.table_model = QSqlTableModel() self.table_model.setTable("telefon_verwaltung") self.table_model.select() self.GUI()
The select method was missing.
wrote on 1 Sept 2022, 13:02 last edited by@PythonQTMarlem
Yes, I must admit I thought I recalledsetTable()
did aselect()
, but per https://doc.qt.io/qt-6/qsqltablemodel.html#setTableDoes not select data from the table, but fetches its field information.
To populate the model with the table's data, call select().
-
wrote on 2 Sept 2022, 04:32 last edited by
okay!
Thank you for your post!
1/6