一半君的总结纸

听话只听一半君

右键菜单Context Menu 弹出的三种方法

官网很详细了,这里总结记录一下

  • Subclass widget 然后reimplement 他的 contextMenuEvent()
  • 给 widget 添加actions, 把 context menu policy 设成 ActionsContextMenu.
  • 把context menu policy 设成 CustomContextMenu, 然后把widget的 customContextMenuRequested() signal 连到一个slot上,在这个slot里创建menu
方法一,subclass widget,reimplement contextMenuEvent()
import os
import sys
from PyQt4 import QtGui, QtCore, uic
import sip


class MyWindow(QtGui.QDialog):

    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.layout = QtGui.QVBoxLayout()
        self.widget = QtGui.QListWidget()
        self.layout.addWidget(self.widget)
        self.setLayout(self.layout)
        self.resize(300,200)

    # 当按右键的时候,这个event会被触发
    def contextMenuEvent(self, event):

        menu = QtGui.QMenu(self)
        quitAction = menu.addAction("Quit")
        action = menu.exec_(self.mapToGlobal(event.pos()))
        if action == quitAction:
            QtGui.qApp.quit()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())
方法二,subclass widget,reimplement contextMenuEvent()
import os
import sys
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtCore import Qt
import sip


class MyWindow(QtGui.QDialog):

    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.layout = QtGui.QVBoxLayout()
        self.widget = QtGui.QListWidget()
        self.layout.addWidget(self.widget)
        self.setLayout(self.layout)
        self.resize(300,200)
        
        self.setContextMenuPolicy(Qt.ActionsContextMenu)
        # 直接把action加到自身上
        quitAction = QtGui.QAction("Quit", self)
        quitAction.triggered.connect(QtGui.qApp.quit)
        self.addAction(quitAction)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

方法三,使用customContextMenuRequested signal
import os
import sys
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtCore import Qt
import sip


class MyWindow(QtGui.QDialog):

    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.layout = QtGui.QVBoxLayout()
        self.widget = QtGui.QListWidget()
        self.layout.addWidget(self.widget)
        self.setLayout(self.layout)
        self.resize(300, 200)

        self.widget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.widget.customContextMenuRequested.connect(self.openMenu)

    def openMenu(self, position):

        menu = QtGui.QMenu()
        quitAction = menu.addAction("Quit")
        action = menu.exec_(self.widget.mapToGlobal(position))
        if action == quitAction:
            QtGui.qApp.quit()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

参考:
Handling context menus

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: