python - How to display color icons in menu, PyQt? -


what i'm trying achieve display color icons in context menu, in python. displays of used colors mark table.

here example of i'm trying achieve.

enter image description here

any guess how ?

update here more detailed version of vision.

on clicking mark , sub menu opens color options.

enter image description here

you can using qwidgetaction.

the example code below uses tool-buttons icons grid of colors, there many other widgets be used. palette method can reimplemented if need different set of colors.

class coloraction(qtgui.qwidgetaction):     colorselected = qtcore.pyqtsignal(qtgui.qcolor)      def __init__(self, parent):         qtgui.qwidgetaction.__init__(self, parent)         widget = qtgui.qwidget(parent)         layout = qtgui.qgridlayout(widget)         layout.setspacing(0)         layout.setcontentsmargins(2, 2, 2, 2)         palette = self.palette()         count = len(palette)         rows = count // round(count ** .5)         row in range(rows):             column in range(count // rows):                 color = palette.pop()                 button = qtgui.qtoolbutton(widget)                 button.setautoraise(true)                 button.clicked[()].connect(                     lambda color=color: self.handlebutton(color))                 pixmap = qtgui.qpixmap(16, 16)                 pixmap.fill(color)                 button.seticon(qtgui.qicon(pixmap))                 layout.addwidget(button, row, column)         self.setdefaultwidget(widget)      def handlebutton(self, color):         self.parent().hide()         self.colorselected.emit(color)      def palette(self):         palette = []         g in range(4):             r in range(4):                 b in range(3):                     palette.append(qtgui.qcolor(                         r * 255 // 3, g * 255 // 3, b * 255 // 2))         return palette  class colormenu(qtgui.qmenu):     def __init__(self, parent):         qtgui.qmenu.__init__(self, parent)         self.coloraction = coloraction(self)         self.coloraction.colorselected.connect(self.handlecolorselected)         self.addaction(self.coloraction)         self.addseparator()         self.addaction('custom color...')      def handlecolorselected(self, color):         print(color.name()) 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -