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

前端 未结 4 2099
滥情空心
滥情空心 2021-01-07 06:17

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 sugg

4条回答
  •  再見小時候
    2021-01-07 06:32

    Answer from @Controlix is a generic implementation, however, I was getting a "recursive painting call" warning. I was able to address that by combining approaches from @Controlix and @Ivo. Here is my implementation:

    from PyQt5.Qt import QLabel
    from PyQt5 import QtGui
    
    
    class VerticalLabel(QLabel):
    
        def __init__(self, *args):
            QLabel.__init__(self, *args)
    
        def paintEvent(self, event):
            painter = QtGui.QPainter(self)
            painter.translate(0, self.height())
            painter.rotate(-90)
            painter.drawText(0, self.width()/2, self.text())
            painter.end()
    

提交回复
热议问题