PyQt5 - 实时 QSlider
PyQt5 - QSlider in real time
我正在开发一个软件来阅读和处理视频。我刚刚开始这个项目,但我已经遇到了问题。我想创建一个实时反应的滑块。目前,滑块仅在几秒钟内移动。
我的滑块就是这样设置的。
self.slider = QSlider(Qt.Horizontal)
self.slider.sliderMoved.connect(self.set_position)
def set_position(self, position):
self.mediaPlayer.setPosition(position)
我尝试使用 setTickPosition(QSlider::TickPosition position) 但它没有任何改变。可能是我没用好功能
也许这段代码可以帮助您了解我卡在哪里
from PyQt5.QtWidgets import QWidget, QApplication, QSlider, QHBoxLayout, QStyle, QFileDialog, QPushButton, QVBoxLayout
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtGui import QIcon
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import Qt, QUrl
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Slider tick settings")
self.setGeometry(350, 100, 720, 700)
self.setWindowIcon(QIcon("icon.png"))
self.init_ui()
self.show()
def init_ui(self):
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
VideoWidget = QVideoWidget()
self.slider = QSlider(Qt.Horizontal)
self.slider.sliderMoved.connect(self.set_position)
self.playBtn = QPushButton("Play")
self.openFile = QPushButton("Open File")
self.playBtn.clicked.connect(self.play_video)
self.openFile.clicked.connect(self.open_file)
self.qhbox = QHBoxLayout()
self.qvbox = QVBoxLayout()
self.qhbox.addWidget(self.slider)
self.qhbox.addWidget(self.openFile)
self.qhbox.addWidget(self.playBtn)
self.qvbox.addLayout(self.qhbox)
self.qvbox.addWidget(VideoWidget)
self.setLayout(self.qvbox)
self.mediaPlayer.setVideoOutput(VideoWidget)
self.mediaPlayer.stateChanged.connect(self.mediastate_changed)
self.mediaPlayer.positionChanged.connect(self.position_changed)
self.mediaPlayer.durationChanged.connect(self.duration_changed)
def set_position(self, position):
self.mediaPlayer.setPosition(position)
def mediastate_changed(self, state):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.playBtn.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.playBtn.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
def open_file(self):
filename, _ = QFileDialog.getOpenFileName(self, "Open Video")
if filename != '':
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(filename)))
self.playBtn.setEnabled(True)
#self.getRealFilename(filename)
def position_changed(self, position):
self.slider.setValue(position)
def duration_changed(self, duration):
self.slider.setRange(0, duration)
def set_position(self, position):
self.mediaPlayer.setPosition(position)
def play_video(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState :
self.mediaPlayer.pause()
else :
self.mediaPlayer.play()
if __name__ == "__main__":
app = QApplication(sys.argv)
Window = Window()
sys.exit(app.exec_())
刻度与滑块无关。
问题出在 QMediaPlayer 用于通知的默认间隔,即 1000 毫秒。
如果您想更改它,请使用 setNotifyInterval()
和 适当的 毫秒间隔(我建议不要使用小于 100-150 毫秒的值)。
我正在开发一个软件来阅读和处理视频。我刚刚开始这个项目,但我已经遇到了问题。我想创建一个实时反应的滑块。目前,滑块仅在几秒钟内移动。 我的滑块就是这样设置的。
self.slider = QSlider(Qt.Horizontal)
self.slider.sliderMoved.connect(self.set_position)
def set_position(self, position):
self.mediaPlayer.setPosition(position)
我尝试使用 setTickPosition(QSlider::TickPosition position) 但它没有任何改变。可能是我没用好功能
也许这段代码可以帮助您了解我卡在哪里
from PyQt5.QtWidgets import QWidget, QApplication, QSlider, QHBoxLayout, QStyle, QFileDialog, QPushButton, QVBoxLayout
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtGui import QIcon
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import Qt, QUrl
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Slider tick settings")
self.setGeometry(350, 100, 720, 700)
self.setWindowIcon(QIcon("icon.png"))
self.init_ui()
self.show()
def init_ui(self):
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
VideoWidget = QVideoWidget()
self.slider = QSlider(Qt.Horizontal)
self.slider.sliderMoved.connect(self.set_position)
self.playBtn = QPushButton("Play")
self.openFile = QPushButton("Open File")
self.playBtn.clicked.connect(self.play_video)
self.openFile.clicked.connect(self.open_file)
self.qhbox = QHBoxLayout()
self.qvbox = QVBoxLayout()
self.qhbox.addWidget(self.slider)
self.qhbox.addWidget(self.openFile)
self.qhbox.addWidget(self.playBtn)
self.qvbox.addLayout(self.qhbox)
self.qvbox.addWidget(VideoWidget)
self.setLayout(self.qvbox)
self.mediaPlayer.setVideoOutput(VideoWidget)
self.mediaPlayer.stateChanged.connect(self.mediastate_changed)
self.mediaPlayer.positionChanged.connect(self.position_changed)
self.mediaPlayer.durationChanged.connect(self.duration_changed)
def set_position(self, position):
self.mediaPlayer.setPosition(position)
def mediastate_changed(self, state):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.playBtn.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.playBtn.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
def open_file(self):
filename, _ = QFileDialog.getOpenFileName(self, "Open Video")
if filename != '':
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(filename)))
self.playBtn.setEnabled(True)
#self.getRealFilename(filename)
def position_changed(self, position):
self.slider.setValue(position)
def duration_changed(self, duration):
self.slider.setRange(0, duration)
def set_position(self, position):
self.mediaPlayer.setPosition(position)
def play_video(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState :
self.mediaPlayer.pause()
else :
self.mediaPlayer.play()
if __name__ == "__main__":
app = QApplication(sys.argv)
Window = Window()
sys.exit(app.exec_())
刻度与滑块无关。
问题出在 QMediaPlayer 用于通知的默认间隔,即 1000 毫秒。
如果您想更改它,请使用 setNotifyInterval()
和 适当的 毫秒间隔(我建议不要使用小于 100-150 毫秒的值)。