0

A long time ago, I wanted to make a logo appear on top of the text in a QPushButton stacked on top of each other, but I couldn't find anyway

  • I read some stylesheets (couldn't find a single doc to read it all about all styles I can apply to a button)
  • tried the setLayoutDirection (RightToLeft and LeftToRight were there, but no UpToDown direction)
  • In my (I wish) last attempt I tried to inherit a QAbstractButton (I didn't find QAbstractPushButton, so I guess QAbstractButton is the answer) and change its paintEvent/paintEngine to draw an image or maybe add a vbox inside it as a layout to draw to components, but I can't find anything in python (specially PySide) which has an example in any possible way close to that. The best thing I found was the analogue clock example which was not very helpful because it was trying to work a QWidget and not a QAbstractButton and I want to keep the feel of a Native looking button.

I like my final product to be something like this.

Enaml based example image

source of the implemention of that

Python Enaml toolkit supported this feature out of the box (in one of its widgets), and I know it is QT based, so I really wish to know how it is possible?

p.s.: Also, is there a market for qt widgets? e.g.: a plugin system. Because rewriting an android like switch doesn't seem like the correct thing that I should do! even a good tutorial or doc would be appreicated (excluding official doc)

musicamante
  • 41,230
  • 6
  • 33
  • 58
AARMN
  • 13
  • 3
  • Use [QToolButton](https://doc.qt.io/qt-6/qtoolbutton.html) and [`setToolButtonStyle()`](https://doc.qt.io/qt-6/qtoolbutton.html#toolButtonStyle-prop) with `Qt.ToolButtonTextUnderIcon`. – musicamante May 20 '22 at 16:08
  • Note that if you want a *tool bar*, you should use [QToolBar()](https://doc.qt.io/qt-5/qtoolbar.html) and add [QActions](https://doc.qt.io/qt-5/qaction.html) to it (which is fundamentally what the linked code does). Similarly as above, you must use `setToolButtonStyle()` on that tool bar, which will use the given button style as default for all actions. – musicamante May 20 '22 at 16:19
  • @musicamante Right now I gave it a try, I couldn't make QToolbutton to work, but adding as action worked, although the size of icon is smaller and when I set min height for toolbar didn't help it (the action size kept constent and empty region expanded around it) – AARMN May 20 '22 at 17:22
  • @musicamante My process was I made a QToolButton, and addwidget it to QToolbar, which resulted to it not showing up, I had some issues similar to it with VBox in widget (which is setLayout and not addWidget) if I'm making an idiot mistake like that please mention me – AARMN May 20 '22 at 17:24
  • Please carefully read the *whole* documentation about QToolBar. – musicamante May 20 '22 at 17:52
  • I fixed it, problem was in third party (qt_material) styling package which probably had some specific stylesheets specified height of toolbox. Fixed by addAction (still QToolButton didn't work for me). Thanks for participation, You can submit your comment, so I can accept it as answer – AARMN May 20 '22 at 19:13
  • As said above and explained in the documentation, you shall not need to add a QToolButton to QToolBar (which might also create styling/layout issues and unavailability of the extension button if the toolbar is not set on a main window), but add a QAction, which will properly follow the style inheritance of QToolBar *and* QToolButton according to the style rules, and follow the predefined behavior. When you add a QAction, QToolBar creates its own QToolButtons *internally*, so you don't need to do that. – musicamante May 20 '22 at 20:50

1 Answers1

0

It is easier than you think, you can use QToolButton() like this:

import sys

from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QApplication, QVBoxLayout,QStyle, QWidget, 
QToolButton

class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        
        button = QToolButton()
       
        # here you choose the position of the icon and its text
        button.setToolButtonStyle(
        Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
    
        # here I just use built-in icon by PySide6 for this example
        name = 'SP_DialogSaveButton'
        pixmapi = getattr(QStyle, name)
        icon = self.style().standardIcon(pixmapi)
        
        # here we set text and icon of size 32x32 to the button 
        button.setIcon(icon)
        button.setText("Sample text")
        button.setIconSize(QSize(32, 32))
        
        # finally we add our button to the layout
        lay = QVBoxLayout(self)
        lay.addWidget(button, alignment=Qt.AlignCenter)

if __name__ == "__main__":

    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec())

enter image description here

OualidSai
  • 97
  • 1
  • 5
  • I have one problem with this, how to add it to a QToolBar. Because no matter what I tried, I couldn't get the QToolButton into the Toolbar and only adding action to it (and letting it implicitly creating it for me) worked, I guess that's my only problem with your answer (and overall, my only remaining problem) – AARMN Jun 15 '22 at 05:41