[Moved] How to rotate QPushButton?



  • I would like to rotate a QPushButton (or at least its text) so it can stand vertically. I've seen "some documentation online":http://developer.qt.nokia.com/faq/answer/how_can_i_draw_vertical_text , but I couldn't make much sense out of it (it's in C and I'm a Python user).

    So I understood I need to re-implement the paintEvent() handler and rotate the QPainter(). What I can't figure out however is how to do this for the QString or QPushButton I need only. I supposed 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?

    Thanks.



  • If you want the result like in the "doc":http://developer.qt.nokia.com/faq/answer/how_can_i_draw_vertical_text you mentioned,
    ie,

    t
    e
    x
    t

    @QString tmp = "t\ne\nx\nt";
    ui->pushButton->setText(tmp);@ this would also do. \n after every char ;)

    I know, its a dirty workaround, nevermind ;)



  • Thanks, but I want rotation :)

    On StackOverflow, I've been pointed to this doc: http://www.qtcentre.org/wiki/index.php?title=OrientationButton

    Which is also in C. Now I'll try something from there. I realise I need to reimplement the QPushButton's paintEvent and not the main widget's :D



  • Yes, you have to reimplement the PushButtons paint event. Its clear in that example. Actually, i believe, the "link":http://www.qtcentre.org/wiki/index.php?title=OrientationButton solves all your problems!?



  • Yes, but it doesn't solve the fact I'm C-illiterate though :D
    I will try my best and come back if I need help for Pythonic translation.



  • Well, then you are talking to a Python-illiterate!! Good luck anyway :)



  • Hoping someone here is familiar with PyQt4, so far I tried the following, which failed:

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

    @

    Any suggestion?



  • Check out LibQxt. It supports rotated buttons in its "QxtPushButton":http://libqxt.bitbucket.org/doc/0.6/qxtpushbutton.html class.

    P.S. Note that the suff you refered to is in C++, not in C. There is quite a big difference.



  • Thanks Andre. Are there Python bindings for this too?

    [quote author="Andre" date="1315469791"]Check out LibQxt. It supports rotated buttons in its "QxtPushButton":http://libqxt.bitbucket.org/doc/0.6/qxtpushbutton.html class.

    P.S. Note that the suff you refered to is in C++, not in C. There is quite a big difference. [/quote]



  • I got help from StackOverflow, and the "above example":http://www.qtcentre.org/wiki/index.php?title=OrientationButton was translated to Python properly. Now I need to figure out what it's really doing :)

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

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.