简体   繁体   中英

How to rotate PyQt5 QPushButton without using paintEvent?

Or just text in it. Is it possible?

I found implementation with paintEvent How to rotate a QPushButton? , but it's not very 'pythonic' in my opinion.

I'm not sure you can, but it's possible to emulate the effect by using a mask over a button and emulating the foreground with a bitmap. For example:

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets

    class MyFrame(QtWidgets.QFrame):
        def __init__(self, parent=None,initials=None):
            QtWidgets.QFrame.__init__(self, parent)
            self.btn = QtWidgets.QPushButton(self)
            self.btn.setIcon(QtGui.QIcon('rotatedBitmap.png'))
            self.btn.setMask(QtGui.QBitmap('rotatedBitmapMask.png'))
            self.btn.setIconSize(QtCore.QSize(156,166))
            self.btn.move(30,30)
            self.btn.resize(156,166)
            self.btn.clicked.connect(self.printthis)

        def printthis(self):
            print('this')

    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        Frame = MyFrame(None)
        Frame.resize(720,600)
        Frame.show()
        app.exec_()

This was a fast example and I'm not sure if I can use use the same bitmap strictly as both Foreground and Mask (in fact I'm not sure about the rules for the mask) but it should work. Use the following image as test.

按钮的旋转像素图

and the following as Mask.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM