0

I have a QPushButton with an Icon. By default the Icon is placed to the left of the button text like this:

enter image description here

Using a QToolButton I can achieve the desired behavior with: button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)

is there a similar straightforward approach to do this for a QPushButton?

4
  • No, there isn't. While you could attempt to use the button as a container with its own layout, adding a widget for the icon (possibly using QLabel or a custom QWidget displaying the QIcon) and the label as an actual QLabel, the effort would be quite huge (and unreliable) considering the possible related issues, such as styling problems, proper layout management, icon size and display, etc. What is your reason for avoiding QToolButton? It seems like you're having an XY problem. Commented Jun 21, 2024 at 4:51
  • My hesitation from using a QToolButton is that its supposed to be used for tool bars, and I want to add this button to my main layout. Additionally, I want my button to appear flat and Tool Button doesn't have the method .setFlat() However, if using a ToolButton in my main GUI doesn't cause any problems? I will just use that. Thanks! Commented Jun 21, 2024 at 5:16
  • 1
    From docs: "It is also possible to construct tool buttons in the same way as any other widget, and arrange them alongside other widgets in layouts." Commented Jun 21, 2024 at 7:43
  • 2
    QToolButton has setAutoRaise as an alternative to setFlat. Despite the name, this widget wasn't designed only for use with tool-bars - it has many other common uses apart from that (e.g. in combination with a line-edit for selecting files). That's why it's called a tool-button, rather than a toolbar-button. Commented Jun 21, 2024 at 8:35

1 Answer 1

0

You can use QToolButton, it work's. in QToolButton you can set icon position.

self.my_button = QToolButton(self)
self.my_button.setText("Hello!")
self.my_button.move(10, 10)
self.my_button.setIcon(QIcon("my_icon.png"))
self.my_button.setFixedSize(40, 40)
self.my_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.