本文介绍了Python 3.5.1:QVariant 表示映射类型且无法实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 3.5.1,我正在尝试运行此代码,但 QVariant 有问题

I'm working with Python 3.5.1 and I'm trying to run this code but I have a problem with QVariant

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class Model(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']

    def rowCount(self, parent=QModelIndex()):
        return len(self.items)
    def columnCount(self, parent=QModelIndex()):
        return 1

    def data(self, index, role):
        if not index.isValid(): return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()

        row=index.row()
        if row<len(self.items):
            return QVariant(self.items[row])
        else:
            return QVariant()

class Proxy(QSortFilterProxyModel):
    def __init__(self):
        super(Proxy, self).__init__()
        self.filterActive = False

    def setView(self, view):
        self._view = view

    def filterAcceptsRow(self, row, parent):
        if self.filterActive and '_B_' in self.sourceModel().data(self.sourceModel().index(row, 0), Qt.DisplayRole).toPyObject():
        self._view.selectRow(row)
        return True

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        tableModel=Model(self)

        proxyModel=Proxy()
        proxyModel.setSourceModel(tableModel)

        self.tableview=QTableView(self)
        self.tableview.setModel(proxyModel)
        self.tableview.horizontalHeader().setStretchLastSection(True)
        self.tableview.setSelectionMode(QAbstractItemView.MultiSelection)

        proxyModel.setView(self.tableview)

        button=QPushButton(self)
        button.setText('Select Items with B')
        button.clicked.connect(self.clicked)

        layout = QVBoxLayout(self)
        layout.addWidget(self.tableview)
        layout.addWidget(button)
        self.setLayout(layout)

    def clicked(self, arg):
        proxyModel=self.tableview.model()
        self.tableview.clearSelection()
        proxyModel.filterActive = True
        proxyModel.invalidateFilter()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

确实,当我执行此代码时,会出现一条错误消息: QVariant 代表映射类型,无法实例化.我试图删除 QVariant 并将 None 放置到位

Indeed, when I execute this code an error message appears: QVariant Represents a mapped kind and can not be instantiated .I tried to remove the QVariant and putting None inplace

def data(self, index, role):
    if not index.isValid(): return None
    elif role != Qt.DisplayRole:
        return None

    row=index.row()
    if row<len(self.items):
        return (self.items[row])
    else:
        return None

但是出现了一条新的错误消息:'str' 对象没有属性 'toPyObject'.

But a new error message appears: 'str' object has no attribute 'toPyObject'.

谢谢你帮助我

推荐答案

正如您所发现的,默认情况下,QVariant 在与 Python3 一起使用时在 PyQt4 中不存在.相反,所有内容都会自动转换为等效的 Python 类型.因此,您需要做的就是在通常需要 QVariant.null 的地方使用 None,并避免尝试使用任何 QVariant 方法(例如toPyObject,这显然是多余的.

As you've found out, by default, QVariant does not exist in PyQt4 when used with Python3. Instead, everything is automatically converted to an equivalent python type. So all you need to do is use None wherever QVariant.null would normally be expected, and avoid attempting to use any QVariant methods (such as toPyObject, which is obviously redundant anyway).

这篇关于Python 3.5.1:QVariant 表示映射类型且无法实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 03:31