pet*_*ter 2 python qt pyqt pyside
我在这里学习了一个例子(由neuronet编写).我尝试在QTableView中使用这个方法,但是当我更改文本时,会产生这样的错误
TypeError:QStandardItem.setText(QString):参数1具有意外类型'QVariant'.
一旦按下"撤消"按钮,错误就会出现
TypeError:'instancemethod'对象未连接
这是我的代码,谢谢你的帮助.
# coding:utf-8
# coding:utf-8
from PyQt4 import QtGui, QtCore
import sys
class CommandTextEdit(QtGui.QUndoCommand):
def __init__(self, table, item, oldText, newText, description):
QtGui.QUndoCommand.__init__(self, description)
self.item = item
self.table = table
self.oldText = oldText
self.newText = newText
def redo(self):
self.item.model().itemDataChanged.disconnect(self.table.itemDataChangedSlot)
self.item.setText(self.newText)
self.item.model().itemDataChanged.connect(self.table.itemDataChangedSlot)
def undo(self):
self.item.model().itemDataChanged.disconnect(self.table.itemDataChangedSlot)
self.item.setText(self.oldText)
self.item.model().itemDataChanged.connect(self.table.itemDataChangedSlot)
class StandardItemModel(QtGui.QStandardItemModel):
itemDataChanged = QtCore.pyqtSignal(object, object, object, object)
class StandardItem(QtGui.QStandardItem):
def setData(self, newValue, role=QtCore.Qt.UserRole + 1):
if role == QtCore.Qt.EditRole:
oldValue = self.data(role)
QtGui.QStandardItem.setData(self, newValue, role)
model = self.model()
#only emit signal if newvalue is different from old
if model is not None and oldValue != newValue:
model.itemDataChanged.emit(self, oldValue, newValue, role)
return True
QtGui.QStandardItem.setData(self, newValue, role)
class undoTable(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent = None)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.view = QtGui.QTableView()
self.model = self.createModel()
self.view.setModel(self.model)
self.undoStack = QtGui.QUndoStack(self)
undoView = QtGui.QUndoView(self.undoStack)
buttonLayout = self.buttonSetup()
mainLayout = QtGui.QHBoxLayout(self)
mainLayout.addWidget(undoView)
mainLayout.addWidget(self.view)
mainLayout.addLayout(buttonLayout)
self.setLayout(mainLayout)
self.makeConnections()
def createModel(self):
model = StandardItemModel(4,4)
for row in range(4):
for column in range(4):
item=StandardItem("(%s,%s)" % (row,column))
model.setItem(row,column,item)
return model
def buttonSetup(self):
self.undoButton = QtGui.QPushButton("Undo")
self.redoButton = QtGui.QPushButton("Redo")
self.quitButton = QtGui.QPushButton("Quit")
buttonLayout = QtGui.QVBoxLayout()
buttonLayout.addStretch()
buttonLayout.addWidget(self.undoButton)
buttonLayout.addWidget(self.redoButton)
buttonLayout.addStretch()
buttonLayout.addWidget(self.quitButton)
return buttonLayout
def itemDataChangedSlot(self, item, oldValue, newValue, role):
if role == QtCore.Qt.EditRole:
command = CommandTextEdit(self, item, oldValue, newValue,
"Text changed from '{0}' to '{1}'".format(oldValue, newValue))
self.undoStack.push(command)
return True
def makeConnections(self):
self.model.itemDataChanged.connect(self.itemDataChangedSlot)
self.quitButton.clicked.connect(self.close)
self.undoButton.clicked.connect(self.undoStack.undo)
self.redoButton.clicked.connect(self.undoStack.redo)
def main():
app = QtGui.QApplication(sys.argv)
newtable = undoTable()
newtable.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
从PyQt文档:
Qt使用QVariant类作为任何C++数据类型的包装器.PyQt4允许任何Python对象被包装为QVariant并像任何其他类型一样传递Qt的元对象系统.
如果可以的话,PyQt4会尝试将Python对象转换为C++等价物,以便可以将QVariant传递给不知道Python对象是什么的其他C++代码.
PyQt4的QVariant API版本2将自动将QVariant转换回正确类型的Python对象.
QVariant API的第1版提供了QVariant.toPyObject()方法,以将QVariant转换回正确类型的Python对象.
如果无法进行转换,这两个版本都会引发Python异常.
我猜你是在Python 2.x上,并且PyQt默认使用QVariant API的版本1.它意味着QVariant不会自动转换为另一种类型(这里是a QString),所以你得到一个TypeError.
那么,三个解决方案:
使用Python 3.x,默认情况下,您将拥有QVariant的第2版
sip在导入PyQt之前更改QVariant的版本
import sip
sip.setapi('QVariant',2)
from PyQt4 import QtGui, QtCore
Run Code Online (Sandbox Code Playgroud)使用QVariant.toPyObject()转换为正确的类型
self.item.setText(self.newText.toString())
Run Code Online (Sandbox Code Playgroud)一旦纠正,我在你的代码中发现了另一个错误:
TypeError: invalid result type from StandardItem.setData()
这是因为你做return True的setData(),应该返回void(所以在Python中,应该什么都不返回)