1

我正在尝试将 SQLite 数据库中的一些简单信息显示到 QTableView 中。我遵循了 SO 的一个答案,就其本身而言,它正在工作。当我尝试在我的 GUI 中实现相同的代码(只是一个带有 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,如果这是相关的话。谢谢你。

4

1 回答 1

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

它有效;)

于 2012-07-09T18:09:04.383 回答