Google,尽管我尝试过,但似乎无法弄清如何摆脱白色边框,而这种白色边框是我为QSplashScreen服务的png周围的问题.
我已经看过this article,但是我不知道如何将其转换为PyQt,或者甚至不是我想要的.
我什至尝试过分别设置一个黑白蒙版,这会导致低质量的结果,并且到处都有很多点.
有没有人发现神奇的半透明/半透明的SplashScreen,您还可以将文本数据输入到showMessage命令中?提前致谢.
我应该提到这也是Windows 7.
以下是我用作初始图像的测试PNG以及由此得到的难看的白色边框:
通过QBitmap提供黑白Alpha的另一种方法产生了这个更接近但难看的示例,其中充满了奇怪的点.代码和图像如下.
splash_pix = QPixmap(":/images/images/PyQtSplash.png")
splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
splash.setMask(QBitmap(QPixmap(":/images/images/PyQtSplashAlpha.jpg")))
解决方法:
只要使用透明区域干净的图像,只需要将其蒙版传递给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()