PySide QPushButton 右键单击​​上下文菜单未分配每个按钮

Posted

技术标签:

【中文标题】PySide QPushButton 右键单击​​上下文菜单未分配每个按钮【英文标题】:PySide QPushButton Right-click context menu not assigning per button 【发布时间】:2017-02-24 08:05:14 【问题描述】:

我有一个循环生成带有上下文菜单的 QPushButtons,并根据在目录中的图像中找到的文本填充具有不同值的子菜单(最后一部分不重要)

for self.preview in previewFiles:

self.image = self.dir_path+"/"+self.preview
self.btn = QPushButton()
self.btn.setContextMenuPolicy(Qt.CustomContextMenu)
self.btn.customContextMenuRequested.connect(self.on_context_menu)
self.btn.setMinimumSize(360, 360)
self.btn.setMaximumSize(360, 360)
self.btn.setStyleSheet("background-image: url(" + self.image + ");border:1px solid #333;text-align: bottom center;font: bold 12px;")
#popup menu
self.popMenu = QMenu(self)

# Resolution Drop down
res = QMenu("Resolution")
for reso in albedoRes: # Add resolutions to sub menu based on image res found
    if "256" in reso:
        res.addAction(QAction('256', self, checkable=True))
    if "512" in reso:
        res.addAction(QAction('512', self, checkable=True))
    if "1K" in reso:
        res.addAction(QAction('1024', self, checkable=True))
    if "2K" in reso:
        res.addAction(QAction('2048', self, checkable=True))
    if "4K" in reso:
        res.addAction(QAction('4096', self, checkable=True))
    if "8K" in reso:
        res.addAction(QAction('8192', self, checkable=True))
self.popMenu.addMenu(res)
self.popMenu.addSeparator().setText("Alternate Shaders")
self.popMenu.addAction(QAction('Blinn', self))
self.popMenu.addAction(QAction('Phong', self))
self.popMenu.addSeparator()
self.popMenu.addAction(QAction('Open file(s) location', self))
#self.btn.setMenu(self.popMenu)
self.a_widget.setLayout(self.a_layout)
self.a_layout.addWidget(self.btn) # add rows to main layout

如您所见,如果在文件名中找到某个字符串,它应该向菜单添加一个操作 - 确实如此

然而,这不是保存每个按钮,而是只是根据下一个图像文件名替换 QAction(所以实际上显示的最终 QAction 只是循环中最后一个图像的那个)

使用 setMenu() 可以正确分配所有内容,但我希望它位于上下文菜单中。

我知道我错过了一些简单的事情,但我很困惑,任何指导都将不胜感激。谢谢。

【问题讨论】:

【参考方案1】:

通过按钮而不是在另一个功能中构建您的菜单:

import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui

class test(QtGui.QDialog):

    def __init__(self):

        super(test, self).__init__()
        self.setObjectName("test")
        self.resize(574, 203)
        self.setWindowTitle("Clone UI")

        self.a_layout = QtGui.QHBoxLayout()

        previewFiles = ['blood-lips-lipstick-makeup-red-lips-Favim.com-75480.jpg', 'mins.jpg']

        # ABOVE CODE WAS PLACEHOLDER

        for preview in previewFiles:

            self.image = '/x/path' + preview
            self.btn = QtGui.QPushButton()
            self.btn._name = preview.split('.')[0] # use for my if/else condition menu


            # create the ocntext
            self.btn.installEventFilter(self)
            self.btn.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
            self.btn.customContextMenuRequested.connect(self.btnClicked)

            self.btn.setMinimumSize(360, 360)
            self.btn.setMaximumSize(360, 360)
            self.btn.setStyleSheet("background-image: url(" + self.image + ");border:1px solid #333;text-align: bottom center;font: bold 12px;")

            #popup menu
            self.popMenu = QtGui.QMenu(self)

            #self.btn.setMenu(self.popMenu)
            self.setLayout(self.a_layout)
            self.a_layout.addWidget(self.btn) # add rows to main layout

    def btnClicked(self, QPos):

        # the function that create the popup menu

        # get the button clicked
        btn = self.sender()

        # define where the menu appear on click
        parentPosition = btn.mapToGlobal(QtCore.QPoint(0, 0))
        menuPosition = parentPosition + QPos

        #clear the menu at each click to customize
        self.popMenu.clear()

        #populate
        self.populateBtnContext(btn._name)

        #show
        self.popMenu.move(menuPosition)
        self.popMenu.show()

    def populateBtnContext(self, name):

        # Do some if here :
        if name == "mins":
            self.popMenu.addAction(QtGui.QAction('256', self, checkable=True))
            self.popMenu.addAction(QtGui.QAction('512', self, checkable=True))
            self.popMenu.addAction(QtGui.QAction('1024', self, checkable=True))
        else:
            self.popMenu.addAction(QtGui.QAction('2048', self, checkable=True))
            self.popMenu.addAction(QtGui.QAction('4096', self, checkable=True))
            self.popMenu.addAction(QtGui.QAction('8192', self, checkable=True))  

        self.popMenu.addSeparator().setText("Alternate Shaders")
        self.popMenu.addAction(QtGui.QAction('Blinn', self))
        self.popMenu.addAction(QtGui.QAction('Phong', self))
        self.popMenu.addSeparator()
        self.popMenu.addAction(QtGui.QAction('Open file(s) location', self))

win = test()
win.show()

【讨论】:

以上是关于PySide QPushButton 右键单击​​上下文菜单未分配每个按钮的主要内容,如果未能解决你的问题,请参考以下文章

(pyQt/pySide)setStyleSheet(border....) 使 QPushButton 在 Maya 中不可点击

在 Windows 上使用 PySide/PyQT 移除 QPushButton 周围奇怪的一像素空间

Maya Pyside2 UI,无法让 QPushButton 与同一类中的函数连接

pyqt pyside QPushButton 图标(icon)大小自适应设置

PyQt5 (Python):通过 contextmenu 从 qpushbutton 获取值

从 QPushButton 设置和获取 QComboBox 值