Skip to content

Instantly share code, notes, and snippets.

@akiross
Last active August 29, 2015 14:17
Show Gist options
  • Save akiross/7626deffda99629f07c7 to your computer and use it in GitHub Desktop.
Save akiross/7626deffda99629f07c7 to your computer and use it in GitHub Desktop.
Event loop with QtQuick
#!/usr/bin/env python3
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtQuick import *
from PyQt5.QtWidgets import *
import sys
class Rect(QQuickPaintedItem):
def __init__(self, parent=None):
super().__init__(parent)
self.setWidth(30)
self.setHeight(30)
self.setX(30)
self.setY(30)
def paint(self, pt):
pt.fillRect(self.contentsBoundingRect(), Qt.red)
class Window(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self._win = QQuickWindow()
self._wid = QWidget.createWindowContainer(self._win)
self._wid.setMinimumSize(QSize(200, 200))
lay = QVBoxLayout()
lay.addWidget(self._wid)
self.setLayout(lay)
self._item = Rect(self._win.contentItem())
def keyPressEvent(self, event):
super().keyPressEvent(event)
print('Key pressed')
an = QPropertyAnimation(self._item, 'x')
an.setDuration(2000)
an.setEndValue(100)
ev = QEventLoop()
an.finished.connect(ev.quit)
an.start()
# Prevents other events to be processed
ev.exec_(QEventLoop.ExcludeUserInputEvents)
print('Anim finished!')
if __name__ == '__main__':
a = QApplication(sys.argv)
w = Window()
w.show()
a.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment