PDA

View Full Version : Custom QPushButton shape



Mate de Vita
24th April 2017, 13:21
I'm trying to create a very simple pentagonal custom button in Qt. I'm using PyQt5, but the concepts and style-sheet code should be the same as C++ anyway.

My current code is this:

button = QPushButton()
button.setFixedSize(QSize(20, 15))
button.setSizePolicy(QSizePolicy(QSizePolicy.Fixed , QSizePolicy.Fixed))
x = button.width()
y = button.height()
points = [QPoint(0, 0), QPoint(x/2, 0), QPoint(x, y/2), QPoint(x/2, y), QPoint(0, y)]
button.setMask(QRegion(QPolygon(points)))

This almost works great, but unfortunately it ruins the dropshadow effect and the button just ends up looking weird: https://i.stack.imgur.com/tXsgF.png

Removing the border with


button.setStyleSheet("border-style: none")

doesn't work well because then the button is indistinguishable from the background. What I'm currently using is


button.setStyleSheet("background-color: darkBlue; border-style: none")

but this makes the button blue (which is not ideal) and it also removes the colour changes when hovering or clicking the button. Ideally, I would like to have


button.setStyleSheet("border-style: outset")

and keep colour changing functionality you'd expect from a button. However, the above line actually does the same as `border-style: none`, simply removing the border. Is there a way to get an outset border or at least fix the dropshadow effect of the original button?