0

I am trying to work with QSql Relational TableModel of QT. I am new to Pyqt and the sql table relationship does not display my database, why is that? I have a db file with a database that has relational tables in the same directory. http://pyqt.sourceforge.net/Docs/PyQt4/qtsql.html

from PyQt4.QtGui import * 
from PyQt4.QtSql import * 
from PyQt4.QtCore import * 

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        db = QSqlDatabase.addDatabase('QSQLITE')
        db.setDatabaseName('Example.db')
        if db.open():
            return True
        else:
            print(db.lastError().text())
            return False

        model = QSqlRelationalTableModel('Example.db')
        model.setTable("Product")
        model.select()
        model.setHeaderData(0, Qt.Horizontal, 'ProductID')

        view = QTableView(self)
        view.setModel(model)
Inthu
  • 1,009
  • 1
  • 8
  • 16

1 Answers1

1

You're returning from initUI before setting up the model and the view, so they're not executed:

    if db.open():
        return True
    else:
        print(db.lastError().text())
        return False
Avaris
  • 35,883
  • 7
  • 81
  • 72
  • Thank you, do you know how to setup the fields? And show up all 4 of my tables? And not just one? – Inthu Jan 06 '15 at 15:32