【问题标题】:PyQt4->PyQt5 translationPyQt4->PyQt5 翻译
【发布时间】:2017-04-05 12:48:56
【问题描述】:

我对表格数据的分页显示感兴趣。我找到了这个链接: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)。 我不知道这是由于我的翻译还是代码错误。任何的想法?谢谢

【问题讨论】:

    标签: python python-3.x pyqt4 pyqt5


    【解决方案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'
    

    将其包含在问题中可能会很有用

    【讨论】:

    • 非常感谢。我想知道为什么我没有得到例外!我将 Spyder 与 Python 3.4 (Winpython) 一起使用,并且代码没有显示任何问题。它只是堆叠。除了一切都清楚,但没有它很难说。现在的问题可能是如何绕过代码检查?
    猜你喜欢
    • 2022-10-13
    • 2018-06-13
    • 2018-03-15
    • 2017-11-09
    • 2015-09-06
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多