热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python已停止工作appcrash_关闭QMessageboxwind后PyQt应用程序崩溃

我不知道Python,但是在您的appExit()中,您应该在application对象上调用quit()或{},这将导致您对main中的

我不知道Python,但是在您的appExit()中,您应该在application对象上调用quit()或{},这将导致您对main中的sys.exit(app.exec_())的调用返回。同样,不知道Python的细节,可以使用Qt宏qApp并调用qApp->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++代码:

主.cpp

^{pr2}$

SystemTrayIcon.h#ifndef SYSTEMTRAYICON_H

#define SYSTEMTRAYICON_H

#include

#include

#include

#include

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

系统事务处理.cpp#include

#include

#include

#include

#include

#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." <

m_iconMenu->setEnabled(false);

QMessageBox::information(0, "About Tunarium", "Your text here.");

m_iconMenu->setEnabled(true);

}

void SystemTrayIcon::slot_exit()

{

std::cout <<"slot exit." <

qApp->quit();

}



推荐阅读
author-avatar
Me丶i丶晓莹丬_690
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有