在下面给出的最小示例中,上下文菜单(右键单击 gui 的白色部分)仅短暂显示,然后消失。如果应用程序从 IPython (0.13.1) 控制台启动,就会出现这种情况。当从 shell 正常启动时,它可以正常工作。
import sys
from PySide import QtGui, QtCore
from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4
class ContextTestGui(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_ContextTestWindow()
self.ui.setupUi(self)
self.ui.treeView.addAction(self.ui.actionCopy)
self.ui.treeView.addAction(self.ui.actionShow)
class Ui_ContextTestWindow(object):
def setupUi(self, ContextTestWindow):
ContextTestWindow.resize(200, 100)
self.treeView = QtGui.QTreeView(ContextTestWindow)
self.treeView.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
self.treeView.setMinimumSize(QtCore.QSize(100, 100))
self.actionCopy = QtGui.QAction("Copy",ContextTestWindow)
self.actionShow = QtGui.QAction("Show",ContextTestWindow)
def create_window(window_class,**kwargs):
app = get_app_qt4(sys.argv)
window = window_class()
window.show()
start_event_loop_qt4(app)
return window
if __name__ == '__main__':
simgui = create_window(ContextTestGui)