PyQt4-> PyQt5翻译

时间:2017-04-05 12:48:56

标签: python python-3.x pyqt4 pyqt5

我对表数据的分页显示感兴趣。我找到了这个链接:https://sateeshkumarb.wordpress.com/2012/04/01/paginated-display-of-table-data-in-pyqt/ 使用PyQt4制作的有趣代码。我试图在python 3.4上的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 个答案:

答案 0 :(得分:1)

1) QtWidgets.QVariant正在提出AttributeError,因为QVariant不在QtCore包下{。}}。

PyQt5中的

2)您不需要明确使用QtWidgets,因此您可以完全删除它们。

QVariant中的

3)PersonTableModel.data()是一个字符串,personperson.name会出错。

这是person.city的固定版本:

PersonTableModel

<强> P.S。

您的代码引发了此异常:

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)

在问题

中包含它可能是有用的