I would like to place a phonon videowidget onto a qgraphicsscene so I can overlay graphics etc. When I run the following I get the sound, but no video playing on the qgraphicsview. Help would be appreciated as I thought I was doing as the examples show. However, I suspect its something in how I have understood videoplayer and videowidget.
For testing I am just playing a video straight from a file.
from PySide import QtGui, QtCore
from PySide.phonon import Phonon
from window import Ui_MainWindow # main GUI window
import os, sys
class DiagramScene(QtGui.QGraphicsScene):
InsertItem, InsertLine, InsertText, MoveItem = range(4)
def __init__(self):
super(DiagramScene, self).__init__()
self.myLineColor = QtCore.Qt.black
self.myMode = "Start"
self.line = None
def mousePressEvent(self, mouseEvent):
if (mouseEvent.button() == QtCore.Qt.LeftButton):
if self.myMode == "Start":
self.line = QtGui.QGraphicsLineItem(QtCore.QLineF(mouseEvent.scenePos(), mouseEvent.scenePos()))
self.addItem(self.line)
elif (mouseEvent.button() == QtCore.Qt.RightButton):
self.addText("Hello")
super(DiagramScene, self).mousePressEvent(mouseEvent)
def mouseMoveEvent(self, mouseEvent):
if self.line:
newLine = QtCore.QLineF(self.line.line().p1(), mouseEvent.scenePos())
self.line.setLine(newLine)
def mouseReleaseEvent(self, mouseEvent):
self.line = None
super(DiagramScene, self).mouseReleaseEvent(mouseEvent)
class QPlayer(QtGui.QWidget):
def __init__(self):
super(QPlayer, self).__init__()
media_src = Phonon.MediaSource("C:\Users\Public\Videos\Sample Videos\Wildlife.wmv")
self.audioOuptut=Phonon.AudioOutput(Phonon.MusicCategory, self)
self.player=Phonon.MediaObject(self)
self.player.setCurrentSource(media_src)
Phonon.createPath(self.player, self.audioOuptut)
self.videoWidget=Phonon.VideoWidget(self)
self.videoWidget.FitInView
Phonon.createPath(self.player, self.videoWidget)
self.player.play()
class Main(QtGui.QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.scene = DiagramScene()
self.scene.addWidget(QPlayer())
self.gview = self.ui.gView
self.gview.setScene(self.scene)
def main():
app = QtGui.QApplication(sys.argv)
window=Main()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Ok - think I've sorted it (to an extent). Simple case of:
self.videoWidget.setMinimumSize(640,480)
The video doesn't really run very well - breaks up a lot but at least I can draw on it :)
Related
I'm making a program to specify a specific area using pyqt5.
If I click on the capture button, I will hide the MainWindow and display a screen to specify the area. I want to clear the screen and return to the mainwindow by pressing the esc key. But MainWindow does not show again. How should I work it?
form_class = uic.loadUiType("prototype.ui")[0]
class MainWindow(QtWidgets.QMainWindow, form_class) :
def __init__(self):
super().__init__()
self.setupUi(self)
self.Capture_Button.clicked.connect(self.Capture_Btn_clicked)
self.CaptureWindow = CaptureWidget()
self.CaptureWindow.hide()
def Capture_Btn_clicked(self) :
self.hide()
self.CaptureWindow.close()
self.CaptureWindow.__init__()
self.CaptureWindow.show()
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
def enterEvent(self, QEvent):
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.setWindowOpacity(1)
This is the class that specifies the area (some of the code is omitted).
class CaptureWidget(QtWidgets.QDialog):
def __init__(self):
super().__init__()
root = tk.Tk()
self.setupUi(self)
def setupUi(self) :
self.screen_width = root.winfo_screenwidth()
self.screen_height = root.winfo_screenheight()
self.setGeometry(0, 0, self.screen_width, self.screen_height)
self.setWindowTitle(' ')
self.begin = QtCore.QPoint()
self.end = QtCore.QPoint()
self.setWindowOpacity(0.3)
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.setWindowFlags(QtCore.Qt.FramelessWindowHint|QtCore.Qt.WindowStaysOnTopHint)
print('Capture the screen...')
self.is_set_region = False
self.is_mouse_click = False
self.show()
def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_Escape:
print('esc')
self.close()
elif key == Qt.Key_F1:
self.close()
self.__init__()
First of all, instead of overwriting the keyPressEvent method, it is easier to use QShortcut. On the other hand for this case it is better to create a signal that indicates when the escape key is pressed connecting it to the show method.
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class CaptureWidget(QtWidgets.QDialog):
escape_pressed = QtCore.pyqtSignal()
def __init__(self):
super().__init__()
self.setupUi()
def setupUi(self):
self.setWindowOpacity(0.3)
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
shorcut_scaped = QtWidgets.QShortcut(QtCore.Qt.Key_Escape, self)
shorcut_scaped.activated.connect(self.escape_pressed)
shorcut_scaped.activated.connect(self.close)
shorcut = QtWidgets.QShortcut(QtCore.Qt.Key_F1, self)
shorcut.activated.connect(self.close)
form_class, _ = uic.loadUiType("prototype.ui")
class MainWindow(QtWidgets.QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.Capture_Button.clicked.connect(self.Capture_Btn_clicked)
self.CaptureWindow = CaptureWidget()
self.CaptureWindow.escape_pressed.connect(self.show)
#QtCore.pyqtSlot()
def Capture_Btn_clicked(self):
self.hide()
self.CaptureWindow.showFullScreen()
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
def enterEvent(self, QEvent):
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.setWindowOpacity(1)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I would like to implement a class to create a simple widget of fixed size with a scrollbar to display one or more (that's crucial to the problem) images at the same time. Here is the (yet complete but working) code:
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
imageViewer.show()
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
sys.exit(app.exec_())
The problem is, that the image does not show up at startup resp does not have the desired size. One has to press "n" first, then the image is displayed with its natural size. And of course I would like to have its natural size from the beginning on without the need to press "n" first.
It seems strange to me that pressing "n" and calling self.normalSize() do not have the same effect, and even simulation the key event by the two commented outlines in setImages do not have the same effect as pressing "n" physically.
There are two "solutions":
Show the widget after setting image, that is, move the line imageViewer.show() 2 lines down.
Moving the first 3 lines of the method setImages to the __init__ method.
Both are no reasonable option, since I want to add and remove dynamically QLabels(which is not implemented yet) to display different images, and also the number of images (which are displayed at the same time) can change.
Any suggestions?
Hi I have modified your code.
Added this 2 lines.
self.timerSingleShot = QtCore.QTimer()
self.timerSingleShot.singleShot(1, self.normalSize)
Use with PyQt5 syntax. This syntax is for PyQt4
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
# Added this lines
self.timerSingleShot = QtCore.QTimer()
self.timerSingleShot.singleShot(1, self.normalSize)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
#self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
imageViewer.show()
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
sys.exit(app.exec_())
This will also work. Shifted line :
imageViewer.show()
in your code.
from PyQt5 import QtCore, QtWidgets, QtGui
class ImageViewWidget(QtWidgets.QScrollArea):
def __init__(self, parent = None):
super(ImageViewWidget, self).__init__(parent)
self.w = QtWidgets.QFrame()
self.l = QtWidgets.QVBoxLayout()
self.w.setLayout(self.l)
self.setWidget(self.w)
def setImages(self, *images):
self.imageLabel = QtWidgets.QLabel()
self.imageLabel.setScaledContents(True)
self.l.addWidget(self.imageLabel)
if not images[0].isNull():
self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(images[0]))
#self.normalSize()
## event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, QtCore.Qt.Key_N, QtCore.Qt.NoModifier)
## QtWidgets.QApplication.sendEvent(self, event)
def normalSize(self):
self.w.adjustSize()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_N:
self.normalSize()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imageViewer = ImageViewWidget()
imageViewer.resize(800, 600)
image1 = QtGui.QImage('test.png')
imageViewer.setImages(image1)
imageViewer.show()
sys.exit(app.exec_())
Briefly I want to track mouse coordinate over a QGraphicsView.
This answer works well for a QLabel object, but not works as expected when I switch to QGraphicsView as follows:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.graphicsView = QtGui.QGraphicsView(self)
self.graphicsView.setMouseTracking(True)
self.graphicsView.installEventFilter(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.graphicsView)
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.MouseMove and
source is self.graphicsView):
pos = event.pos()
print('mouse move: (%d, %d)' % (pos.x(), pos.y()))
return QtGui.QWidget.eventFilter(self, source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
window.resize(200, 100)
sys.exit(app.exec_())
Specifically, it seems like that the event is caught only when my cursor moves accross the border of the QGraphicsView (the black lines).
Could anyone tell me why and is there any better solutions?
For certain widgets, you need to use its viewport instead:
self.graphicsView.viewport().installEventFilter(self)
...
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.MouseMove and
source is self.graphicsView.viewport()):
...
An alternative is to override mouseMoveEvent(event) of QGraphicsView directly.
Example:
from PySide import QtGui
class MyGraphicsView(QtGui.QGraphicsView):
def __init__(self, parent):
QtGui.QGraphicsView.__init__(self, parent)
self.setMouseTracking(True)
def mouseMoveEvent(self, event):
print('mouseMoveEvent: pos {}'.format(event.pos()))
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.graphicsView = MyGraphicsView(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.graphicsView)
app = QtGui.QApplication([])
window = Window()
window.show()
window.resize(200, 100)
app.exec_()
I'm trying to catch a closeEvent for several dockWidgets that get added dynamically to a QMainWindow. It is unclear to me how I can figure out which widget has been closed.. Here's an simplified example:
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.leftDockWidget = QtGui.QDockWidget('pick tool', self)
self.leftDockWidget.setWidget( QtGui.QLabel('a dock widget') )
self.addDockWidget( QtCore.Qt.LeftDockWidgetArea, self.leftDockWidget )
self.leftDockWidget.closeEvent = self.dockWidgetCloseEvent
self.show()
def dockWidgetCloseEvent(self, event):
print event
# how to get sender widget ?
event.sender() doesn't seem to exist..
any ideas ?
thanks
One way to achieve what you want would be to use an event filter:
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.leftDockWidget = QtGui.QDockWidget('pick tool', self)
self.leftDockWidget.setWidget(QtGui.QLabel('a dock widget'))
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.leftDockWidget)
self.leftDockWidget.installEventFilter(self)
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.Close and
isinstance(source, QtGui.QDockWidget)):
print source.windowTitle()
return super(Example, self).eventFilter(source, event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Example()
window.show()
sys.exit(app.exec_())
def dockWidgetCloseEvent(self, event):
self.sender()
I've had soooo much trouble getting this code to work properly!!!! It runs fine when I debug it step by step, but when running normally it just crashes. Initially I was using a QThread to update the ImagePreview pixmap, but after a whole day of crashes and pain, I changed course. Now it works, in the above scenario when using the debugger, but otherwise I'm stumped. Please help me! What's wrong with this code? Is there another approach I can use? I'm trying to constantly update the image preview with an image downloaded from a url.
import sys
import io
import urllib2
from PySide import QtCore, QtGui, QtNetwork
import time
class QDownloadBuffer(QtCore.QBuffer):
downloadFinished = QtCore.Signal()
def __init__(self):
super(QDownloadBuffer, self).__init__()
self.open(QtCore.QBuffer.ReadWrite)
self.url = QtCore.QUrl("http://www.google.com.au/images/srpr/logo3w.png")
self.manager = QtNetwork.QNetworkAccessManager()
self.request = QtNetwork.QNetworkRequest(self.url)
self.manager.finished.connect(self.onFinished)
def startDownload(self):
print("Starting Download --")
self.reply = self.manager.get(self.request)
self.reply.error[QtNetwork.QNetworkReply.NetworkError].connect(self.onError)
def onFinished(self):
print("Download Finished -- ")
print(self.write(self.reply.readAll()))
self.reply.close()
self.downloadFinished.emit()
def onError(self):
print("oh no there is an error -- ")
print(self.reply.error())
class ImagePreview(QtGui.QWidget):
def __init__(self, parent=None):
super(ImagePreview, self).__init__(parent)
self.setMinimumSize(50, 50)
self.text = None
self.pixmap = None
self.dl_n = 0
def paintEvent(self, paintEvent):
painter = QtGui.QPainter(self)
if(self.pixmap):
painter.drawPixmap(0, 0, self.pixmap)
if(self.text):
painter.setPen(QtCore.Qt.blue)
painter.setFont(QtGui.QFont("Arial", 30))
painter.drawText(self.rect(), QtCore.Qt.AlignCenter, self.text)
def startDownload(self):
self.setText(str(self.dl_n))
self.dl_n += 1
print("Starting Download {0}".format(self.dl_n))
self.db = QDownloadBuffer()
self.connect(self.db, QtCore.SIGNAL("downloadFinished()"), self, QtCore.SLOT("ondownloadFinished()"))
self.db.startDownload()
def ondownloadFinished(self):
self.paintImage()
print("download finished?")
self.db.close()
self.startDownload()
def paintImage(self):
print("Painting")
pixmap = QtGui.QPixmap()
pixmap.loadFromData(self.db.data())
self.setPixmap(pixmap)
def setPixmap(self, pixmap):
self.pixmap = pixmap
self.setMinimumSize(pixmap.width(), pixmap.height())
self.update()
def setText(self, text):
self.text = text
self.update()
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.imagepreview = ImagePreview()
self.button = QtGui.QPushButton("Start")
self.button.clicked.connect(self.imagepreview.startDownload)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.button)
layout.addWidget(self.imagepreview)
self.setLayout(layout)
if __name__ == "__main__":
import sys
try:
app = QtGui.QApplication(sys.argv)
except RuntimeError:
pass
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
I think the problem is that you are calling self.startDownload() from slot (signal handler). So you are not returning control to Qt main loop (or something like this). Proper way is to call it as deferred event, e.g. by calling it through QTimer.singleShot:
def ondownloadFinished(self):
self.paintImage()
print("download finished?")
self.db.close()
QtCore.QTimer.singleShot(0, self.startDownload)
Note that singleShot with msec set to 0:
QtCore.QTimer.singleShot(0, self.startDownload)
is the same as:
QtCore.QMetaObject.invokeMethod(self, 'startDownload', QtCore.Qt.QueuedConnection)
(source, related question)