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

发布于 2024-09-16 17:30:56 字数 1149 浏览 4 评论 0原文

这是我的简单托盘应用程序的代码。当我从应用程序的上下文菜单中调用信息窗口然后将其关闭时,它会因段错误而崩溃。 我尝试了不同的变体来查找段错误的原因,这是我最后一次尝试。

#!/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_())

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_())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2024-09-23 17:30:56

我不了解 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++ 代码:

ma​​in.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();
}

I don't know Python but in your appExit(), you should be calling quit() or exit() on the application object which will cause your call to sys.exit(app.exec_()) in main to return. Again, not knowing the Python specifics, you can do this by using the Qt macro qApp and call qApp->quit() or QCoreApplication::instance()->quit().

Calling quit() is the same as calling exit(0). You can use exit() 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.

#!/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_())

Update 2:

As requested, here is the equivalent C++ code:

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文