python - 如何旋转 QPushButton?

标签 python user-interface pyqt4 qpainter

我想用 Python 和 Qt4 来旋转 QPushButton (或至少它的文字)所以它可以垂直站立。我看过some documentation online , 但我无法从中得到太多意义——它是用 C 写的,而我是 C 文盲。

不过根据我的阅读,需要重新实现 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_())

最佳答案

根据您的代码:

#!/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_())

关于python - 如何旋转 QPushButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7339685/

相关文章:

java - 子类调用父类的方法

qt - QTreeWidgetItem 内的 QComboBox

python - 如何覆盖 pyqt4 QGraphicsView.mousePressEvent?

python - 如何在 pyqt 中同时运行 2 个线程?

python - 在flask的celery文档中,为什么celery任务需要名称?

python - 用 python 解决一个 3x3 Frog 谜题

cocoa - Apple 的 UI 是否在菜单和/或菜单项的上下文菜单中定义了拖放功能

java - 在 Swing 事件监听器中阻止调用

python - 如何在 Pandas 中获得同一条船连续安装的最旧部分安装?

python - PyQt 声子音频播放器