Python PyQt应用程序在关闭QMessagebox窗口后崩溃
下面是我的简单托盘应用程序的代码。当我从应用程序的上下文菜单中调用信息窗口并关闭它时,它会与segfault一起崩溃。 我尝试了不同的变体来找出SEGFULT的原因,这是我最后一次尝试Python PyQt应用程序在关闭QMessagebox窗口后崩溃,python,pyqt,contextmenu,trayicon,tray,Python,Pyqt,Contextmenu,Trayicon,Tray,下面是我的简单托盘应用程序的代码。当我从应用程序的上下文菜单中调用信息窗口并关闭它时,它会与segfault一起崩溃。 我尝试了不同的变体来找出SEGFULT的原因,这是我最后一次尝试 #!/usr/bin/env python # -*- coding: utf-8 -*- import sys from PyQt4 import QtCore from PyQt4 import QtGui class SystemTrayIcon(QtGui.QSystemTrayIcon):
#!/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_())
我不懂Python,但是在你的
appExit()
中,你应该在应用程序对象上调用quit()
或exit()
,这将导致你在main中返回对sys.exit(app.exec())
的调用。同样,在不了解Python细节的情况下,可以使用Qt宏qApp
并调用qApp->quit()
或QCoreApplication::instance()->quit()
调用quit()
与调用exit(0)
相同。您可以直接使用exit()
返回您选择的任何退出代码
更新:
我已经尝试过C++中的代码,但有一些改进。我已经评论了您应该尝试对代码进行更改的地方。希望对你有用
#!/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)
# Remove this next line, it isn't needed
#self.aboutdialog = QtGui.QWidget(parent)
self.connect(appabout,QtCore.SIGNAL('triggered()'),self.showAbout)
self.connect(appexit,QtCore.SIGNAL('triggered()'),self.appExit)
# Remove this next line, it isn't needed
#self.show()
def showAbout(self):
# Before showing the message box, disable the tray icon menu
self.iconMenu.setEnabled(false)
# Replace self.aboutdialog with the Python equivalent of null (0?)
QtGui.QMessageBox.information(0, self.tr("About Tunarium"), self.tr("Your text here."))
# Re-enable the tray icon menu
self.iconMenu.setEnabled(true)
def appExit(self):
# Replace the next line with something that calls the QApplication's
# exit() or quit() function.
#sys.exit()
app.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
# Tell the application not to exit when the last window is closed. This should
# prevent the application from exiting when the message box is closed.
app.setQuitOnLastWindowClosed(false)
trayIcon = SystemTrayIcon()
trayIcon.show()
sys.exit(app.exec_())
更新2:
按照要求,这里是等效的C++代码:
main.cpp
#include <QtGui/QApplication>
#include "SystemTrayIcon.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
SystemTrayIcon trayIcon(&app);
trayIcon.show();
return app.exec();
}
#include <iostream>
#include <QtCore/QCoreApplication>
#include <QtGui/QIcon>
#include <QtGui/QAction>
#include <QtGui/QMessageBox>
#include "SystemTrayIcon.h"
SystemTrayIcon::SystemTrayIcon(QObject * parent) :
QSystemTrayIcon(parent),
m_appabout(0),
m_appexit(0),
m_iconMenu(0),
m_aboutdialog(0)
{
setIcon(QIcon("icon.png"));
m_iconMenu = new QMenu();
m_appabout = m_iconMenu->addAction("About");
m_appexit = m_iconMenu->addAction("Exit");
setContextMenu(m_iconMenu);
connect(m_appabout, SIGNAL(triggered()), this, SLOT(slot_showAbout()));
connect(m_appexit, SIGNAL(triggered()), this, SLOT(slot_exit()));
}
SystemTrayIcon::~SystemTrayIcon()
{
}
void SystemTrayIcon::slot_showAbout()
{
std::cout << "slot show about." << std::endl;
m_iconMenu->setEnabled(false);
QMessageBox::information(0, "About Tunarium", "Your text here.");
m_iconMenu->setEnabled(true);
}
void SystemTrayIcon::slot_exit()
{
std::cout << "slot exit." << std::endl;
qApp->quit();
}
#包括
#包括“SystemTrayIcon.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
app.setQuitOnLastWindowClosed(false);
SystemTrayIcon trayIcon(&app);
trayIcon.show();
返回app.exec();
}
SystemTrayIcon.h
#ifndef SYSTEMTRAYICON_H
#define SYSTEMTRAYICON_H
#include <QtGui/QSystemTrayIcon>
#include <QtGui/QAction>
#include <QtGui/QMenu>
#include <QtGui/QWidget>
class SystemTrayIcon : public QSystemTrayIcon
{
Q_OBJECT
public:
SystemTrayIcon(QObject * parent = 0);
virtual ~SystemTrayIcon();
private:
QAction * m_appabout;
QAction * m_appexit;
QMenu * m_iconMenu;
QWidget * m_aboutdialog;
private slots:
void slot_showAbout();
void slot_exit();
};
#endif /* SYSTEMTRAYICON_H */
\ifndef SYSTEMTRAYICON\u H
#定义SYSTEMTRAYICON_H
#包括
#包括
#包括
#包括
类SystemTrayIcon:公共QSystemTrayIcon
{
Q_对象
公众:
SystemTrayIcon(QObject*parent=0);
虚拟~SystemTrayIcon();
私人:
QAction*m_appabout;
QAction*m_上诉;
QMenu*m_图标菜单;
QWidget*m_aboutdialog;
专用插槽:
空槽_showAbout();
空槽_出口();
};
#endif/*SYSTEMTRAYICON_H*/
SystemTrayIcon.cpp
#include <QtGui/QApplication>
#include "SystemTrayIcon.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
SystemTrayIcon trayIcon(&app);
trayIcon.show();
return app.exec();
}
#include <iostream>
#include <QtCore/QCoreApplication>
#include <QtGui/QIcon>
#include <QtGui/QAction>
#include <QtGui/QMessageBox>
#include "SystemTrayIcon.h"
SystemTrayIcon::SystemTrayIcon(QObject * parent) :
QSystemTrayIcon(parent),
m_appabout(0),
m_appexit(0),
m_iconMenu(0),
m_aboutdialog(0)
{
setIcon(QIcon("icon.png"));
m_iconMenu = new QMenu();
m_appabout = m_iconMenu->addAction("About");
m_appexit = m_iconMenu->addAction("Exit");
setContextMenu(m_iconMenu);
connect(m_appabout, SIGNAL(triggered()), this, SLOT(slot_showAbout()));
connect(m_appexit, SIGNAL(triggered()), this, SLOT(slot_exit()));
}
SystemTrayIcon::~SystemTrayIcon()
{
}
void SystemTrayIcon::slot_showAbout()
{
std::cout << "slot show about." << std::endl;
m_iconMenu->setEnabled(false);
QMessageBox::information(0, "About Tunarium", "Your text here.");
m_iconMenu->setEnabled(true);
}
void SystemTrayIcon::slot_exit()
{
std::cout << "slot exit." << std::endl;
qApp->quit();
}
#包括
#包括
#包括
#包括
#包括
#包括“SystemTrayIcon.h”
SystemTrayIcon::SystemTrayIcon(QObject*父对象):
QSystemTrayIcon(母公司),
m_appabout(0),
m_上诉(0),
m_iconMenu(0),
m_aboutdialog(0)
{
setIcon(QIcon(“icon.png”);
m_iconMenu=新的QMenu();
m_appabout=m_图标菜单->添加操作(“关于”);
m_appexit=m_iconMenu->addAction(“退出”);
设置上下文菜单(m_图标菜单);
连接(m_appabout,信号(已触发()),此,插槽(插槽显示());
连接(m_appexit,信号(triggered()),此,插槽(SLOT_exit());
}
SystemTrayIcon::~SystemTrayIcon()
{
}
void SystemTrayIcon::slot\u showAbout()
{
std::cout setEnabled(真);
}
void SystemTrayIcon::插槽_出口()
{
std::cout如果我使用exit,我的应用程序将与segfault一起崩溃。我会玩它,但现在一切正常。今晚我将再看一看。你为aboutdialog创建一个小部件,然后创建一个消息框并将其作为aboutdialog小部件的父项的方式有点不寻常。你也在调用show()可能是个问题。我已经更新了我的答案,并给出了一些代码建议。希望它能有所帮助。谢谢你,阿诺德。我会考虑你的建议。如果你有C++变体,你也可以把它贴在这里吗?