首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError: PyQt4.QtCore.QVariantAnimation表示一个C++抽象类,不能实例化

TypeError: PyQt4.QtCore.QVariantAnimation表示一个C++抽象类,不能实例化
EN

Stack Overflow用户
提问于 2019-06-13 03:28:13
回答 1查看 1.9K关注 0票数 2

我有一个PyQt5片段,我正试图将其转换为PyQt4。PyQt5版本工作得很好,但是当我试图转换为PyQt4时,我会得到这个错误。我删除了QtWidgets,但仍然收到此错误。我还试图实例化self.animation = QtCore.QVariantAnimation(),但仍然得到相同的错误。

代码语言:javascript
复制
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版本

代码语言:javascript
复制
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版本

代码语言:javascript
复制
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_())

有人知道怎么解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-13 03:39:58

在Qt4 updateCurrentValue()中,是一个需要实现的纯虚拟方法,在Qt5情况下,它只是一个虚拟方法,在第一种情况下,C++强制实现它,但是随着Qt5的改变,它不再需要实现,所以解决方案是实现该方法:

代码语言:javascript
复制
# ...
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(),))
    # ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56572976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档