'How to overlay (images) widgets in Pyqt5?
I am working on a project where I need to design a circular gauge which can rotate with animation(which I already achieved) but now I need to overlay an image under it here is my code(sorry its not properly identated also thanks in advance)
from PyQt5 import QtCore, QtGui, QtWidgets
class RotateMe(QtWidgets.QLabel):
def _init_(self, *args, **kwargs):
super(RotateMe, self)._init_(*args, **kwargs)
self._pixmap = QtGui.QPixmap()
self._animation = QtCore.QVariantAnimation(
self,
startValue=0.0,
endValue=30.0,
duration=1000,
valueChanged=self.on_valueChanged
)
def set_pixmap(self, pixmap):
self._pixmap = pixmap
self.setPixmap(self._pixmap)
def start_animation(self):
if self._animation.state() != QtCore.QAbstractAnimation.Running:
self._animation.start()
@QtCore.pyqtSlot(QtCore.QVariant)
def on_valueChanged(self, value):
t = QtGui.QTransform()
t.rotate(value)
self.setPixmap(self._pixmap.transformed(t))
class Widget(QtWidgets.QWidget):
def _init_(self, parent=None):
super(Widget, self)._init_(parent)
label = RotateMe(alignment=QtCore.Qt.AlignCenter)
img_path = os.path.join('safari.png')
label.set_pixmap(QtGui.QPixmap(img_path))
label.resize(100,200)
# label.setMaximumWidth(200)
# label.setMaximumHeight(200)
button = QtWidgets.QPushButton('Rotate')
button.clicked.connect(label.start_animation)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(label)
lay.addWidget(button)
if __name__ == '_main_':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
