TypeError: PyQt4.QtCore.QVariantAnimation represents a C++ abstract class and cannot be instantiated
TypeError: PyQt4.QtCore.QVariantAnimation represents a C++ abstract class and cannot be instantiated
我有这个 PyQt5 片段,我正试图将其转换为 PyQt4。 PyQt5 版本运行良好,但当我尝试转换为 PyQt4 时,出现此错误。我删除了 QtWidgets
但我仍然收到此错误。我也尝试仅实例化 self.animation = QtCore.QVariantAnimation()
但仍然出现相同的错误。
Traceback (most recent call last):
File ".\test1.py", line 29, in <module>
lineedit = LineEdit()
File ".\test1.py", line 13, in __init__
valueChanged=self.on_color_change,
TypeError: PyQt4.QtCore.QVariantAnimation represents a C++ abstract class and cannot be instantiated
工作 PyQt5 版本
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class LineEdit(QtWidgets.QLineEdit):
def __init__(self):
super(LineEdit, self).__init__()
self.textChanged.connect(self.start_animation)
self.animation = QtCore.QVariantAnimation(
startValue=QtGui.QColor(255, 127, 127),
endValue=QtGui.QColor(255, 255, 255),
duration=1000,
valueChanged=self.on_color_change,
)
@QtCore.pyqtSlot()
def start_animation(self):
if self.animation.state() == QtCore.QAbstractAnimation.Running:
self.animation.stop()
self.animation.start()
@QtCore.pyqtSlot(QtCore.QVariant)
@QtCore.pyqtSlot(QtGui.QColor)
def on_color_change(self, color):
self.setStyleSheet("QLineEdit{background-color: %s}" % (color.name(),))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
lineedit = LineEdit()
lineedit.show()
sys.exit(app.exec_())
损坏的 PyQt4 版本
import sys
from PyQt4 import QtCore, QtGui
class LineEdit(QtGui.QLineEdit):
def __init__(self):
super(LineEdit, self).__init__()
self.textChanged.connect(self.start_animation)
self.animation = QtCore.QVariantAnimation(
startValue=QtGui.QColor(255, 127, 127),
endValue=QtGui.QColor(255, 255, 255),
duration=1000,
valueChanged=self.on_color_change,
)
@QtCore.pyqtSlot()
def start_animation(self):
if self.animation.state() == QtCore.QAbstractAnimation.Running:
self.animation.stop()
self.animation.start()
@QtCore.pyqtSlot(QtCore.QVariant)
@QtCore.pyqtSlot(QtGui.QColor)
def on_color_change(self, color):
self.setStyleSheet("QLineEdit{background-color: %s}" % (color.name(),))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
lineedit = LineEdit()
lineedit.show()
sys.exit(app.exec_())
有人知道如何解决这个问题吗?
在 Qt4 中 updateCurrentValue() 是一个需要实现的纯虚方法,在 Qt5 的情况下它只是一个虚方法,在第一种情况下 C++ 强制实现它但是改变了Qt5 不再需要,所以解决方案是实现该方法:
# ...
class VariantAnimation(QtCore.QVariantAnimation):
def updateCurrentValue(self, value):
pass
class LineEdit(QtGui.QLineEdit):
def __init__(self):
super(LineEdit, self).__init__()
self.textChanged.connect(self.start_animation)
self.animation = VariantAnimation(
startValue=QtGui.QColor(255, 127, 127),
endValue=QtGui.QColor(255, 255, 255),
duration=1000,
valueChanged=self.on_color_change,
)
# ...
@QtCore.pyqtSlot(QtCore.QVariant)
@QtCore.pyqtSlot(QtGui.QColor)
def on_color_change(self, color):
if isinstance(color, QtCore.QVariant):
color = QtGui.QColor(color)
self.setStyleSheet("QLineEdit{background-color: %s}" % (color.name(),))
# ...
我有这个 PyQt5 片段,我正试图将其转换为 PyQt4。 PyQt5 版本运行良好,但当我尝试转换为 PyQt4 时,出现此错误。我删除了 QtWidgets
但我仍然收到此错误。我也尝试仅实例化 self.animation = QtCore.QVariantAnimation()
但仍然出现相同的错误。
Traceback (most recent call last):
File ".\test1.py", line 29, in <module>
lineedit = LineEdit()
File ".\test1.py", line 13, in __init__
valueChanged=self.on_color_change,
TypeError: PyQt4.QtCore.QVariantAnimation represents a C++ abstract class and cannot be instantiated
工作 PyQt5 版本
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class LineEdit(QtWidgets.QLineEdit):
def __init__(self):
super(LineEdit, self).__init__()
self.textChanged.connect(self.start_animation)
self.animation = QtCore.QVariantAnimation(
startValue=QtGui.QColor(255, 127, 127),
endValue=QtGui.QColor(255, 255, 255),
duration=1000,
valueChanged=self.on_color_change,
)
@QtCore.pyqtSlot()
def start_animation(self):
if self.animation.state() == QtCore.QAbstractAnimation.Running:
self.animation.stop()
self.animation.start()
@QtCore.pyqtSlot(QtCore.QVariant)
@QtCore.pyqtSlot(QtGui.QColor)
def on_color_change(self, color):
self.setStyleSheet("QLineEdit{background-color: %s}" % (color.name(),))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
lineedit = LineEdit()
lineedit.show()
sys.exit(app.exec_())
损坏的 PyQt4 版本
import sys
from PyQt4 import QtCore, QtGui
class LineEdit(QtGui.QLineEdit):
def __init__(self):
super(LineEdit, self).__init__()
self.textChanged.connect(self.start_animation)
self.animation = QtCore.QVariantAnimation(
startValue=QtGui.QColor(255, 127, 127),
endValue=QtGui.QColor(255, 255, 255),
duration=1000,
valueChanged=self.on_color_change,
)
@QtCore.pyqtSlot()
def start_animation(self):
if self.animation.state() == QtCore.QAbstractAnimation.Running:
self.animation.stop()
self.animation.start()
@QtCore.pyqtSlot(QtCore.QVariant)
@QtCore.pyqtSlot(QtGui.QColor)
def on_color_change(self, color):
self.setStyleSheet("QLineEdit{background-color: %s}" % (color.name(),))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
lineedit = LineEdit()
lineedit.show()
sys.exit(app.exec_())
有人知道如何解决这个问题吗?
在 Qt4 中 updateCurrentValue() 是一个需要实现的纯虚方法,在 Qt5 的情况下它只是一个虚方法,在第一种情况下 C++ 强制实现它但是改变了Qt5 不再需要,所以解决方案是实现该方法:
# ...
class VariantAnimation(QtCore.QVariantAnimation):
def updateCurrentValue(self, value):
pass
class LineEdit(QtGui.QLineEdit):
def __init__(self):
super(LineEdit, self).__init__()
self.textChanged.connect(self.start_animation)
self.animation = VariantAnimation(
startValue=QtGui.QColor(255, 127, 127),
endValue=QtGui.QColor(255, 255, 255),
duration=1000,
valueChanged=self.on_color_change,
)
# ...
@QtCore.pyqtSlot(QtCore.QVariant)
@QtCore.pyqtSlot(QtGui.QColor)
def on_color_change(self, color):
if isinstance(color, QtCore.QVariant):
color = QtGui.QColor(color)
self.setStyleSheet("QLineEdit{background-color: %s}" % (color.name(),))
# ...