Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Moved] How to rotate QPushButton?
Forum Updated to NodeBB v4.3 + New Features

[Moved] How to rotate QPushButton?

Scheduled Pinned Locked Moved Language Bindings
10 Posts 3 Posters 15.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jesuisbenjamin
    wrote on 7 Sept 2011, 19:39 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on 7 Sept 2011, 19:59 last edited by
      #2

      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 ;)


      Declaration of (Platform) independence.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jesuisbenjamin
        wrote on 7 Sept 2011, 20:05 last edited by
        #3

        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

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rahul Das
          wrote on 7 Sept 2011, 20:11 last edited by
          #4

          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!?


          Declaration of (Platform) independence.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jesuisbenjamin
            wrote on 7 Sept 2011, 20:13 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rahul Das
              wrote on 7 Sept 2011, 20:16 last edited by
              #6

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


              Declaration of (Platform) independence.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jesuisbenjamin
                wrote on 7 Sept 2011, 21:26 last edited by
                #7

                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?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on 8 Sept 2011, 08:16 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jesuisbenjamin
                    wrote on 8 Sept 2011, 10:12 last edited by
                    #9

                    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]

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jesuisbenjamin
                      wrote on 8 Sept 2011, 10:14 last edited by
                      #10

                      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_())
                      

                      @

                      1 Reply Last reply
                      0

                      1/10

                      7 Sept 2011, 19:39

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved