0

目前我正在使用边框图像来获得六边形。我想动态改变六边形的颜色。如果我尝试使用背景颜色更改六边形的颜色,则只有方形背景颜色会发生变化。

这是当前代码和结果。我的要求是得到一个像这样的黑色六边形:

数字

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(gridLayout)
4

1 回答 1

1

我不知道我是否正确理解了您的问题,但这是一个使用自定义类的版本。可以根据需要进行修改。部分代码来自这里 在此处输入图像描述

import sys
from PyQt4 import QtCore, QtGui
class HexButton(QtGui.QPushButton):
    size = 100
    x = (3**0.5 / 2)
    font = QtGui.QFont('Arial', size*0.08)
    font.setWeight(500)
    font.setStyleStrategy(QtGui.QFont.PreferAntialias)
    hexaPoints = [QtCore.QPoint(size/4,0),
                    QtCore.QPoint(size/4 + size/2,0),
                    QtCore.QPoint(size,size*0.5*x),
                    QtCore.QPoint(size/4 + size/2,size*x),
                    QtCore.QPoint(size/4,size*x),
                    QtCore.QPoint(0,size*0.5*x)]
    hexaPointsF = [QtCore.QPointF(size/4,0),
                    QtCore.QPointF(size/4 + size/2,0),
                    QtCore.QPointF(size,size*0.5*x),
                    QtCore.QPointF(size/4 + size/2,size*x),
                    QtCore.QPointF(size/4,size*x),
                    QtCore.QPointF(0,size*0.5*x)]
    hexa = QtGui.QPolygon(hexaPoints)
    hexaF = QtGui.QPolygonF(hexaPointsF)

    def __init__(self, parent=None):
        QtGui.QPushButton.__init__(self)
        self.setMinimumSize(HexButton.size + 10, HexButton.size + 10)
        self.text = None
        self.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        self.color = QtGui.QColor(241, 186, 82)
        self.setFlat(True)

    def setText(self, text):
        self.text = text
        self.update()

    def setColor(self, r,g,b):
        self.color = QtGui.QColor(r,g,b)
        self.update()

    def paintEvent(self, event):
        qp = QtGui.QPainter()
        qp.begin(self)
        opt = QtGui.QStyleOptionButton()
        self.initStyleOption(opt)
        self.style().drawControl(QtGui.QStyle.CE_PushButton, opt, qp, self)
        qp.end()
        painter = QtGui.QPainter()
        painter.begin(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setPen(QtCore.Qt.NoPen)
        painter.setBrush(self.color)
        basePoly = QtGui.QPolygonF(HexButton.hexaF)
        ss = 0.98
        basePoly.translate(QtCore.QPointF(HexButton.size*(1.03-ss)*0.8, HexButton.size*(1.19-ss)*0.6*HexButton.x))
        painter.drawPolygon(basePoly)
        plist = HexButton.hexaPointsF + [HexButton.hexaPointsF[0]]
        s = 0.95
        painter.translate(QtCore.QPointF(HexButton.size*(1.03-s)*0.8, HexButton.size*(1.2-s)*0.6*HexButton.x))
        painter.scale(s, s)
        painter.setPen(QtGui.QPen(QtGui.QColor(0, 0, 0), (HexButton.size*0.016)*(1/s)))
        painter.drawPolyline(*plist)
        painter.resetTransform()
        if self.text:
            pen_text = QtGui.QPen()
            pen_text.setBrush(QtGui.QColor(0,0,0))
            painter.setPen(pen_text)
            painter.setFont(HexButton.font)
            painter.drawText(0, 0, HexButton.size+10, HexButton.size*HexButton.x+20, QtCore.Qt.AlignCenter, self.text)
        painter.end()


class MainDialog(QtGui.QMainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        centralwidget = QtGui.QWidget(self)
        self.layout = QtGui.QHBoxLayout(centralwidget)
        self.button = HexButton()
        self.button.setText("Foooooo")
        self.anotherButton = HexButton()
        self.anotherButton.setText("Barrr")
        self.anotherButton.setColor(255, 102, 102)
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.anotherButton)
        self.setCentralWidget(centralwidget)


def main():
     app = QtGui.QApplication(sys.argv)
     form = MainDialog()
     form.show()
     app.exec_()

if __name__ == '__main__':
     main()
于 2015-12-01T23:47:31.047 回答