PyQt应用程序在关闭QMessagebox窗口后崩溃

3

这是我简单托盘应用的代码。当我从应用程序的上下文菜单调用信息窗口并关闭它时,它会崩溃并出现段错误。

我尝试了不同的变体来查找段错误的原因,这是我最后一次尝试。

#!/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_())
1个回答

3
我不会Python,但在你的appExit()函数中,你应该调用应用程序对象上的quit()exit(),这将导致主函数中的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();
}

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 */

SystemTrayIcon.cpp

#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();
}

如果我使用exit,我的应用程序会崩溃并出现段错误。我会继续尝试解决这个问题,但现在一切都正常运行。 - PocketSam
我已经更新了我的答案,并提供了一些代码建议。希望能有所帮助。 - Arnold Spence
哇塞!太好了!我在你的代码中唯一需要更改的是 QtGui.QMessageBox.information(None,...,并使用0和1代替false/true。谢谢! - PocketSam
现在我只需要15点声望就可以感谢你了。=) - PocketSam
我需要一个Python版本的这个,因为它被命名为PyQt。 - greendino
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接