当我在cythonize之前运行程序时,使用纯.py文件,一切都正常工作,当动画值更改时,会调用连接到valueChanged的方法。但是,在我构建了cythonize,并且程序从.pyd扩展运行之后,就再也不会调用连接到valueChanged信号的方法。
尽管我用QVariantAnimation完成的信号检查了同样的事情,并调用了它,但一切都正常,这是一个问题。
Python版本:3.11PyQt6版本:6.4.0Cython版本:3.0.0a11操作系统:Windows 11
以下是它在我的代码中的外观示例:
from PyQt6.QtCore import QAbstractAnimation, QVariant, QVariantAnimation, pyqtSlot
from PyQt6.QtWidgets import QHBoxLayout, QPushButton, QWidget
class SampleWidget(QWidget):
def __init__(self, parent: QWidget | None = None) -> None:
super().__init__(parent)
horizontal_layout = QHBoxLayout()
horizontal_layout.setContentsMargins(0, 0, 0, 0)
horizontal_layout.setSpacing(0)
self.button = QPushButton(self)
self.button.setText("Start animation")
self.button.clicked.connect(lambda: self.start_animation(180))
horizontal_layout.addWidget(self.button)
self.setLayout(horizontal_layout)
def start_animation(self, value: float) -> None:
self._animation = QVariantAnimation(self)
self._animation.setStartValue(0)
self._animation.setEndValue(value)
self._animation.setDuration(400)
self._animation.valueChanged.connect(self._on_animation_value_changed)
self._animation.finished.connect(self._on_animation_finished)
self._animation.start(QAbstractAnimation.DeletionPolicy.DeleteWhenStopped)
@pyqtSlot()
def _on_animation_finished(self) -> None:
# This executes as it should be after cythonize.
print("Animation finished")
@pyqtSlot(QVariant)
def _on_animation_value_changed(self, value: float) -> None:
# Here is problem. This is never executes after cythonize.
print("Animation value changed to", value)
和细胞化功能:
...
cythonize(
module_list=extension_modules,
# Don't build in source tree (this leaves behind .c files)
build_dir=BUILD_DIR,
# Don't generate an .html output file. This will contain source.
annotate=False,
# Tell Cython we're using Python 3
compiler_directives={"language_level": "3", "always_allow_keywords": True},
# (Optional) Always rebuild, even if files untouched
force=True,
)
...
我将value
的类型注释设置为float
,尽管Qt发送int
,但由于Cython使用了错误的注释,因此发生了冲突。
非常感谢大家的回答和解释,非常感谢大家!