2017-04-05 155 views
1

我對錶格數據的分頁顯示感興趣。我發現這個鏈接:https://sateeshkumarb.wordpress.com/2012/04/01/paginated-display-of-table-data-in-pyqt/ 由PyQt4製作有趣的代碼。我試圖在python 3.4上的PyQt5中翻譯它。代碼如下:PyQt4-> PyQt5翻譯

import sys 
from PyQt5 import QtWidgets, QtCore 

class Person(object): 
    """Name of the person along with his city""" 
    def __init__(self,name,city): 
     self.name = name 
     self.city = city 
class PersonDisplay(QtWidgets.QMainWindow): 
    def __init__(self, parent=None): 
     super(PersonDisplay, self).__init__(parent) 
     #QtWidgets.QMainWindow.__init__(self, parent) 
     self.setWindowTitle('Person City') 
     view = QtWidgets.QTableView() 
     tableData = PersonTableModel() 
     view.setModel(tableData) 
     self.setCentralWidget(view) 
     tableData.addPerson(Person('Ramesh', 'Delhi')) 
     tableData.addPerson(Person('Suresh', 'Chennai')) 
     tableData.addPerson(Person('Kiran', 'Bangalore')) 

class PersonTableModel(QtCore.QAbstractTableModel): 
    def __init__(self): 
     super(PersonTableModel,self).__init__() 
     self.headers = ['Name','City'] 
     self.persons = ['Ramesh', 'Delhi'] 

    def rowCount(self,index=QtCore.QModelIndex()): 
     return len(self.persons) 

    def addPerson(self,person): 
     self.beginResetModel() 
     self.persons.append(person) 
     self.endResetModel() 

    def columnCount(self,index=QtCore.QModelIndex()): 
     return len(self.headers) 

    def data(self,index,role=QtCore.Qt.DisplayRole): 
     col = index.column() 
     person = self.persons[index.row()] 
     if role == QtCore.Qt.DisplayRole: 
      if col == 0: 
       return QtWidgets.QVariant(person.name) 
      elif col == 1: 
       return QtWidgets.QVariant(person.city) 
      return QtWidgets.QVariant() 

    def headerData(self,section,orientation,role=QtCore.Qt.DisplayRole): 
     if role != QtCore.Qt.DisplayRole: 
      return QtWidgets.QVariant() 

     if orientation == QtCore.Qt.Horizontal: 
      return QtWidgets.QVariant(self.headers[section]) 
     return QtWidgets.QVariant(int(section + 1)) 

if __name__ == '__main__': 
    app = QtWidgets.QApplication(sys.argv) 
    appWin = PersonDisplay() 
    appWin.show() 
    sys.exit(app.exec_()) 

這似乎是正確的,但運行停在:view.setModel(tableData)。 我不知道這是由於我的翻譯或代碼錯誤。任何想法?由於

回答

1

1)QtWidgets.QVariant是提高了AttributeError因爲QVariantQtCore包不QtWidgets下。

2)在PyQt5中,你不需要明確地使用QVariant,所以你可以完全刪除它們。

3)PersonTableModel.data()person是一個字符串,person.nameperson.city將上升錯誤。

這裏一個固定的版本的PersonTableModel

class PersonTableModel(QtCore.QAbstractTableModel): 
    def __init__(self): 
     super(PersonTableModel,self).__init__() 
     self.headers = ['Name','City'] 
     self.persons = ['Ramesh', 'Delhi'] 

    def rowCount(self,index=QtCore.QModelIndex()): 
     return len(self.persons) 

    def addPerson(self,person): 
     self.beginResetModel() 
     self.persons.append(person) 
     self.endResetModel() 

    def columnCount(self,index=QtCore.QModelIndex()): 
     return len(self.headers) 

    def data(self,index,role=QtCore.Qt.DisplayRole): 
     col = index.column() 
     person = self.persons[index.row()] 
     if role == QtCore.Qt.DisplayRole: 
      if col == 0: 
       return person 
      elif col == 1: 
       return person 
      return None 

    def headerData(self,section,orientation,role=QtCore.Qt.DisplayRole): 
     if role != QtCore.Qt.DisplayRole: 
      return None 

     if orientation == QtCore.Qt.Horizontal: 
      return self.headers[section] 
     return int(section + 1) 

附:

你的代碼是提高此異常:

Traceback (most recent call last): 
    File "test.py", line 51, in headerData 
    return QtWidgets.QVariant() 
AttributeError: module 'PyQt5.QtWidgets' has no attribute 'QVariant' 

它可能是有用的,包括在這個問題

+0

非常感謝。我想知道爲什麼我沒有得到例外!我用Python 3.4(Winpython)使用Spyder,代碼沒有顯示任何問題。它只是堆疊。除了一切都很清楚,但沒有很難說。現在的問題可能是如何通過代碼檢查來實現? – polgia0