I'm a beginner in PyQt. I was trying to create a simple app to try some of the toolkit's many features. My question is, how can I hide the app icon from the taskbar? I don't want the user to be able to see the icon in taskbar and to minimize it using this icon. Is there any window 开发者_运维问答flags that I can use to achieve this?
This should do the trick:
myApp.setWindowFlags(QtCore.Qt.Tool)
This drove me nuts for days. Complete app code to implement below.
Key bits:
- override closeEvent(), enabling it to do either of just hiding window or true exit
- create some facility for user to choose either hide or exit behavior
- don't show() main window on instantiation, just exec_() the App
import sys from PyQt4.QtGui import QAction, QApplication, QFrame, QIcon, \ QMainWindow, QMenu, QSystemTrayIcon from PyQt4.QtCore import SIGNAL class MyApp(QMainWindow): def __init__(self, parent, title): super(QMainWindow, self).__init__(parent) self.exitOnClose = False exit = QAction(QIcon(), "Exit", self) self.connect(exit, SIGNAL("triggered()"), self.exitEvent) self.trayIcon = QSystemTrayIcon(QIcon(), self) menu = QMenu(self) menu.addAction(exit) self.trayIcon.setContextMenu(menu) self.connect(self.trayIcon, \ SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), \ self.trayIconActivated) self.trayIcon.show() self.trayIcon.showMessage("MyApp is running!", "Click to open window\nRight click for menu" ) def trayIconActivated(self, reason): if reason == QSystemTrayIcon.Context: self.trayIcon.contextMenu().show() elif reason == QSystemTrayIcon.Trigger: self.show() self.raise_() def closeEvent(self, event): if self.exitOnClose: self.trayIcon.hide() del self.trayIcon event.accept() else: self.hide() event.setAccepted(True) event.ignore() def exitEvent(self): self.exitOnClose = True self.close() if __name__ == "__main__": app = QApplication(sys.argv) myapp = MyApp(None, "My System Tray App") app.exec_()
Adapted from this thread:
import sys
from PyQt4.QtGui import *
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = QWidget()
mainWindow = QMainWindow(widget)
mainWindow.show()
sys.exit(app.exec_())
I wouldn't recommend trying to hide an application's taskbar presence, especially if the application is visible. If you are only trying to prevent minimizing from the taskbar then you can achieve this by creating your top level widget with the following window flags like this:
QWidget *mainWindow = new QWidget(0, Qt::CustomizeWindowHint
| Qt::WindowTitleHint | Qt::WindowSystemMenuHint
| Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint);
If you don't want a maximize flag, you can leave that one out of the list too.
The various window flags that Qt can use are documented here (Qt::WindowFlags).
If you are on Ubuntu with Unity and want to hide an application's icon from the launcher on the left-hand-side, you will probably need Qt.SplashScreen
. This worked for me but I don't remember if I also needed Qt.Tool
, which is enough on Windows. For the SplashScreen attempt you may have to reimplement the resize functionality as it disables this feature of a QStatusBar (that has a SizeGrip) for example.
Here is a little example to try out window flags.
Just initialise your main window like this self.setWindowFlags(Qt.ToolTip)
精彩评论