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

前端 未结 4 2098
滥情空心
滥情空心 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()
    
    0 讨论(0)
  • 2021-01-07 06:36

    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.

    0 讨论(0)
  • 2021-01-07 06:38

    I used this post to make another solution that I think is maybe better. Here it is:

    class VerticalLabel(QLabel):
    
        def __init__(self, *args):
            QLabel.__init__(self, *args)
    
        def paintEvent(self, event):
            QLabel.paintEvent(self, event)
            painter = QPainter (self)
            painter.translate(0, self.height()-1)
            painter.rotate(-90)
            self.setGeometry(self.x(), self.y(), self.height(), self.width())
            QLabel.render(self, painter)
    
        def minimumSizeHint(self):
            size = QLabel.minimumSizeHint(self)
            return QSize(size.height(), size.width())
    
        def sizeHint(self):
            size = QLabel.sizeHint(self)
            return QSize(size.height(), size.width())
    
    0 讨论(0)
  • 2021-01-07 06:48

    Try this

    class myStyle(QCommonStyle):
    
        def __init__(self, angl=0, point=QPoint(0, 0)):
            super(myStyle, self).__init__()
            self.angl = angl
            self.point = point
    
        def drawItemText(self, painter, rect, flags, pal, enabled, text, textRole):
            if not text:
                return
            savedPen = painter.pen()
            if textRole != QPalette.NoRole:
                painter.setPen(QPen(pal.brush(textRole), savedPen.widthF()))
            if not enabled:
                pen = painter.pen()
                painter.setPen(pen)
            painter.translate(self.point)
            painter.rotate(self.angl)
            painter.drawText(rect, flags, text)
            painter.setPen(savedPen)
    

    and

    label = QLabel()
    label.setStyle(myStyle(-45, QPoint(0, 100)))
    
    0 讨论(0)
提交回复
热议问题