Search code examples
pythonqtqt4pyqtpyqt4

PyQT: Rotate a QLabel so that it's positioned diagonally instead of horizontally


I'm working on a touch screen app where gui space is very tight. I'd like to rotate a QLabel a bit so that it's either vertical, or offset at a diagonal a bit. Any suggestions? I couldn't find anything relevant on the QLabel interface.

Thanks so much!


Solution

  • QLabel does not do this. But you can easily create your own widget containing a bit of text:

    class MyLabel(QtGui.QWidget):
        def paintEvent(self, event):
            painter = QtGui.QPainter(self)
            painter.setPen(QtCore.Qt.black)
            painter.translate(20, 100)
            painter.rotate(-90)
            painter.drawText(0, 0, "hellos")
            painter.end()
    

    Another option is to draw a QGraphicsView, you then have the liberty to map real widgets (i.e. a QLabel) through any coordinate transformation.