Creating rounded QPushButton

2,942 views
Skip to first unread message

kiteh

unread,
Dec 17, 2018, 8:35:23 PM12/17/18
to Python Programming for Autodesk Maya

I am trying to create a rounded QPushButton in pythonic format.

Read some solutions which are in C++ and while trying to convert them in Python terms, it does not seems to be working.


btn = QtGui.QPushButton()
btn.setAttribute(QtCore.Qt.WA_TranslucentBackground)
btn.setStyleSheet(
    "border: 1px solid black;"
    "border-radius: 150px;"
    "color: lightGray; "
    "font-size: 35px;"
)

There are indeed some changes effected on the button but I am still seeing sharp edges (squarish).

Wondering if anyone has attempt this?

P.S - If possible, I would not want to create a class just for the sake of creating a circular button

Justin Israel

unread,
Dec 17, 2018, 9:40:35 PM12/17/18
to python_in...@googlegroups.com
The 'border-radius' value is going to be relative to the size of the button, so if you set it too high (as you do in this example), it ends up just drawing a square. You will notice that if you resize the button in different dimensions it can end up drawing rounded corners. But overall I would say using 'border-radius' is not going to be a good way to achieve a completely circular button. I can think of a number of different ways to accomplish this goal:

  1. Custom QAbstractButton class with a paintEvent that draws a circle
  2. A QPushbutton with a stylesheet using "border-image: url("/path/to/image.png") 0 0 0 0 stretch stretch;", although this looks kind of awful with the aliasing on the drawing
  3. Flat QPushButton with an icon that has a pre-made circle graphic
I can give an example of option 3:

# This could either be a file on disk,
# or a :/resource/file.png
# or you could use QPixmap and QPainter to draw the graphic dynamically
ico = QtGui.QIcon("/path/to/circle.png")

btn = QtGui.QPushButton()
btn.setIcon(btn)
btn.setFixedSize(64, 64)
btn.setIconSize(btn.size())
btn.setFlat(True)
btn.setStyleSheet("""
    border: 0;
    color: lightGray;
    font-size: 35px;
""")

This will give you a button that has an icon set to the same size as the fixed size. Icons won't automatically scale unless you create a sub-class, so you have to go with just a fixed button size.

Justin

 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/04810283-74c7-4a36-b938-b5ae19bf4c54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kiteh

unread,
Dec 18, 2018, 9:07:16 PM12/18/18
to Python Programming for Autodesk Maya
Got it, thanks!
Reply all
Reply to author
Forward
0 new messages