Skip to content

getopenfilename does not block when when using ipython inputhook/start_event_loop #7301

@triccare

Description

@triccare

This is very similar to #2380. However, since that issue, and related ones, are all closed, I'm presenting this similar issue, which has already been posted on Stack Overflow

Note: If not using the native dialog option, all works fine. This is just when using the native dialog.

When trying to run Qt in iPython, using the inputhook and start_event_loop, to create non-blocking app, the function PyQt4.QtGui.QFileDialog.getOpenFileName does not allow for input. It pops up and immediately closes.

The code below demonstrates the behavior. If run through _main(), which starts the Qt eventloop normally, all works fine. Both the generic Dialog and FileDialog operate as expected.

However, when run as

$ ipython
In [1]: import ipython_nonblock_example as nb
In [2]: nb_instance = nb.start_gui(nb.Example)

the Dialog works as expected. But the FileDialog does the open/close behavior.
Versions are: IPython 2.3.1, Qt 4.8, PyQt4 4.11.1,Python 2.7.5

File ipython_nonblock_example.py

import sys
from PyQt4 import QtGui, QtCore

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

        self.initUI()

    def initUI(self):
        self.btn = QtGui.QPushButton('Dialog', self)
        self.btn.move(20, 20)
        self.btn.clicked.connect(self.showDialog)

        self.btn2 = QtGui.QPushButton('File', self)
        self.btn2.move(20,50)
        self.btn2.clicked.connect(self.showFile)

        self.le = QtGui.QLineEdit(self)
        self.le.move(130, 22)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Input dialog')
        self.show()

    def showDialog(self):
        text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
            'Enter your name:')

        if ok:
            self.le.setText(str(text))

    def showFile(self):
        file_name = str(QtGui.QFileDialog.getOpenFileName(parent=self, caption='Open file'))

        if file_name:
            self.le.setText(str(file_name))

def start_gui(ui, block=False):
    try:
        if not block & __IPYTHON__:
            from IPython.lib.inputhook import enable_gui
            app = enable_gui('qt4')
        else:
            raise ImportError
    except (ImportError, NameError):
        app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)

    ui_instance = ui()

    try:
        from IPython.lib.guisupport import start_event_loop_qt4
        start_event_loop_qt4(app)
        return ui_instance
    except ImportError:
        app.exec_()

def _main():
    start_gui(Example, block=True)

if __name__ == '__main__':
    _main()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions