I would like --- with Python and Qt4 --- to rotate the QPushButton (or at least its text), so it can stand upright. I saw some documentation on the Internet , but I could not understand that it is in C, and I am C -illiterate.
From what I read, you need to re-execute paintEvent () , instantiate and rotate QPainter (). However, I cannot figure out how to do this for the single QString or QPushButton that I need. I assumed that QPaintEvent will have a “sender” attribute, for example, signals, but this is not the case. All I can get from this event is QRect or QRegion.
How do I know which event depends on my button or its label?
Or, because the actual question is how to rotate a QPushButton?
Mru, here below is some C ++ example that completely completes QPushButton. Since I have no idea about C ++, and since I really don't need a complete re-implementation, I tried to override the handler painEvent()in Python based on this example.
Here is what I translated, but it does not work: \
from PyQt4 import QtGui, QtCore
import sys
class RotatedButton(QtGui.QPushButton):
def __init__(self, text, parent, orientation = "west"):
QtGui.QPushButton.__init__(self, text, parent)
self.orientation = orientation
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
if self.orientation == 'west':
painter.rotate(90)
elif self.orientation == 'east':
painter.rotate(270)
else:
raise TypeError
painter.drawControl(QtGui.QStyle.CE_PushButton, self.getSyleOptions())
def getSyleOptions(self):
options = QtGui.QStyleOptionButton()
options.initFrom(self)
size = options.rect.size()
size.transpose()
options.rect.setSize(size)
options.features = QtGui.QStyleOptionButton.None
options.text = self.text()
options.icon = self.icon()
options.iconSize = self.iconSize()
return options
class Main(QtGui.QFrame):
def __init__(self):
QtGui.QFrame.__init__(self)
self.count = 0
self.application = QtCore.QCoreApplication.instance()
self.layout = QtGui.QHBoxLayout()
self.button = RotatedButton("Hello", self, orientation="west")
self.layout.addWidget(self.button)
self.setLayout(self.layout)
if __name__ == '__main__':
application = QtGui.QApplication(sys.argv)
application.main = Main()
application.main.show()
sys.exit(application.exec_())