在为我的一个程序测试QSound的过程中,我遇到了一个我无法理解的问题。每当我在另一个对象中实现QSound对象时,play()函数似乎不能调用QSound对象的成员变量。你知道吗
下面是我用来分析问题的例子:
from PyQt5.Qt import QApplication
from PyQt5.QtMultimedia import QSound
import sys
app=QApplication(sys.argv)
SoundObject=QSound("./path/sound.wav")
SoundObject.play()
SoundObject.play("./path/sound.wav")
sys.exit(app.exec())
from PyQt5.Qt import QApplication
from PyQt5.QtMultimedia import QSound
import sys
app=QApplication(sys.argv)
class SoundClass:
def __init__(self):
SoundObject = QSound("./path/sound.wav")
print(SoundObject.fileName()) # output= "./path/sound.wav"
SoundObject.play() # this doesn't do anything
SoundObject.play("./path/sound.wav")
SoundClass()
sys.exit(app.exec())
在这两种情况下,我都希望声音能连续播放两次。但是,当我在另一个对象中使用QSound对象时,“.play()”函数似乎会忽略对象的设置,而是调用静态函数Q声音播放(). QSound对象的其他成员变量(例如setLoops/loops)也会出现相同的行为。你知道吗
在寻找问题的答案时,我发现了一个与C++和QT相同的问题。虽然有人提到QSound可能会被否决,但没有一个明确的答案。(我想知道为什么4年后它仍然是文档的一部分。)链接到线程:QSound::play("soundpath") call works but a QSound object doesn't
我的规格:
我是错过了一些重要的东西还是这只是一个错误?你知道吗
在第二段代码中,SoundObject变量是一个局部变量,当构造函数完成执行时,它将被删除,因此如果它是一个不依赖于任何对象的静态方法,则不会执行第一个播放,而是执行第二个播放。你知道吗
在第一个代码中,SoundObject变量将在脚本完成时被删除,因为它的作用域更大。你知道吗
解决方案是通过使SoundObject成为类的成员来增加其范围:
相关问题 更多 >
编程相关推荐