PyQt5向QMediaPlayer发送一个信号

时间:2021-05-16 23:04:52

Teaching myself PyQt5, I wanted to write a program that plays a video link found on a website. Since QT Designer didn't have a video widget, I had to find my own that someone else had written, and imported it into my program. The entire code that I'm importing can be found HERE. This video widget window is a 2nd window that opens after the video is selected.

教自己PyQt5,我想写一个程序,播放在网站上找到的视频链接。由于QT设计器没有视频小部件,所以我必须找到其他人编写的我自己的小部件,并将它导入到我的程序中。我导入的整个代码都可以在这里找到。这个视频窗口是在视频被选中后打开的第二个窗口。

What I'm trying to do is change the video window's title when the playlist's index changes. Seems I should be able to do this with: self.playlist.currentIndexChanged.connect(self.dialog.setWindowTitle('Changed!')) but the problem I'm having is since I'm importing the class/code that opens the video widget, I can't figure out how to watch for the signal.

我要做的是当播放列表的索引改变时,改变视频窗口的标题。看起来我应该可以用:self。playlist. currentindexchange .connect(self.dialog.setWindowTitle('Changed!'))来实现这个功能,但是我遇到的问题是,因为我正在导入打开视频小部件的类/代码,所以我不知道如何查看信号。

Here's my function:

这是我的功能:

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!'))

So I'm kind of lost here. How do I watch for this signal change, and then pass it over to the video widget I've imported so I can change the window title (or other things, like I want to enable the "next" button if it's a playlist, or the "previous" button after the first video in the playlist has played)?

所以我有点迷糊了。我怎么看这个信号的变化,然后将其传递到视频小部件我进口可以改变窗口标题(或其他的事情,比如我想启用“下一个”按钮,如果它是一个播放列表,或者是“以前”按钮后第一个视频播放列表中扮演了)?

1 个解决方案

#1


0  

Here's the solution. Change the last line of the function to:

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

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

Which then calls this function:

这个函数叫做

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

#1


0  

Here's the solution. Change the last line of the function to:

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

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

Which then calls this function:

这个函数叫做

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