Python TypeError:PyQt4.QtCore.QVariantAnimation表示一个C++;抽象类,无法实例化

Python TypeError:PyQt4.QtCore.QVariantAnimation表示一个C++;抽象类,无法实例化,python,pyqt,pyqt5,pyqt4,qvariantanimation,Python,Pyqt,Pyqt5,Pyqt4,Qvariantanimation,我有一个PyQt5片段,我正试图将其转换为PyQt4。PyQt5版本工作得很好,但是当我尝试转换成PyQt4时,我得到了这个错误。我删除了QtWidgets,但仍然收到此错误。我还试图实例化self.animation=QtCore.QVariantAnimation(),但仍然得到相同的错误 Traceback (most recent call last): File ".\test1.py", line 29, in <module> lineedit = Line

我有一个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
损坏的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_())
< Q> 4UpTurrrTrimeValueE()中的

< P>是一个需要实现的纯虚方法,在QT5的情况下,它只是变成一种虚拟方法,在第一种情况下C++强制实现它,但是QT5的改变不再需要,所以解决方案是实现该方法:

#。。。
类VariantAnimation(QtCore.QVariantAnimation):
def UPDATECURENTVALUE(自身,值):
通过
类LineEdit(QtGui.QLineEdit):
定义初始化(自):
超级(行编辑,自我)。\uuuuu初始化
self.textChanged.connect(self.start\u动画)
self.animation=VariantAnimation(
startValue=QtGui.QColor(255、127、127),
endValue=QtGui.QColor(255,255,255),
持续时间=1000,
valueChanged=self.on\u color\u change,
)
# ...
@pyqtSlot(QtCore.QVariant)
@pyqtlot(QtGui.QColor)
def on_color_change(自身,颜色):
如果isinstance(颜色,QtCore.QVariant):
颜色=QtGui.QColor(颜色)
self.setStyleSheet(“QLineEdit{background color:%s}”%(color.name(),)
# ...
Qt4 UpDeCurrRealValueAudio()中的>P>是一个需要实现的纯虚方法,在QT5的情况下,它只是变成一个虚拟方法,在第一种情况下C++强制实现它,但是QT5的改变不再需要,所以解决方案是实现该方法:

#。。。
类VariantAnimation(QtCore.QVariantAnimation):
def UPDATECURENTVALUE(自身,值):
通过
类LineEdit(QtGui.QLineEdit):
定义初始化(自):
超级(行编辑,自我)。\uuuuu初始化
self.textChanged.connect(self.start\u动画)
self.animation=VariantAnimation(
startValue=QtGui.QColor(255、127、127),
endValue=QtGui.QColor(255,255,255),
持续时间=1000,
valueChanged=self.on\u color\u change,
)
# ...
@pyqtSlot(QtCore.QVariant)
@pyqtlot(QtGui.QColor)
def on_color_change(自身,颜色):
如果isinstance(颜色,QtCore.QVariant):
颜色=QtGui.QColor(颜色)
self.setStyleSheet(“QLineEdit{background color:%s}”%(color.name(),)
# ...

Hi@eyllansc,这修复了错误,但现在我得到了
AttributeError:'QVariant'对象在颜色更改的
中没有属性“name”
function@coffeewin蟒蛇2还是蟒蛇3?我用的是蟒蛇2。7@coffeewin查看我的更新。由于Python和SIP版本具有相同的限制,PyQt在每个版本的Python中的行为都不同,因此您必须放置环境的信息。哇,您太棒了!QLineEdit最初为红色是否正常?如何将其更改为最初的白色?嗨@eyllanesc,这修复了错误,但现在我得到的是
AttributeError:'QVariant'对象在
中没有属性“name”
function@coffeewin蟒蛇2还是蟒蛇3?我用的是蟒蛇2。7@coffeewin查看我的更新。由于Python和SIP版本具有相同的限制,PyQt在每个版本的Python中的行为都不同,因此您必须放置环境的信息。哇,您太棒了!QLineEdit最初为红色是否正常?如何将其更改为最初的白色?
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_())