peg*_*gaz 5 python qt styles pyqt background-color
QRubberBand我尝试的是稍微定制一下我的自定义的外观和感觉。我想实现带有透明蓝色选择矩形背景的蓝色选择边缘。问题是,当我使用CleanLooks风格时,背景始终是透明的,但当我切换到WindowsVista风格时,一切正常。
我在 Windows 机器上使用 PyQt 10.4.3。
这是一个小代码示例,您可以从中看到这种奇怪的行为。
from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import Qt
class Selector(QtGui.QRubberBand):
"""
Custom QRubberBand
"""
def __init__(self,*arg,**kwargs):
super(Selector,self).__init__(*arg,**kwargs)
def paintEvent(self, QPaintEvent):
painter = QtGui.QPainter(self)
# set pen
painter.setPen(QtGui.QPen(Qt.blue,4))
# set brush
color = QtGui.QColor(Qt.blue)
painter.setBrush(QtGui.QBrush(color))
# set opacity
painter.setOpacity(0.3)
# draw rectangle
painter.drawRect(QPaintEvent.rect())
class Panel(QtGui.QWidget):
def __init__(self,parent = None):
super(Panel,self).__init__(parent)
self.rubberBand = Selector(QtGui.QRubberBand.Rectangle,self)
def mousePressEvent(self, QMouseEvent):
self.clickPosition = QMouseEvent.pos()
self.rubberBand.setGeometry(QtCore.QRect(self.clickPosition,QtCore.QSize()))
self.rubberBand.show()
def mouseMoveEvent(self, QMouseEvent):
pos = QMouseEvent.pos()
self.rubberBand.setGeometry(QtCore.QRect(self.clickPosition,pos).normalized())
def mouseReleaseEvent(self, QMouseEvent):
self.rubberBand.hide()
if __name__ == "__main__":
import sys
app = QtGui.QApplication([])
QtGui.QApplication.setStyle("cleanlooks")
pn = Panel()
pn.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
我在 Qt 中的其他 GUI 元素中看到了类似的行为(例如,在Is it possible to change the color of a QTableWidget row label? 中),我的印象是 Qt 中的样式可能很棘手,因为它可以覆盖用户特定的自定义设置。
原则上,您可以实现自己的 QStyle 并将所有自定义设置放在那里。
这里更简单的方法可能是将元素的样式(此处QRubberBand)设置为不干扰的样式。
这应该有效:
class Selector(QtGui.QRubberBand):
def __init__(self,*arg,**kwargs):
super().__init__(*arg,**kwargs)
self.setStyle(QtGui.QStyleFactory.create('windowsvista'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1155 次 |
| 最近记录: |