【问题标题】:PyQt5 sending a signal to QMediaPlayerPyQt5 向 QMediaPlayer 发送信号
【发布时间】:2016-03-22 05:39:41
【问题描述】:

自学 PyQt5,我想编写一个程序来播放网站上的视频链接。由于 QT Designer 没有视频小部件,我必须找到别人编写的自己的小部件,并将其导入到我的程序中。我正在导入的整个代码可以在HERE 找到。此视频小部件窗口是在选择视频后打开的第二个窗口。

我要做的是在播放列表的索引更改时更改视频窗口的标题。似乎我应该能够做到这一点:self.playlist.currentIndexChanged.connect(self.dialog.setWindowTitle('Changed!')) 但我遇到的问题是因为我正在导入打开视频小部件的类/代码,我无法弄清楚如何观察信号。

这是我的功能:

def play_all_replays(self):
        """
        creates a QMediaPlaylist object and inserts all the games replays (in order) to the playlist
        and then plays it.
        :return:
        """
        self.dialog = videowidget.VideoPlayer()  # this is the video player widget I import
        self.playlist = QMediaPlaylist()  # the playlist
        self.dialog.mediaPlayer.setPlaylist(self.playlist)  #mediaplay is the QMediaPlayer created in videowidget()
        for v in self.single_game_highlights_ordered:
            url = QUrl(v)
            self.playlist.addMedia(QMediaContent(url))
        self.dialog.mediaPlayer.setPlaylist(self.playlist)

        self.playlist.setCurrentIndex(0)
        self.dialog.mediaPlayer.play()
        self.dialog.setWindowTitle('MLB Replay - {}')
        self.dialog.show()
        self.playlist.currentIndexChanged.connect(self.dialog.setWindowTitle('Changed!'))

所以我有点迷路了。如何观察此信号变化,然后将其传递给我导入的视频小部件,以便我可以更改窗口标题(或其他内容,例如如果它是播放列表,我想启用“下一步”按钮,或者播放列表中第一个视频播放后的“上一个”按钮)?

【问题讨论】:

  • Doh,我想我明白了。我不能将self.dialog.setWindowTitle('Changed!')self.playlist.currentIndexChanged.connect 一起使用。相反,我需要创建一个函数来处理信号(就像我对程序中的所有其他信号所做的那样)并将setWindowTitle 放在那里。这样做,它的工作原理。我猜是眼睛和大脑都累了。
  • 或:connect(lambda: self.dialog.setWindowTitle('Changed!'))
  • 既然您自己解决了问题,请考虑删除您的问题。如果您认为未来的读者可以从中受益,请添加您自己的答案。

标签: python python-3.x video pyqt5


【解决方案1】:

这是解决方案。将函数的最后一行更改为:

self.playlist.currentIndexChanged.connect(self.playlist_changed)

然后调用这个函数:

def playlist_changed(self):
        num = self.playlist.currentIndex()  # gets current playlist index
        self.dialog.setWindowTitle('MLB Replay - {}'.format(self.replay_window.item(num).text()))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多