我希望---用Python和Qt4 ---来旋转QPushButton(或至少它的文本),使它可以垂直竖立。我见过some documentation online,但我无法理解它 - 它在C中,而且我是C-illiterate。
从我读过的内容来看,需要重新实现paintEvent()处理程序,实例化并旋转QPainter()。然而,我无法弄清楚如何为我只需要的一个QString或QPushButton做这个。我假设QPaintEvent会有一个“发送者”属性,就像信号一样,但它没有。我似乎可以从这个事件得到的是QRect或QRegion。
如何查找特定于我的按钮或其标签的事件?
或者,因为这是真正的问题,如何旋转QPushButton?
Mru,下面提到了some C++ example,它完全重新实现了QPushButton。由于我对C ++没有任何线索,因为我不需要完全重新实现,所以我试图在Python中重新实现painEvent()
处理程序。基于这个例子。
这是我翻译的内容,但它不起作用:\
#!/usr/bin/env python
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_())
答案 0 :(得分:6)
根据您的代码:
#!/usr/bin/env python
from PyQt4 import QtGui, QtCore
import sys
class RotatedButton(QtGui.QPushButton):
def __init__(self, text, parent, orientation = "west"):
super(RotatedButton,self).__init__(text, parent)
self.orientation = orientation
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
painter.rotate(90)
painter.translate(0, -1 * self.width());
painter.drawControl(QtGui.QStyle.CE_PushButton, self.getSyleOptions())
def minimumSizeHint(self):
size = super(RotatedButton, self).minimumSizeHint()
size.transpose()
return size
def sizeHint(self):
size = super(RotatedButton, self).sizeHint()
size.transpose()
return size
def getSyleOptions(self):
options = QtGui.QStyleOptionButton()
options.initFrom(self)
size = options.rect.size()
size.transpose()
options.rect.setSize(size)
options.features = QtGui.QStyleOptionButton.None
if self.isFlat():
options.features |= QtGui.QStyleOptionButton.Flat
if self.menu():
options.features |= QtGui.QStyleOptionButton.HasMenu
if self.autoDefault() or self.isDefault():
options.features |= QtGui.QStyleOptionButton.AutoDefaultButton
if self.isDefault():
options.features |= QtGui.QStyleOptionButton.DefaultButton
if self.isDown() or (self.menu() and self.menu().isVisible()):
options.state |= QtGui.QStyle.State_Sunken
if self.isChecked():
options.state |= QtGui.QStyle.State_On
if not self.isFlat() and not self.isDown():
options.state |= QtGui.QStyle.State_Raised
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.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_())
答案 1 :(得分:2)
如果您希望此代码适用于" east"和" west"您应该使用的方向参数:
ini_set('xdebug.max_nesting_level', 200);
答案 2 :(得分:1)
我知道这是在原帖后几年,但如果你只想让文字是垂直的而不是水平的,那么这是有效的。使用QTextDocument和一些HTML。添加
<br>
每封信后。
QTextDocument doc;
doc.setHtml("<p align=center><font>B<br>u<br>t<br>t<br>o<br>n</font></p>");
doc.setTextWidth(doc.size().width());
QPixmap pixmap(doc.size().width(), doc.size().height());
pixmap.fill( Qt::transparent );
QPainter painter(&pixmap);
doc.drawContents(&painter);
QPushButton button;
button->setIconSize(pixmap.size());
button->setIcon(pixmap);