下面是我的简单托盘应用程序的代码。它与段错误崩溃时,我从应用的上下文菜单中调用信息窗口,然后将其关闭。 我tryed不同的变体找个理由段错误的,这是我最后一次尝试。

#!/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()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();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top