Here is the code of my simple tray application. It crashes with segfault when i call information window from context menu of application and then close it.
I’ve tryed different variants to find a reason of segfault, this is my last try.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, parent=None):
QtGui.QSystemTrayIcon.__init__(self, parent)
self.setIcon(QtGui.QIcon("icon.png"))
self.iconMenu = QtGui.QMenu(parent)
appabout = self.iconMenu.addAction("About")
appexit = self.iconMenu.addAction("Exit")
self.setContextMenu(self.iconMenu)
self.aboutdialog = QtGui.QWidget(parent)
self.connect(appabout,QtCore.SIGNAL('triggered()'),self.showAbout)
self.connect(appexit,QtCore.SIGNAL('triggered()'),self.appExit)
self.show()
def showAbout(self):
QtGui.QMessageBox.information(self.aboutdialog, self.tr("About Tunarium"), self.tr("Your text here."))
def appExit(self):
sys.exit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
trayIcon = SystemTrayIcon()
trayIcon.show()
sys.exit(app.exec_())
I don’t know Python but in your
appExit(), you should be callingquit()orexit()on the application object which will cause your call tosys.exit(app.exec_())in main to return. Again, not knowing the Python specifics, you can do this by using the Qt macroqAppand callqApp->quit()orQCoreApplication::instance()->quit().Calling
quit()is the same as callingexit(0). You can useexit()directly to return any exit code of your choice.Update:
I’ve tried your code in C++ with a few tweaks and it does work. I’ve commented where you should try making changes to your code. Hope it works for you.
Update 2:
As requested, here is the equivalent C++ code:
main.cpp
SystemTrayIcon.h
SystemTrayIcon.cpp