PDA

View Full Version : How to create hexagonal shaped pushButton in pyqt4?



femy
1st December 2015, 09:07
Currently i am using border-image to get the shape.I want to change color of hexagon dynamically. if i try to change the color of hexagon using background-color, only the square back-ground color is changing.


11548


class ExampleApp(QtGui.QMainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.myFunction()

def myFunction(self):
self.newWindow = design_3.Ui_MainWindow()
self.newWindow.setupUi(self);
self.newWindow.campusNameLabel.setStyleSheet("QLabel { color : white; }");
self.newWindow.campusNameLabel.setText("Label 1")


gridLayout = QtGui.QGridLayout()
gridLayout.setSpacing(5)
gridLayout.setRowStretch(1,1)
gridLayout.setVerticalSpacing(20)


alertBtn = QtGui.QPushButton("myBtn",self)
alertBtn.setAutoFillBackground(True)
alertBtn.setFlat(True)
alertBtn.setSizePolicy(QtGui.QSizePolicy.Preferred , QtGui.QSizePolicy.Preferred)
alertBtn.setMaximumWidth(150)
alertBtn.setMaximumHeight(150)
alertBtn.setMinimumWidth(150)
alertBtn.setMinimumHeight(150)


alertBtn.setStyleSheet("border-image: url(rsz_imagetest.png);background-color:black;")
gridLayout.addWidget(alertBtn,0,0)
self.newWindow.verticalLayout_2.addLayout(gridLayo ut)

ChrisW67
1st December 2015, 20:02
The colour comes from your image. If you want to change the colour you will need to change the image.

Another approach would be to subclass QPushbutton and provide it a paint() function that draws the button with QPainter (using a member variable to hold the desired button colour).

femy
2nd December 2015, 09:19
how can i make the corners of this hexagon rounded ?

yeye_olive
2nd December 2015, 09:46
how can i make the corners of this hexagon rounded ?
By opening rsz_imagetest.png in your favorite image editor?