Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6560265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:26:15+00:00 2026-05-25T13:26:15+00:00

I would like—with Python and Qt4—to rotate a QPushButton (or at least its text)

  • 0

I would like—with Python and Qt4—to rotate a QPushButton (or at least its text) so it can stand vertically. I’ve seen some documentation online, but I couldn’t make much sense out of it—it’s in C and I’m C-illiterate.

From what I read though, one needs to re-implement the paintEvent() handler, instantiate and rotate a QPainter(). What I can’t figure out however is how to do this for the one QString or QPushButton I need only. I assumed the QPaintEvent would have a “sender” attribute, like signals do, but it hasn’t. All I can seem to get from this event is a QRect or QRegion.

How can I find out the event specific to my button or its label?
Or, because that’s the question really, how to rotate a QPushButton?

Mru, here below suggested some C++ example, which reimplements the QPushButton completely. Since I have no clue about C++ and since I don’t really need a full reimplementation, I’ve tried to reimplement the painEvent() handler in Python, based on that example.

Here is what I have translated, but it does not work :\

#!/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_())
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:26:15+00:00Added an answer on May 25, 2026 at 1:26 pm

    Based on your code:

    #!/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_())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to hide some text inside a div-block, but I don't want to
Would like to maintain a map/hash of DOM objects. Can they serve as key
Would like to see if someone can help me on figuring out how to
would like to know how can this be implemented in Joomla. I have a
Would like to know options in 3D graphics engines out there that can even
would like to build an app that can run on any of the new
Would like to see how they've implemented their widget (at least - widget layout).
Would like to know what is the best F/OSS IDE for Python Web development.
Would like to set cookies from the server (CGI - C) Can it be
Would like opinions on the best way to go. As you can see int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.