2

This is an example of code for simple system tray PyQt application.

import sys
from PyQt4 import QtGui

def main():
   app = QtGui.QApplication(sys.argv)

   trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon('test.png'), app)
   menu = QtGui.QMenu()
   exitAction = menu.addAction("Exit")
   trayIcon.setContextMenu(menu)

   # I'd like to show picture in tooltip, BUT IT'S NOT WORK IN WINDOWS
   trayIcon.setTooltip('<img src="SomePicture.png" width="48" height="48"/>')

   trayIcon.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

In this code I'd like to show balloon tooltip with a some picture and some kind of text formating. For this purpose I use RichText tags formatting. As the result for Ubuntu Linux system (Gnome desktop) everything is Ok. But when I try use RichText formatting for tooltip in Windows XP system, nothing works. Tooltip text equals source string: ''. Python version on Windows 2.7, on Linux 2.6 but I think that problem is not in different versions.

If in Windows OS RichText isn't parsing how can I make same kind of GUI (Crossplatform is prefered)?

4

2 に答える 2

2

Windowsでは、QtはテキストのみをサポートするOSのツールチップシステムを使用します。

より高度なものが必要な場合は、ここでQSystemTrayIcon.showMessage()説明されているように使用できます。ヘルプイベントを取得するには、おそらくeventfilterをインストールするか、sメソッドをオーバーライドする必要があります。QTrayIconevent

于 2012-05-24T10:52:22.337 に答える
2

誰かがバルーン ウィジェットの作成にも興味がある場合。この私のコード:

class SystemTrayIcon(QtGui.QSystemTrayIcon):
    def __init__(self, parent = None): 
        QtGui.QSystemTrayIcon.__init__(self, icon, parent)
        traySignal = "activated(QSystemTrayIcon::ActivationReason)"
        self.connect(self, QtCore.SIGNAL(traySignal), self._activateRoutine)
        self.balloon = balloonWidget(name)

    def _activateRoutine(self, reason):
        if reason == QtGui.QSystemTrayIcon.Trigger:
            self.balloon.show(self.geometry())

class balloonWidget(QtGui.QWidget):
    def __init__(self,name):
        QtGui.QWidget.__init__(self, parent = None, flags = QtCore.Qt.Popup)

        self.name = name

        self.offsetX = 10
        self.offsetY = 10

        self.outInfo = QtGui.QLabel(self)

        self.setStyleSheet("QWidget {border:5px solid rgb(170, 170, 255);}")

    def show(self,coord):
        richText = tr('Any text with Rich Format')
        self.outInfo.setText(richText)
        self.outInfo.show()
        self.adjustSize()

        origin = QtGui.QDesktopWidget().availableGeometry().bottomRight()

        if coord.y() < origin.y()/2:
            moveY = coord.bottomLeft().y() + self.offsetY
        else:
            moveY = coord.topLeft().y() - (self.height() + self.offsetY)

        if coord.x() + self.width() + self.offsetX >= origin.x():
            moveX = origin.x() - (self.width() + self.offsetX)
        else:
            moveX = coord.x()

        self.move(moveX,moveY)
        self.setVisible(True)

    def closeEvent(self, event):
        event.ignore()
        self.hide()

    def mousePressEvent(self, event):
        self.close()
于 2012-05-28T11:57:07.480 に答える