Inthu
Inthu

Reputation: 1029

How to display table a relational sql table in QT QSqlRelationalTableModel?

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)

Upvotes: 0

Views: 1121

Answers (1)

Avaris
Avaris

Reputation: 36715

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

Upvotes: 1

Related Questions