2017-04-05 7 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)で停止します。 これが私の翻訳またはコードエラーによるものかどうかはわかりません。何か案が? QVariantはないQtWidgetsQtCoreパッケージの下にあるので、おかげで

答えて

1

1)QtWidgets.QVariantAttributeErrorを上げています。

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) 

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' 

。なぜ私は例外を取得していないのだろうか?私はPython 3.4(Winpython)でSpyderを使用していますが、コードに問題はありません。それはちょうど積み重なります。例外を除いてはすべてが明確ですが、言い表せません。今、問題はコードチェックに合格することが可能なのでしょうか? – polgia0