如何设置QComboBox Popup材质的样式?

时间:2016-02-04 07:41:17

标签: pyside material qcombobox

我想用PySide创建一个材质风格的应用程序 我试图让QComboBox看起来像下面的屏幕:
http://i.stack.imgur.com/UBMTy.png
我现在坚持使用showPopup()函数,我不知道如何重新实现它。 一些提示会很棒。

from PySide import QtGui, QtCore
import sys
class MyComboBox(QtGui.QComboBox):

    def __init__(self, *args, **kwargs):
        QtGui.QComboBox.__init__(self, *args, **kwargs)
        self.grey = QtGui.QColor(115,115,115)
        self.line = QtGui.QColor(210,210,210)

    # def mousePressEvent(self, *args, **kwargs):
    #     pass

    def paintEvent(self, Event):
        self.setMinimumHeight(25)
        self.setMinimumWidth(40)

        self.setMaximumHeight(40)
        self.setMaximumWidth(120)

        offset = self.width() - 12-14

        font = QtGui.QFont()
        font.setPixelSize(12)
        font.setFamily('Arial')

        self.resize(self.parent().width(), self.parent().height())
        painter = QtGui.QPainter()
        painter.begin(self)

        brush = QtGui.QBrush(QtGui.QColor(238, 238, 238), style=QtCore.Qt.SolidPattern)
        painter.fillRect(self.rect(), brush)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)

        brush = QtGui.QBrush(self.line, style=QtCore.Qt.SolidPattern)
        painter.setBrush(brush)
        painter.setPen(QtGui.QColor(119, 194, 187, 0))
        painter.drawRoundedRect(10, self.height()-1, self.width(), 1, 0, 0)

        brush = QtGui.QBrush(self.grey, style=QtCore.Qt.SolidPattern)
        painter.setBrush(brush)
        poly = QtGui.QPolygon([QtCore.QPoint(offset, 18), QtCore.QPoint(offset + 12, 18), QtCore.QPoint(6 + offset, 25), QtCore.QPoint(offset, 18)])
        painter.drawConvexPolygon(poly)

        painter.setFont(font)
        painter.setPen(QtGui.QColor(0, 0, 0))
        painter.drawText(20, 27, self.currentText())

    def showPopup(self):
        QtGui.QComboBox.showPopup(self)

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    widget = QtGui.QWidget()
    widget.setStyleSheet("background-color: rgb(238, 238, 238);\n")
    grid = QtGui.QGridLayout()
    combo = MyComboBox()
    combo.setParent(widget)
    for strItem in ['Hello', 'World']:
        combo.addItem(strItem)

    grid.addWidget(combo, 0, 0)
    widget.setLayout(grid)
    widget.resize(200, 300)
    widget.show()
    combo.show()
    sys.exit(app.exec_())

0 个答案:

没有答案