谷歌虽然我尝试过,但我似乎无法弄清楚如何摆脱我正在为 QSplashScreen 服务的 png 周围的白色边框。
我看过 this article ,但我不知道如何将其转换为 PyQt,或者这是否是我想要的。
我什至尝试过分别设置黑白蒙版,结果质量很差,到处都是很多点。
有没有人想出神奇的半透明/半透明 SplashScreen,您也可以将文本数据输入其中,ala 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()
关于python - PyQt 中透明 QSplashScreen 图像上的白色边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8332704/