简体   繁体   中英

Context menu disappears if PySide app is running from IPython interactive console

In the minimal example given below, the context menu (right click on white section of the gui) is displayed only briefly and then disappears. This is the case if the app is started from the IPython (0.13.1) console. When started normally from shell it works as it should.

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)

I believe this is IPython Issue #2380 , which should be fixed in current git master.

If you want your app to workaround this bug in 0.13, you will have to ship with your own code a version of IPython/lib/inputhookqt4.py from master, and monkeypatch IPython with:

from IPython.lib import inputhook
inputhook.enable_qt4 = my_enable_qt4

OR, alternatively, just override create_inputhook_qt4

from IPython.lib import inputhookqt4
inputhookqt4.create_inputhook_qt4 = my_create_inputhook_qt4

I expect both approaches should work, as long as you do it before %gui qt is called.

How do you run the application ? In[1]: %run file.py ?

I can't reproduce it on ~last master (feb 4 f46bfec 08389b4) with OS X

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM