Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQT6, SQL-Light - show table data in QTableView - what am I missing?
Forum Updated to NodeBB v4.3 + New Features

PyQT6, SQL-Light - show table data in QTableView - what am I missing?

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PythonQTMarlem
    wrote on 1 Sept 2022, 10:32 last edited by
    #1

    Hello,
    Python 3.9.1
    windows 11,
    PyQt6

    I 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?

    J 1 Reply Last reply 1 Sept 2022, 11:30
    0
    • P PythonQTMarlem
      1 Sept 2022, 10:32

      Hello,
      Python 3.9.1
      windows 11,
      PyQt6

      I 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?

      J Offline
      J Offline
      JonB
      wrote on 1 Sept 2022, 11:30 last edited by
      #2

      @PythonQTMarlem
      Start by getting it working with a QSqlTableModel for either of the two tables (test for both). Then change to the QSqlRelationalTableModel, if you have anything wrong there it could lead to no rows.

      On a separate matter: I would create the QApplication before the QSqlDatabase, and you are not supposed to have a permanent/global db = QSqlDatabase.addDatabase(...) left around, see https://doc.qt.io/qtforpython/PySide6/QtSql/QSqlDatabase.html#detailed-description

      Warning

      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.

      1 Reply Last reply
      1
      • P Offline
        P Offline
        PythonQTMarlem
        wrote on 1 Sept 2022, 11:47 last edited by
        #3

        Thank You. I start to learn with a Youtube-Video.
        Do you know a good guide to learn Python Qt and SQL Light?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PythonQTMarlem
          wrote on 1 Sept 2022, 12:06 last edited by
          #4

          It works:

          self.table_model = QSqlTableModel()
                  self.table_model.setTable("telefon_verwaltung")
                  self.table_model.select()
                  self.GUI()
          

          The select method was missing.

          J 1 Reply Last reply 1 Sept 2022, 13:02
          1
          • P PythonQTMarlem
            1 Sept 2022, 12:06

            It works:

            self.table_model = QSqlTableModel()
                    self.table_model.setTable("telefon_verwaltung")
                    self.table_model.select()
                    self.GUI()
            

            The select method was missing.

            J Offline
            J Offline
            JonB
            wrote on 1 Sept 2022, 13:02 last edited by
            #5

            @PythonQTMarlem
            Yes, I must admit I thought I recalled setTable() did a select(), but per https://doc.qt.io/qt-6/qsqltablemodel.html#setTable

            Does not select data from the table, but fetches its field information.

            To populate the model with the table's data, call select().

            1 Reply Last reply
            0
            • P Offline
              P Offline
              PythonQTMarlem
              wrote on 2 Sept 2022, 04:32 last edited by
              #6

              okay!
              Thank you for your post!

              1 Reply Last reply
              0

              1/6

              1 Sept 2022, 10:32

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved