关于python:PyQt4-> PyQt5翻译

PyQt4->PyQt5 translation

我对表数据的分页显示很感兴趣。我找到了这个链接:https://sateshkumarb.wordpress.com/2012/04/01/paginated-display-of-table-data-in-pyqt/用Pyqt4编写的有趣代码。我试图用pyqt5在python 3.4上翻译它。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)是在QtWidgets.QVariant筹集AttributeErrorQVariant是因为在QtCoreQtWidgets包装困难。

2)在pyqt5你不需要使用明确的QVariant,所以你可以完全删除它们。

3)在PersonTableModel.data()person是一个字符串,和person.nameperson.city想和高层的错误。

在一个固定的版本的PersonTableModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)

备注

异常:这是提高您的代码

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

它可以包括在已经是这样的问题。