I am trying to display some simple info from SQLite database into QTableView. I have followed one answer from SO, and for itself, it is working. When I try to implement the same code into my GUI (just a simple mainwindow with a QTableView object) it shows nothing. Here is the code:
from PyQt4 import QtCore, QtGui
from gui import Ui_MainWindow
from dialog import Ui_Dialog
from PyQt4.QtSql import QSqlQueryModel,QSqlDatabase,QSqlQuery
import sys
class Glavni(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Glavni, self).__init__()
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.show()
#QtCore.QObject.connect(self.ui.actionRegistar, QtCore.SIGNAL("triggered()"), self.popup)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("baza.db")
db.open()
projectModel = QSqlQueryModel()
projectModel.setQuery("select name from people",db)
projectView = QtGui.QTableView()
projectView.setModel(projectModel)
projectView.show()
def popup(self):
dialog = QtGui.QDialog()
dialog.show()
class Dialog(QtGui.QDialog):
def __init__(self,parent=None):
super(Dialog,self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
prozor = Glavni()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
What am I doing wrong? QListView's name in QT Designer is lista, if that is relavant. Thank you.
projectView = QtGui.QTableView() #THIS part is wrong if the GUI is designed through Designer
projectView.setModel(projectModel)
projectView.show()
the correct code is similar to this:
projectView = self.ui.myList #or some other name, which is the SAME AS that object name in Designer
projectView.setModel(projectModel)
projectView.show()
and it works ;)