Pepijn
Pepijn

Reputation: 4253

Why does PySide exit after a file dialog?

I'm writing a simple tray icon application. But after adding a file dialog to a menu action, the whole application exits.

If I run the code on my Mac, it prints the file name, so it does not outright crash. If I replace the call to getOpenFileName with a string, it continues to run.

import sys
from PySide import QtCore, QtGui

def share():
    (filename, _) = QtGui.QFileDialog.getOpenFileName()
    print(filename)

app = QtGui.QApplication(sys.argv)

icon = QtGui.QSystemTrayIcon(QtGui.QIcon('images/glyphicons-206-electricity.png'), app)

menu = QtGui.QMenu()
menu.addAction(QtGui.QAction("Share...", menu, triggered=share))
menu.addAction(QtGui.QAction("Quit", menu, triggered=app.quit))


icon.setContextMenu(menu)
icon.show()

app.exec_()

I'm using Mac OS X 10.10.1, Python 3.4.2, Qt 4.8.6 and PySide 1.2.2

Upvotes: 1

Views: 286

Answers (1)

Meefte
Meefte

Reputation: 6735

By default Qt application implicitly quits when the last window is closed. To prevent it, you can use setQuitOnLastWindowClosed in QGuiApplication.

app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(false)

Upvotes: 3

Related Questions