2017-04-05 1 views
1

테이블 데이터의 페이지 매김 디스플레이에 관심이 있습니다. 나는이 링크를 찾았습니다 : https://sateeshkumarb.wordpress.com/2012/04/01/paginated-display-of-table-data-in-pyqt/ 흥미로운 코드가 PyQt4에 의해 만들어졌습니다. Python 3.4에서 PyQt5로 번역하려고했습니다. 코드는 다음과 같습니다.PyQt4-> PyQt5 translation

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)에서 중지됩니다. 이것이 번역 또는 코드 오류로 인한 것인지 알 수 없습니다. 어떤 생각? QVariantQtCore 패키지하지 QtWidgets 아래에있는 AttributeError 때문에 감사

답변

1

1)QtWidgets.QVariant이 제기된다.

PyQt5에서은 명시 적으로 QVariant을 사용할 필요가 없으므로 완전히 제거 할 수 있습니다.

3)는PersonTableModel.data()에서 person 오류가 일어날 것 문자열과 person.cityperson.name하고있다. 여기

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) 

P.S.

코드는이 예외를 발생한다 :이 질문에

+0

덕분에 많은 것을 포함하는 것이 유용 수 있었다

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

. 나는 왜 내가 예외를 얻지 않았는지 궁금해! 파이어 3.4 (Winpython)와 스파이더를 사용하고 코드는 어떤 문제도 보여주지 않습니다. 그냥 겹쳐 쌓여. 예외를 제외하고는 모든 것이 명확하지만 말하기가 어렵습니다. 이제 코드 검사를 통과하는 것이 어떻게 가능할까요? – polgia0