尽管我尝试过谷歌,但我似乎无法弄清楚如何摆脱我在一个png屏幕上看到的白色边框。
我见过,但我不知道如何把它翻译成PyQt,也不知道这是否是我想要的。
我甚至试过分别设置一个黑白面具,结果质量很低,到处都是点。
有没有人发现了神奇的半透明/半透明SplashScreen,您还可以将文本数据馈送给showMessage命令ala?提前谢谢。
我应该说这也是Windows7。
下面是一个测试PNG,我使用它作为splash图像,以及由此产生的难看的白色边框:
通过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/