只要你使用干净透明区域的图像,只需要将其掩码传递给
QSplashScreen.setMask
即可。在Linux上,此脚本按预期工作。
from PyQt4 import QtGui, QtCore
def show_splash(path):
image = QtGui.QPixmap(path)
splash = QtGui.QSplashScreen(image)
splash.setAttribute(QtCore.Qt.WA_DeleteOnClose)
splash.setMask(image.mask())
font = QtGui.QFont(splash.font())
font.setPointSize(font.pointSize() + 5)
splash.setFont(font)
splash.show()
QtGui.QApplication.processEvents()
for count in range(1, 6):
splash.showMessage(splash.tr('Processing %1...').arg(count),
QtCore.Qt.AlignBottom | QtCore.Qt.AlignLeft,
QtCore.Qt.white)
QtGui.QApplication.processEvents()
QtCore.QThread.msleep(1000)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
show_splash(sys.argv[1])
app.quit()
编辑
这个自定义的 SplashScreen 类应该在 Linux 和 Windows 上都能产生合理的结果:
from PyQt4 import QtGui, QtCore
class SplashScreen(QtGui.QWidget):
def __init__(self, pixmap):
QtGui.QWidget.__init__(self)
self._pixmap = pixmap
self._message = QtCore.QString()
self._color = QtGui.QColor.black
self._alignment = QtCore.Qt.AlignLeft
self.setWindowFlags(QtCore.Qt.FramelessWindowHint |
QtCore.Qt.WindowStaysOnTopHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setFixedSize(self._pixmap.size())
self.setMask(self._pixmap.mask())
def clearMessage(self):
self._message.clear()
self.repaint()
def showMessage(self, message, alignment=QtCore.Qt.AlignLeft,
color=QtGui.QColor.black):
self._message = QtCore.QString(message)
self._alignment = alignment
self._color = color
self.repaint()
def paintEvent(self, event):
textbox = QtCore.QRect(self.rect())
textbox.setRect(textbox.x() + 5, textbox.y() + 5,
textbox.width() - 10, textbox.height() - 10)
painter = QtGui.QPainter(self)
painter.drawPixmap(self.rect(), self._pixmap)
painter.setPen(QtGui.QColor(self._color))
painter.drawText(textbox, self._alignment, self._message)
def mousePressEvent(self, event):
self.hide()
def show_splash(path):
image = QtGui.QPixmap(path)
splash = SplashScreen(image)
font = QtGui.QFont(splash.font())
font.setPointSize(font.pointSize() + 5)
splash.setFont(font)
splash.show()
QtGui.QApplication.processEvents()
for count in range(1, 6):
splash.showMessage(splash.tr('Processing %1...').arg(count),
QtCore.Qt.AlignCenter, QtCore.Qt.white)
QtGui.QApplication.processEvents()
QtCore.QThread.msleep(1000)
splash.hide()
splash.close()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
show_splash(sys.argv[1])
app.quit()
QtCore.Qt.FramelessWindowHint
作为窗口标志时,我得到了预期的所有显示内容,甚至包括透明区域。也许这是一个错误,现在已经修复了。(PySide 1.2.1,Python 3.3,Windows 7) - Trilarion