私が達成しようとしているのは、Python のコンテキスト メニューにカラー アイコンを表示することです。テーブルをマークするために使用される色の一部が表示されるようにします。
これが私が達成しようとしているものの例です。
これを行う方法を推測しますか?
更新 これは私のビジョンのより詳細なバージョンです。
Mark をクリックすると、サブメニューが開き、色のオプションが表示されます。
QWidgetActionを使用すると、これをかなり簡単に行うことができます。
以下のコード例では、色のグリッドにアイコン付きのツール ボタンを使用していますが、他にも同じように簡単に使用できるウィジェットが多数あります。別の色のセットが必要な場合は、このpalette
メソッドを再実装できます。
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)
for row in range(rows):
for 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 = []
for g in range(4):
for r in range(4):
for 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())