2012-07-01 72 views
1

我想顯示一些從SQLite數據庫到QTableView的簡單信息。我跟着one answer from SO,而且它本身正在工作。當我嘗試在我的GUI中實現相同的代碼(只是一個帶有QTableView對象的簡單主窗口)時,它什麼也不顯示。這裏是代碼:PyQt4基於QTableView模型

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() 

我在做什麼錯? QT Designer中的QListView的名字是lista,如果是相關的話。謝謝。

+0

好吧,讓我換一種說法; 如何在QT Designer中創建的QList或QTableView中顯示SQLite內容? – ivica

回答

0
projectView = QtGui.QTableView() #THIS part is wrong if the GUI is designed through Designer 
projectView.setModel(projectModel) 
projectView.show() 

正確的代碼與此類似:

projectView = self.ui.myList #or some other name, which is the SAME AS that object name in Designer 
projectView.setModel(projectModel) 
projectView.show() 

和它的作品;)