From anonymous, 4 Months ago, written in Plain Text.
Embed
  1. # use PySide2 if available, else PyQt5
  2. try:
  3.     #raise ImportError("Forcing PyQt5") # force PyQt5 for testing when PySide2 is available
  4.     import PySide2 as PyQt
  5.     from PySide2 import QtCore, QtGui, QtWidgets
  6.     print("Qt using PySide")
  7. except ImportError as ex:
  8.     print(ex)
  9.     import sip
  10.     sip.setapi('QString', 2)
  11.     sip.setapi('QVariant', 2)
  12.     import PyQt5 as PyQt
  13.     from PyQt5 import QtCore, QtGui, QtWidgets
  14.     print("Qt using PyQt5")
  15.  
  16. import sys
  17.  
  18. _app = QtWidgets.QApplication(["test"])
  19.  
  20. class Canvas(QtWidgets.QWidget):
  21.     def paintEvent(self, event):
  22.         painter = QtGui.QPainter(self)
  23.         painter.drawLine(0, 0, self.width(), self.height())
  24.         img = QtGui.QImage(self.width(), self.height(), QtGui.QImage.Format_ARGB32)
  25.         img.fill(QtGui.QColor(128, 0, 0, alpha=128).rgba())
  26.         painter.drawImage(QtCore.QPointF(0, 0), img)
  27.  
  28. window = QtWidgets.QMainWindow()
  29. canvas = Canvas()
  30. window.setCentralWidget(canvas)
  31.  
  32. window.show()
  33.  
  34. sys.exit(_app.exec_())
  35.