def paint(self, painter, option, index):
if option.state & QStyle.State_Selected:
painter.fillRect(option.rect, option.palette.highlight())
size = min(option.rect.width(), option.rect.height())
brightness = index.model().data(index, Qt.DisplayRole)
radius = (size/2.0) - (brightness/255.0 * size/2.0)
if radius == 0.0:
return
painter.save()
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
if option.state & QStyle.State_Selected:
painter.setBrush(option.palette.highlightedText())
else:
painter.setBrush(QBrush(Qt.black))
painter.drawEllipse(QRectF(
option.rect.x() + option.rect.width()/2 - radius,
option.rect.y() + option.rect.height()/2 - radius,
2*radius, 2*radius))
painter.restore()
python类QBrush()的实例源码
def __init__(self, parent=None):
super(SvgView, self).__init__(parent)
self.renderer = SvgView.Native
self.svgItem = None
self.backgroundItem = None
self.outlineItem = None
self.image = QImage()
self.setScene(QGraphicsScene(self))
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
# Prepare background check-board pattern.
tilePixmap = QPixmap(64, 64)
tilePixmap.fill(Qt.white)
tilePainter = QPainter(tilePixmap)
color = QColor(220, 220, 220)
tilePainter.fillRect(0, 0, 32, 32, color)
tilePainter.fillRect(32, 32, 32, 32, color)
tilePainter.end()
self.setBackgroundBrush(QBrush(tilePixmap))
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.fillRect(event.rect(), QBrush(Qt.white))
painter.translate(66, 66)
painter.save()
self.transformPainter(painter)
self.drawShape(painter)
painter.restore()
self.drawOutline(painter)
self.transformPainter(painter)
self.drawCoordinates(painter)
def paintEvent(self, event):
background = QRadialGradient(QPointF(self.rect().topLeft()), 500,
QPointF(self.rect().bottomRight()))
background.setColorAt(0, self.backgroundColor1)
background.setColorAt(1, self.backgroundColor2)
painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.fillRect(event.rect(), QBrush(background))
painter.setPen(self.pen)
for bubble in self.bubbles:
if QRectF(bubble.position - QPointF(bubble.radius, bubble.radius),
QSizeF(2*bubble.radius, 2*bubble.radius)).intersects(QRectF(event.rect())):
bubble.drawBubble(painter)
if self.newBubble:
self.newBubble.drawBubble(painter)
painter.end()
def draw_box(self, n, box, color, style, num):
x1, y1, x2, y2 = box.left(), box.top(), box.right(), box.bottom()
x1 = int(x1 * self.i_scale[n])
y1 = int(y1 * self.i_scale[n])
x2 = int(x2 * self.i_scale[n])
y2 = int(y2 * self.i_scale[n])
width = BASEWIDTH
if style == 'match':
width *= 2
painter = QPainter(self.i_pixmap[n])
painter.setPen(QPen(QBrush(color), width))
painter.drawRect(x1, y1, x2 - x1, y2 - y1)
painter.setPen(QPen(QBrush(TEXTCOLOR), TEXTWIDTH))
painter.setFont(TEXTFONT)
painter.drawText(x1, y2 + TEXTSIZE + 2 * BASEWIDTH, '{}: {}'.format(n + 1, num))
painter.end()
self.i_lbl[n].setPixmap(self.i_pixmap[n])
def brush(self, color=Qt.white, style=Qt.SolidPattern):
return QBrush(color, style)
def __init__(self):
self.brushes = {}
for key, values in self.COLOR_CODE.items():
self.brushes.update({
key: QtGui.QBrush(QtGui.QColor(*values))
})
def _add_latest_input_line(self, angle):
"""Adds a line to the graphic scene that visualizes a scanned angle"""
mx, my = self._get_middle()
angle_rad = deg2rad(angle)
angle_1_rad = deg2rad(angle - self.measurement_angle/2.0)
angle_2_rad = deg2rad(angle + self.measurement_angle/2.0)
length = max(self.width(), self.height())
start_point = (mx, my)
p1 = (mx + length * math.cos(angle_1_rad), my + length * math.sin(angle_1_rad))
p2 = (mx + length * math.cos(angle_2_rad), my + length * math.sin(angle_2_rad))
gradient_start_point, gradient_end_point = (mx, my), (mx + length * math.cos(angle_rad), my + length * math.sin(angle_rad))
gradient = QLinearGradient(*gradient_start_point, *gradient_end_point)
gradient.setColorAt(0, Qt.transparent)
gradient.setColorAt(0.8, Qt.red)
gradient.setColorAt(1, Qt.darkRed)
triangle = QPolygonF()
triangle.append(QPointF(*start_point))
triangle.append(QPointF(*p1))
triangle.append(QPointF(*p2))
triangle.append(QPointF(*start_point))
self.scene.addPolygon(triangle, pen=QPen(Qt.transparent), brush=QBrush(gradient))
def _add_measurement(self, length, angle, added_time):
"""
Adds a visualization for a measured distance to the scene
:param length: length in cm
:param angle: the angle
"""
mx, my = self._get_middle()
angle_rad = deg2rad(angle)
ex, ey = mx + length * math.cos(angle_rad), my + length * math.sin(angle_rad)
age = time.time() - added_time
age = age if age < self.fade_out_time else self.fade_out_time
alpha_channel_value = scale((0, self.fade_out_time), (255, 0), age)
assert 0 <= alpha_channel_value <= 255
brush_color = QColor(self.measured_distances_color)
brush_color.setAlpha(alpha_channel_value)
brush = QBrush(brush_color)
tpen = QPen(brush_color)
self.scene.addLine(mx, my, ex, ey, pen=tpen)
self.scene.addEllipse(ex-self.dot_width/2, ey-self.dot_width/2, self.dot_width, self.dot_width, pen=tpen, brush=brush)
def add_gpu_sensors(self):
"""Add selected temperature sensor(s) to the "Selected GPU sensor(s)" three widget."""
items = [item for item in self.ui.treeWidgetHWMonData.selectedItems()]
# The new items should have the tree widget itself as parent
parent = self.ui.treeWidgetSelectedGPUSensors
for item in items:
sensor_item = QtWidgets.QTreeWidgetItem(parent)
sensor_item.setText(0, item.text(0))
sensor_item.setText(1, item.text(1))
sensor_item.setForeground(0, QtGui.QBrush(QtCore.Qt.blue)) # Text color blue
# Deselect all items in the HWMon tree widget after they have been added
self.ui.treeWidgetHWMonData.clearSelection()
def __init__(self, parent=None):
super(PlayList, self).__init__()
self.parent = parent
self.setParent(self.parent.parent)
self.setObjectName("PlayList")
self.musicList = []
self.currentRow = -1
self.allRow = 0
self.itemColor = QBrush(QColor.fromRgb(95,95,99))
with open('QSS/playList.qss', 'r') as f:
self.setStyleSheet(f.read())
self.resize(574, 477)
self.hide()
self.setButtons()
self.setLabels()
self.setTables()
self.setLayouts()
# ???
def setColor(self, color):
self._color = color
self._ellipseItem.setBrush(QtGui.QBrush(self._color))
def unhighlight(self):
self._ellipseItem.setBrush(QtGui.QBrush(self._color))
self._ellipseItem.setRect(
-self.__radius,
-self.__radius,
self.__diameter,
self.__diameter,
)
# ===================
# Connection Methods
# ===================
def addRectangle(self):
item = QtCanvasRectangle(rand() % self.canvas.width(), rand() % self.canvas.height(), 50, 50, self.canvas)
item.setBrush(QBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8)))
item.setPen(QPen(QColor(rand() % 32*8, rand() % 32*8, rand() % 32*8), 4))
item.setZ(rand() % 256)
item.show()
return item
def addEllipse(self):
item = QtCanvasEllipse(50, 50, self.canvas)
item.setBrush(QBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8)))
item.move(rand() % self.canvas.width(), rand() % self.canvas.height())
item.setZ(rand() % 256)
item.show()
return item
def addRectangle(self):
item = QtCanvasRectangle(rand() % self.canvas.width(), rand() % self.canvas.height(), 50, 50, self.canvas)
item.setBrush(QBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8)))
item.setPen(QPen(QColor(rand() % 32*8, rand() % 32*8, rand() % 32*8), 4))
item.setZ(rand() % 256)
item.show()
return item
def addEllipse(self):
item = QtCanvasEllipse(50, 50, self.canvas)
item.setBrush(QBrush(QColor(rand() % 32 * 8, rand() % 32 * 8, rand() % 32 * 8)))
item.move(rand() % self.canvas.width(), rand() % self.canvas.height())
item.setZ(rand() % 256)
item.show()
return item
def __init__( self, app ):
self.app = app
self.debugLog = self.app.debug_options.debugLogLogHistory
super().__init__()
self.all_commit_nodes = []
self.all_tags_by_id = {}
self.all_unpushed_commit_ids = set()
self.__brush_is_tag = QtGui.QBrush( QtGui.QColor( 0, 0, 255 ) )
self.__brush_is_unpushed = QtGui.QBrush( QtGui.QColor( 192, 0, 192 ) )
def __init__( self, app ):
self.app = app
self.debugLog = self.app.debug_options.debugLogLogHistory
super().__init__()
self.all_commit_nodes = []
self.all_tags_by_rev = {}
self.__brush_is_tag = QtGui.QBrush( QtGui.QColor( 0, 0, 255 ) )
def __init__( self, app ):
self.app = app
self.debugLog = self.app.debug_options.debugLogTableModel
super().__init__()
self.scm_project_tree_node = None
self.all_files = []
self.all_included_files = None
self.__brush_is_cached = QtGui.QBrush( QtGui.QColor( 255, 0, 255 ) )
self.__brush_is_changed = QtGui.QBrush( QtGui.QColor( 0, 0, 255 ) )
self.__brush_is_uncontrolled = QtGui.QBrush( QtGui.QColor( 0, 128, 0 ) )