QWidgetAction 不显示菜单

QWidgetAction not showing menu

我创建了一个在 QMenu 中使用的自定义小部件。用户单击 Block 矩形显示的菜单,如下图所示。但是,当我更改菜单分配给控件的方式时,它由于某种原因停止工作。

但是由于某些原因,当我使用这段代码时菜单没有显示:

menu_colors = QtGui.QMenu('Colors')
menu_colors.addAction(colAction)
self.ui_color_filter.setMenu(menu_colors)

而不是这个:

fileMenu = menubar.addMenu('&File')
fileMenu.addAction(colAction)
self.ui_color_filter = ColorBlock()
self.ui_color_filter.setMenu(fileMenu)

下面是完整的工作示例。只是 un-comment 创建我要解决的问题的代码。您会注意到,单击 UI.

主要部分的黑色大矩形时,菜单将不再出现
import sys
from PySide import QtGui, QtCore


class ColorBlock(QtGui.QPushButton):

    colorClicked = QtCore.Signal(QtGui.QColor)

    def __init__(self, *args, **kwargs):
        super(ColorBlock, self).__init__(*args, **kwargs)
        self.setAutoFillBackground(True)
        self.pressed.connect(self.color_clicked)

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.fillRect(0, 0, self.width(), self.height(), QtGui.QColor(0, 0, 0, 255))
        painter.end()

    def color_clicked(self):
        self.colorClicked.emit(QtGui.QColor())


class ColorBlocks(QtGui.QWidget):

    colorSelected = QtCore.Signal(QtGui.QColor) 

    def __init__(self, parent=None):
        super(ColorBlocks, self).__init__(parent)
        lay_main = QtGui.QGridLayout(self)
        lay_main.setSpacing(5)
        lay_main.setContentsMargins(5,5,5,5)

        row = 0
        column = 0
        for i in range(10):
            ui_swatch = ColorBlock()
            lay_main.addWidget(ui_swatch, row, column)
            ui_swatch.colorClicked.connect(self.colorSelected)

            column += 1
            if column == 5: 
                row += 1
                column = 0


class Example(QtGui.QMainWindow):
    def __init__(self):
        super(Example, self).__init__()

        ql = ColorBlocks()

        colAction = QtGui.QWidgetAction(self)
        colAction.setDefaultWidget(ql)
        ql.colorSelected.connect(self.clicked_color)
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(colAction)


        self.ui_color_filter = ColorBlock()
        self.ui_color_filter.setMenu(fileMenu)

        # menu_colors = QtGui.QMenu('Colors')
        # menu_colors.addAction(colAction)
        # self.ui_color_filter.setMenu(menu_colors)

        lay_main = QtGui.QVBoxLayout()
        lay_main.setAlignment(QtCore.Qt.AlignTop)
        lay_main.addWidget(self.ui_color_filter)
        widget_main = QtGui.QWidget()
        widget_main.setLayout(lay_main)
        self.setCentralWidget(widget_main)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Menubar')    
        self.show()

    def clicked_color(self, color):
        print('Clicked:', color.isValid(), color)
        self.ui_color_filter.color = color
        self.sender().parent().hide()


app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

您不能在多个 QMenu 中共享相同的 QAction,因此您必须为主窗口小部件的 QMenuQMenuBar 创建一个,另一个为self.ui_color_filter 的菜单。另一方面,menu_colors 你必须传递父级,因为如果不删除。

class Example(QtGui.QMainWindow):
    def __init__(self):
        super(Example, self).__init__()

        ql = ColorBlocks()

        colAction = QtGui.QWidgetAction(self)
        colAction.setDefaultWidget(ql)
        ql.colorSelected.connect(self.clicked_color)
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(colAction)

        self.ui_color_filter = ColorBlock()
        ql1 = ColorBlocks()
        colAction1 = QtGui.QWidgetAction(self)
        colAction1.setDefaultWidget(ql1)
        ql1.colorSelected.connect(self.clicked_color)
        menu_colors = QtGui.QMenu('Colors', self)
        menu_colors.addAction(colAction1)
        self.ui_color_filter.setMenu(menu_colors)

        lay_main = QtGui.QVBoxLayout()
        lay_main.setAlignment(QtCore.Qt.AlignTop)
        lay_main.addWidget(self.ui_color_filter)
        widget_main = QtGui.QWidget()
        widget_main.setLayout(lay_main)
        self.setCentralWidget(widget_main)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Menubar')    
        self.show()