当我在cythonize之前运行程序时,所有的纯.py文件都按其应有的方式工作,当动画值发生变化时,会调用连接到valueChanged的方法。但是,在我构建了cythonize之后,程序从.pyd扩展运行,连接到valueChanged信号的方法永远不会被调用。
虽然我用QVariantAnimation完成的信号检查了相同的东西,并且调用了它,但是所有的事情都正常工作,这是valueChanged的问题所在。
Python : 3.11 PyQt6版本:6.4.0Cython版本:3.0.0a11OS:Windows11
下面是一个示例,说明它在代码中的外观:
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,
)
...发布于 2022-10-29 15:43:20
我将value的类型注释设置为float,虽然Qt发送int,但由于使用了错误的注释,因此发生了冲突。
非常感谢大家的回答和解释,我非常感谢你们!
https://stackoverflow.com/questions/74245047
复制相似问题