def drawLines(self, qp): pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) # SolidLine qp.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashLine) qp.setPen(pen) # DashLine qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) # DashDotLine qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) qp.setPen(pen) # DotLine qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) # DashDotDotLine qp.drawLine(20, 200, 250, 200) pen.setStyle(Qt.CustomDashLine) # Our pattern is 1px dash, 4px space, 5px dash, 4px space etc. pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) # CustomDashLine qp.drawLine(20, 240, 250, 240)
def drawhoverframe(self, painter): self.__grid = [[((161 + r * UNIT) / WIDTH * self.width, (111 + c * UNIT) / HEIGHT * self.height) for r in range(16)] for c in range(16)] pen = QPen(Qt.red, 2, Qt.DashLine) pos_x, pos_y = self.point2index() if 0 <= pos_x and pos_x < 15 and 0 <= pos_y and pos_y < 15 and not self.__pieces[ pos_x][pos_y]: pen.setDashPattern( [UNIT * self.width / WIDTH / 8, UNIT * self.width / WIDTH / 4]) painter.setPen(pen) painter.drawLine(self.__grid[pos_x][pos_y][0], self.__grid[pos_x][pos_y][1], self.__grid[pos_x + 1][pos_y][0], self.__grid[pos_x + 1][pos_y][1]) painter.drawLine(self.__grid[pos_x][pos_y + 1][0], self.__grid[pos_x][pos_y + 1][1], self.__grid[pos_x + 1][pos_y + 1][0], self.__grid[pos_x + 1][pos_y + 1][1]) pen.setDashPattern([ UNIT * self.height / HEIGHT / 8, UNIT * self.height / HEIGHT / 4 ]) painter.setPen(pen) painter.drawLine(self.__grid[pos_x + 1][pos_y][0], self.__grid[pos_x + 1][pos_y][1], self.__grid[pos_x + 1][pos_y + 1][0], self.__grid[pos_x + 1][pos_y + 1][1]) painter.drawLine(self.__grid[pos_x][pos_y][0], self.__grid[pos_x][pos_y][1], self.__grid[pos_x][pos_y + 1][0], self.__grid[pos_x][pos_y + 1][1])
def drawGrid(self, painter, rect, gridColor): tileWidth = self.map().tileWidth() tileHeight = self.map().tileHeight() if (tileWidth <= 0 or tileHeight <= 0): return startX = max(0, int(rect.x() / tileWidth) * tileWidth) startY = max(0, int(rect.y() / tileHeight) * tileHeight) endX = min(math.ceil(rect.right()), self.map().width() * tileWidth + 1) endY = min(math.ceil(rect.bottom()), self.map().height() * tileHeight + 1) gridColor.setAlpha(128) gridPen = QPen(gridColor) gridPen.setCosmetic(True) _x = QVector() _x.append(2) _x.append(2) gridPen.setDashPattern(_x) if (startY < endY): gridPen.setDashOffset(startY) painter.setPen(gridPen) for x in range(startX, endX, tileWidth): painter.drawLine(x, startY, x, endY - 1) if (startX < endX): gridPen.setDashOffset(startX) painter.setPen(gridPen) for y in range(startY, endY, tileHeight): painter.drawLine(startX, y, endX - 1, y)
def drawLines(self, qp): # 新建一个`QPen`对象,设置颜色黑色,宽2像素,这样就能看出来各个笔样式的区别。`Qt.SolidLine`是预定义样式的一种。 pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) # 绘制直线,前两个参数为左端点的坐标,后两个参数为右端点的坐标 qp.drawLine(50, 100, 280, 100) pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(50, 140, 280, 140) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(50, 180, 280, 180) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(50, 220, 280, 220) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(50, 260, 280, 260) # 这里我们自定义了一个笔的样式。定义为`Qt.CustomDashLine`然后调用`setDashPattern()`方法。 # 数字列表是线的样式,要求必须是个数为奇数,奇数位定义的是空格,偶数位为线长,数字越大,空格或线长越大 # 比如本例的就是1像素线,4像素空格,5像素线,4像素空格。 pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(50, 300, 280, 300)
def drawLines(self, qp): # 新建一个QPen对象,设置颜色黑色,宽2像素,这样就能看出来各个笔样式的区别。 pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) qp.drawLine(20, 40, 250, 40) # 使用预设的画笔样式 pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 120, 250, 80) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 160, 250, 80) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 200, 250, 80) # 自定义了一个笔的样式 # 数字列表是线的样式,要求必须是个数为奇数,奇数位定义的是空格,偶数位为线长,数字越大,空格或线长越大,比如本例的就是1像素线,4像素空格,5像素线,4像素空格 pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 80)
def draw(self, painter, draw=True): if self.color is None: return if draw: painter.save() pen = QPen(QColor(self.color)) if self.stroke is not None: pen.setWidthF(self.stroke) if self.line_style is not None: if self.line_style in [Qt.SolidLine, Qt.DashLine, Qt.DashDotLine, Qt.DotLine, Qt.DashDotDotLine]: pen.setStyle(self.line_style) elif isinstance(self.line_style, list): pen.setStyle(Qt.CustomDashLine) pen.setDashPattern(self.line_style) painter.setPen(pen) painter.setOpacity(self.opacity) polyline_to_draw = self.translated(0, 0) if self.scale is not None and self.scale != 1: polyline_to_draw = self.__scaled() # print('mid rect_to_plot', rect_to_plot) if self.translation is not None: # rect_to_plot.setX(rect_to_plot.x()+self.translation.x()) # rect_to_plot.setY(rect_to_plot.y()+self.translation.y()) polyline_to_draw.translate(self.translation.x(), self.translation.y()) # painter.drawPolygon(polygon_to_draw) if self.theta is not None and self.theta != 0: painter.translate(polyline_to_draw.boundingRect().center()) painter.rotate(self.theta) painter.translate(-polyline_to_draw.boundingRect().center()) painter.drawPolyline(polyline_to_draw) painter.restore()
def drawForeground(self, painter, rect): if self._tiling is None: return if self._showTileOutlines: tile_nos = self._tiling.intersected(rect) for tileId in tile_nos: ## draw tile outlines # Dashed black line pen = QPen() pen.setWidth(0) pen.setDashPattern([5, 5]) painter.setPen(pen) painter.drawRect(self._tiling.imageRects[tileId]) # Dashed white line # (offset to occupy the spaces in the dashed black line) pen = QPen() pen.setWidth(0) pen.setDashPattern([5, 5]) pen.setDashOffset(5) pen.setColor(QColor(Qt.white)) painter.setPen(pen) painter.drawRect(self._tiling.imageRects[tileId])
def draw_line(self, qp): qp.setPen(QPen(Qt.black, 3, Qt.SolidLine)) qp.drawLine(20, 20, 380, 20) qp.drawText(30, 40, 'Qt.SolidLine') qp.setPen(QPen(Qt.black, 3, Qt.DashLine)) qp.drawLine(20, 70, 380, 70) qp.drawText(30, 90, 'Qt.DashLine') qp.setPen(QPen(Qt.black, 3, Qt.DotLine)) qp.drawLine(20, 120, 380, 120) qp.drawText(30, 140, 'Qt.DotLine') qp.setPen(QPen(Qt.black, 3, Qt.DashDotLine)) qp.drawLine(20, 170, 380, 170) qp.drawText(30, 190, 'Qt.DashDotLine') qp.setPen(QPen(Qt.black, 3, Qt.DashDotDotLine)) qp.drawLine(20, 220, 380, 220) qp.drawText(30, 240, 'Qt.DashDotDotLine') pen = QPen(Qt.black, 3, Qt.CustomDashLine) pen.setDashPattern([4, 3, 2, 5]) qp.setPen(pen) qp.drawLine(20, 270, 380, 270) qp.drawText(30, 290, 'Qt.CustomDashLine')
def paintEvent(self, event): painter = QPainter() painter.begin(self) pen = QPen(Qt.blue, Qt.SolidLine) painter.setPen(pen) painter.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashDotDotLine) painter.setPen(pen) painter.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashLine) painter.setPen(pen) painter.drawLine(20, 100, 250, 100) pen.setStyle(Qt.DotLine) painter.setPen(pen) painter.drawLine(20, 120, 250, 120) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 10, 5, 8]) painter.setPen(pen) painter.drawLine(20, 140, 250, 140) painter.end()
def paintEvent(self, event): painter = QPainter(self) drawColor = Qt.gray if not self.file_over else Qt.blue pen = QPen() pen.setDashPattern([4, 2]) pen.setWidth(3) pen.setColor(drawColor) painter.setPen(pen) painter.setRenderHint(QPainter.Antialiasing) # Rounded dashed rect painter.translate(self.width() / 2 - 60, self.height() / 2 - 60) painter.drawRoundedRect(0, 0, 120, 120, 25.0, 25.0) # Arrow painter.translate(22, 30) arrow = QPainterPath() arrow.moveTo(20, 40) arrow.lineTo(30, 40) arrow.lineTo(30, 1) arrow.lineTo(50, 1) arrow.lineTo(50, 40) arrow.lineTo(60, 40) arrow.lineTo(40, 60) arrow.closeSubpath() painter.setPen(Qt.NoPen) painter.setBrush(drawColor) painter.drawPath(arrow)
def drawLines(self, qp): pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) qp.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 200, 250, 200) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240)
def drawLinesOnVideo(self, pt): ''' Draw Lines on Video ''' scr_x, scr_y = self.GetInverseMatrix(pt[1], pt[0]) radius = 3 center = QPoint(scr_x, scr_y) pen = QPen(Qt.yellow) pen.setWidth(radius) pen.setCapStyle(Qt.RoundCap) pen.setDashPattern([1, 4, 5, 4]) painter_p = QPainter(self) painter_p.setPen(pen) painter_p.setRenderHint(QPainter.HighQualityAntialiasing, True) painter_p.drawPoint(center) if len(self.drawLines) > 1: try: idx = self.drawLines.index(pt) scr_x, scr_y = self.GetInverseMatrix( self.drawLines[idx + 1][1], self.drawLines[idx + 1][0]) end = QPoint(scr_x, scr_y) painter_p.drawLine(center, end) except: None return
def draw_line(self, qp): # 电梯绳 pen = QPen(Qt.black, 0.5, Qt.SolidLine) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([4, 20]) qp.setPen(pen) for i in range(5): qp.drawLine(100 + i * self.LENGTH, 0, 100 + i * self.LENGTH, 660)
def drawGrid(self, painter, rect, gridColor): tileWidth = self.map().tileWidth() tileHeight = self.map().tileHeight() r = rect.toAlignedRect() r.adjust(-tileWidth / 2, -tileHeight / 2, tileWidth / 2, tileHeight / 2) startX = int(max(0.0, self.screenToTileCoords_(r.topLeft()).x())) startY = int(max(0.0, self.screenToTileCoords_(r.topRight()).y())) endX = int(min(self.map().width(), self.screenToTileCoords_(r.bottomRight()).x())) endY = int(min(self.map().height(), self.screenToTileCoords_(r.bottomLeft()).y())) gridColor.setAlpha(128) gridPen = QPen(gridColor) gridPen.setCosmetic(True) _x = QVector() _x.append(2) _x.append(2) gridPen.setDashPattern(_x) painter.setPen(gridPen) for y in range(startY, endY+1): start = self.tileToScreenCoords(startX, y) end = self.tileToScreenCoords(endX, y) painter.drawLine(start, end) for x in range(startX, endX+1): start = self.tileToScreenCoords(x, startY) end = self.tileToScreenCoords(x, endY) painter.drawLine(start, end)
def drawGrid(self, painter, rect, gridColor): tileWidth = self.map().tileWidth() tileHeight = self.map().tileHeight() r = rect.toAlignedRect() r.adjust(-tileWidth / 2, -tileHeight / 2, tileWidth / 2, tileHeight / 2) startX = int(max(0.0, self.screenToTileCoords_(r.topLeft()).x())) startY = int(max(0.0, self.screenToTileCoords_(r.topRight()).y())) endX = int( min(self.map().width(), self.screenToTileCoords_(r.bottomRight()).x())) endY = int( min(self.map().height(), self.screenToTileCoords_(r.bottomLeft()).y())) gridColor.setAlpha(128) gridPen = QPen(gridColor) gridPen.setCosmetic(True) _x = QVector() _x.append(2) _x.append(2) gridPen.setDashPattern(_x) painter.setPen(gridPen) for y in range(startY, endY + 1): start = self.tileToScreenCoords(startX, y) end = self.tileToScreenCoords(endX, y) painter.drawLine(start, end) for x in range(startX, endX + 1): start = self.tileToScreenCoords(x, startY) end = self.tileToScreenCoords(x, endY) painter.drawLine(start, end)
def drawLines(self, qp): #画笔,颜色、线宽、线型 pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) #设置画笔 qp.drawLine(20, 40, 250, 40) #绘制直线 pen.setStyle(Qt.DashLine) #更改线型 qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 200, 250, 200) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240)
def paintEvent(self, QPaintEvent): painter = QPainter() painter.begin(self) # 初始化画笔 pen1 = QPen(Qt.red, 5, Qt.SolidLine) # 红色, 粗细=5,直线 pen2 = QPen(Qt.blue, 6, Qt.DashLine) # 蓝色, 粗细=6,虚线 pen3 = QPen(Qt.yellow, 7, Qt.DashDotDotLine) # 黄色, 粗细=7,点点虚线 pen4 = QPen(Qt.black, 8, Qt.DashDotLine) # 黑色, 粗细=8,点虚线 pen6 = QPen(Qt.cyan, 10, Qt.DotLine) # 青色, 粗细=9,虚线 painter.setPen(pen1) # 设置画线使用的画笔 painter.drawLine(20, 40, 400, 40) # 前两个参数是直线在窗口的起点坐标,后两个参数是直线在窗口的终点坐标 painter.setPen(pen2) # 设置画线使用的画笔 painter.drawLine(20, 60, 400, 60) # 前两个参数是直线在窗口的起点坐标,后两个参数是直线在窗口的终点坐标 painter.setPen(pen3) # 设置画线使用的画笔 painter.drawLine(20, 80, 400, 80) # 前两个参数是直线在窗口的起点坐标,后两个参数是直线在窗口的终点坐标 painter.setPen(pen4) # 设置画线使用的画笔 painter.drawLine(20, 100, 400, 100) # 前两个参数是直线在窗口的起点坐标,后两个参数是直线在窗口的终点坐标 painter.setPen(pen6) # 设置画线使用的画笔 painter.drawLine(20, 140, 400, 140) # 前两个参数是直线在窗口的起点坐标,后两个参数是直线在窗口的终点坐标 # 可以通过setStyle来修改画笔的属性 pen6.setStyle(Qt.CustomDashLine) # 修改画笔的属性 pen6.setDashPattern([1, 4, 10, 3]) # 自定义的线:线和空白按照数组给定的数据来走的 只能有四位 painter.setPen(pen6) # 设置画线使用的画笔 painter.drawLine(20, 120, 400, 120) # 前两个参数是直线在窗口的起点坐标,后两个参数是直线在窗口的终点坐标 painter.end()
def paintEvent(self, eventPaintQEvent): g = self.geometry() self.bl = QPoint(0, g.height() - 1) self.br = QPoint(g.width() - 1, g.height() - 1) self.tl = QPoint(0, 0) self.tr = QPoint(g.width() - 1, 0) self.wedgePt = self.calc_wedgePt() myQP = QPainter(self) pen = QPen() pen.setWidth(3) pen.setColor(Qt.lightGray) pen.setDashPattern([5, 5]) myQP.setPen(pen) myQP.drawLine(self.wedgePt, self.tr) myQP.drawLine(self.tr, self.br) pen.setColor(Qt.black) pen.setDashPattern([1, 0]) myQP.setPen(pen) myQP.drawLine(self.bl, self.br) myQP.drawLine(self.bl, self.tl) myQP.drawLine(self.tl, self.wedgePt) myQP.drawLine(self.wedgePt, self.br)
def update_chart(self, update_axis=True, update_lines=True): pixmap = QPixmap(self.legend.size()) pixmap.fill(QColor(0, 0, 0, 0)) qp = QPainter() qp.begin(pixmap) y_spacing = 30 legend_pen = QPen() qp.setPen(legend_pen) for i in range(len(self.page['lines'])): line = self.page['lines'][i] qp.setPen(legend_pen) qp.drawText(QRect(10, 12 + i * y_spacing, 120, 20), Qt.AlignLeft, line.name) line_pen = QPen() line_pen.setWidth(4) line_pen.setColor(line.line_colour) line_pen.setStyle(line.line_style) if line.line_pattern and line.line_style is Qt.CustomDashLine: line_pen.setDashPattern(line.line_pattern) qp.setPen(line_pen) qp.drawLine(140, 10 + i * y_spacing + 10, 190, 10 + i * y_spacing + 10) qp.end() self.legend.setPixmap(pixmap) if self.page['lines'] and self.datasets: self.chart.paint_lines(self.page['lines'], update_lines, update_axis)
def drawLine(self, paint): pen = QPen(Qt.blue, 4, Qt.SolidLine) paint.setPen(pen) paint.drawLine(100, 40, 400, 40) pen.setStyle(Qt.DashLine) pen.setColor(Qt.yellow) paint.setPen(pen) paint.drawLine(100, 80, 400, 80) pen.setStyle(Qt.DashDotLine) pen.setColor(Qt.red) paint.setPen(pen) paint.drawLine(100, 120, 400, 120) pen.setStyle(Qt.DashDotDotLine) pen.setColor(Qt.darkMagenta) paint.setPen(pen) paint.drawLine(100, 160, 400, 160) pen.setStyle(Qt.DotLine) pen.setColor(Qt.darkGreen) pen.setWidth(2) paint.setPen(pen) paint.drawLine(100, 200, 400, 200) pen.setStyle(Qt.CustomDashLine) # 내가 직접 제작하는 라인 pen.setDashPattern([4, 4, 6, 4]) # Dash, 공간, Dash, 공간 pen.setColor(Qt.darkGray) pen.setWidth(8) paint.setPen(pen) paint.drawLine(100, 240, 400, 240)
def drawLines(self, painter): """自定义绘制参数""" pen = QPen(Qt.black, 2, Qt.SolidLine) painter.setPen(pen) painter.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashLine) painter.setPen(pen) painter.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) painter.setPen(pen) painter.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) painter.setPen(pen) painter.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) painter.setPen(pen) painter.drawLine(20, 200, 250, 200) pen.setStyle(Qt.CustomDashLine) # 线 + 空格 + 线 + 空格 : 单位像素 pen.setDashPattern([1, 4, 5, 4]) painter.setPen(pen) painter.drawLine(20, 240, 250, 240)
def drawLines(self, qp): # QPen(画笔),此处画了6条线段,其中前5条为预定义样式,最后一条为自定义样式 pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) qp.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 200, 250, 200) pen.setStyle(Qt.CustomDashLine) # 该方法,它的参数(一个数字列表)定义了一种风格,必须有偶数个数字; # 其中奇数表示绘制实线,偶数表示留空。数值越大,直线或空白就越大。 # 这里我们定义了1像素的实线,4像素的空白,5像素实线,4像素空白 pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240)
def drawPath(self, paths): pen = QPen(QColor('#000000')) pen.setStyle(Qt.DashLine) pen.setDashPattern([10, 4]) self.painter.setPen(pen) gs = controller.gridSize for path in paths: for i in range(len(path) - 1): a = path[i] b = path[i + 1] vx, vy = b[0] - a[0], b[1] - a[1] d = math.sqrt(vx * vx + vy * vy) # Chuyển về vector đơn vị vx = vx / d vy = vy / d # Xoay 90 độ vx, vy = -vy, vx vx = vx * 2 + gs / 2 vy = vy * 2 + gs / 2 self.qpDrawLine(a[0] * gs + vx, a[1] * gs + vy, b[0] * gs + vx, b[1] * gs + vy) self.painter.setPen(Qt.SolidLine)
def drawLines(self, qp): # 新建一个QPen对象,设置颜色黑色,宽2像素,Qt.SolidLine是预定义样式的一种 pen = QPen(Qt.black, 2, Qt.SolidLine) qp.setPen(pen) qp.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 200, 250, 200) # 自定义了一个笔的样式。 # 定义为Qt.CustomDashLine然后调用setDashPattern()方法 # 数字列表是线的样式,要求必须是个数为奇数,奇数位定义的是空格,偶数位为线长, # 数字越大,空格或线长越大,比如本例的就是1像素线,4像素空格,5像素线,4像素空格。 pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240)
def draw_lines(self, qp): pen = QPen(Qt.black, 2, Qt.SolidLine) # 钢笔样式 2px 黑色 qp.setPen(pen) qp.drawLine(20, 40, 250, 40) # 以下是五种风格的钢笔 pen.setStyle(Qt.DashLine) # 虚线 qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) # 虚+点线 qp.setPen(pen) qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) # 点线 qp.setPen(pen) qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) # 虚+点+点线 qp.setPen(pen) qp.drawLine(20, 200, 250, 200) pen.setStyle(Qt.CustomDashLine) # 自定义了一个形状 # 第一个数 1 表示 1px 实点的宽度,第二个数 4 表示 4px 的空白 # 第三个数 5 表示 5px 实点的宽度,第四个数 4 表示 4px 的空白 # 我们可以增加或减少列表里的数字 pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240)
def paintEvent(self, QPaintEvent): painter = QPainter() painter.begin(self) pen = QPen(Qt.red, 3, Qt.SolidLine) painter.setPen(pen) # 两端点的坐标(x1,y1) (x2,y2) painter.drawLine(20, 40, 250, 40) pen.setStyle(Qt.DashDotLine) painter.setPen(pen) painter.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotDotLine) painter.setPen(pen) painter.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DashLine) painter.setPen(pen) painter.drawLine(20, 160, 250, 160) #自定义 pen.setStyle(Qt.CustomDashLine) #相对宽度 pen.setDashPattern([1, 10, 5, 8]) painter.setPen(pen) painter.drawLine(20, 200, 250, 200) painter.end()
def drawLines(self, qp): pen = QPen(Qt.black, 2, Qt.SolidLine) # создание объекта, цвет - черный, жирность - 2px, # Qt.SolidLine – это один из стилей (жирная линия) qp.setPen(pen) # создание объекта, и установка параметров ручки qp.drawLine(20, 40, 250, 40) # рисование линии pen.setStyle(Qt.DashLine) # стиль пунктир qp.setPen(pen) qp.drawLine(20, 80, 250, 80) pen.setStyle(Qt.DashDotLine) # стиль пунктир точка qp.setPen(pen) qp.drawLine(20, 120, 250, 120) pen.setStyle(Qt.DotLine) # стиль пунктир мелкий qp.setPen(pen) qp.drawLine(20, 160, 250, 160) pen.setStyle(Qt.DashDotDotLine) # стиль пунктир точка точка qp.setPen(pen) qp.drawLine(20, 200, 250, 200) # определяем пользовательский стиль ручки. # Мы устанавливаем стиль ручки Qt.CustomDashLine и вызываем метод setDashPattern(). # Список чисел определяет стиль. Может быть даже несколько чисел. # Нечётные числа определяют сплошную линию, чётные числа – промежутки. # Чем больше число, тем больше промежуток или штрих. # Наш образец – штрих в 1 пиксель, промежуток в 4 пикселя, штрих в 5 пикселей, промежуток в 4 пикселя pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240)
def drawLines(self, qp): pen = QPen(Qt.gray, 3, Qt.SolidLine) qp.setPen(pen) qp.drawLine(20, 300, 400, 0) pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(20, 320, 400, 0) pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 340, 400, 0) pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 360, 400, 40) pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 380, 400, 40) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 400, 400, 40)
def penDraw(self, painter): # 设置钢笔 pen = QPen(Qt.red, 2, Qt.SolidLine) painter.setPen(pen) painter.drawLine(10, 10, 300, 100) pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 54]) # 当改变钢笔的塑性时要setPen painter.setPen(pen) painter.drawLine(30, 30, 40, 30)
def format_axis(axis, min_value, max_value, step): for s in axis.categoriesLabels(): axis.remove(s) axis.setStartValue(min_value) for i in range(ceil(min_value / step), floor(max_value / step) + 1): v = i * step axis.append('%s' % v, v) axis.setRange(min_value, max_value) grid_pen = QPen(QColor('silver')) grid_pen.setDashPattern([1, 1, 1, 1, 0, 0, 0, 0]) #grid_pen.setDashPattern([1, 1, 0, 0]) axis.setGridLinePen(grid_pen)
def update_pixmap(self, size, convert_to_gui_point): print('updating pixmap') pixmap = QPixmap(size) pixmap.fill(QColor(255, 255, 255, 0)) qp = QPainter() qp.begin(pixmap) if self.display_line or self.display_points: x_data, y_data = self.get_data() line_points = np.dstack(convert_to_gui_point([np.array(x_data), np.array(y_data)]))[0] if self.sort_by_x: line_points = line_points[line_points[:,0].argsort()] qpoints = [QPoint(e[0], e[1]) for e in line_points] if self.display_line and len(line_points) > 0: pen = QPen() pen.setStyle(self.line_style) pen.setCapStyle(Qt.RoundCap) if self.line_style is Qt.CustomDashLine and self.line_pattern: pen.setDashPattern(self.line_pattern) pen.setColor(self.line_colour) pen.setWidth(self.line_width) qp.setPen(pen) qp.drawPolyline(*qpoints) # if self.is_point_visible(e)]) if self.display_points: pen = QPen() pen.setColor(self.point_colour) if self.point_shape is PointShape.CIRCLE: pen.setWidth(self.point_size / 10) pen.setCapStyle(Qt.RoundCap) draw_func = lambda painter, point: painter.drawPoint(point) elif self.point_shape is PointShape.SQUARE: pen.setWidth(self.point_size / 10) pen.setCapStyle(Qt.SquareCap) draw_func = lambda painter, point: painter.drawPoint(point) elif self.point_shape is PointShape.RING: pen.setWidth(self.point_size / 40) draw_func = lambda painter, point: painter.drawEllipse(point, self.point_size / 20, self.point_size / 20) else: draw_func = print qp.setPen(pen) for pnt in qpoints: draw_func(qp, pnt) qp.end() self._pixmap = pixmap return self._pixmap
def paint(self, painter, option, widget=None): """ Paint a single line of the slice intersection marker. """ width = self._parent._width height = self._parent._height thickness = self._parent.PEN_THICKNESS # Our pen thickness is consistent across zoom levels because the pen is "cosmetic" # However, that doesn't ensure a consistent-size dash pattern. # Determine the inverse scaling factor from scene to view to set a consistent dash pattern at all scales. view = self.scene().views()[0] inverted_transform, has_inverse = view.transform().inverted() dash_length = old_div(4, inverted_transform.m11()) dash_length = max(0.5, dash_length) # Draw the line with two pens to get a black-and-white dashed line. # pen_white = QPen( Qt.white, thickness ) pen_white = QPen(self._parent._cropColor, thickness) pen_white.setDashPattern([dash_length, dash_length]) pen_white.setCosmetic(True) pen_black = QPen(Qt.black, thickness) pen_black.setDashPattern([dash_length, dash_length]) pen_black.setCosmetic(True) pen_black.setDashOffset(dash_length) with painter_context(painter): t = painter.transform() painter.setTransform(self.scene().data2scene * t) # Draw the line with two pens to get a black-and-white dashed line. for pen in [pen_white, pen_black]: painter.setPen(pen) if self._direction == "horizontal": painter.drawLine(QPointF(0.0, self.position), QPointF(width, self.position)) else: painter.drawLine(QPointF(self.position, 0.0), QPointF(self.position, height))
def drawGrid(self, painter, exposed, gridColor): rect = exposed.toAlignedRect() if (rect.isNull()): return p = RenderParams(self.map()) # Determine the tile and pixel coordinates to start at startTile = self.screenToTileCoords_(rect.topLeft()).toPoint() startPos = self.tileToScreenCoords_(startTile).toPoint() ## Determine in which half of the tile the top-left corner of the area we # need to draw is. If we're in the upper half, we need to start one row # up due to those tiles being visible as well. How we go up one row # depends on whether we're in the left or right half of the tile. ## inUpperHalf = rect.y() - startPos.y() < p.sideOffsetY inLeftHalf = rect.x() - startPos.x() < p.sideOffsetX if (inUpperHalf): startTile.setY(startTile.y() - 1) if (inLeftHalf): startTile.setX(startTile.x() - 1) startTile.setX(max(0, startTile.x())) startTile.setY(max(0, startTile.y())) startPos = self.tileToScreenCoords_(startTile).toPoint() oct = [ QPoint(0, p.tileHeight - p.sideOffsetY), QPoint(0, p.sideOffsetY), QPoint(p.sideOffsetX, 0), QPoint(p.tileWidth - p.sideOffsetX, 0), QPoint(p.tileWidth, p.sideOffsetY), QPoint(p.tileWidth, p.tileHeight - p.sideOffsetY), QPoint(p.tileWidth - p.sideOffsetX, p.tileHeight), QPoint(p.sideOffsetX, p.tileHeight)] lines = QVector() #lines.reserve(8) gridColor.setAlpha(128) gridPen = QPen(gridColor) gridPen.setCosmetic(True) _x = QVector() _x.append(2) _x.append(2) gridPen.setDashPattern(_x) painter.setPen(gridPen) if (p.staggerX): # Odd row shifting is applied in the rendering loop, so un-apply it here if (p.doStaggerX(startTile.x())): startPos.setY(startPos.y() - p.rowHeight) while(startPos.x() <= rect.right() and startTile.x() < self.map().width()): rowTile = QPoint(startTile) rowPos = QPoint(startPos) if (p.doStaggerX(startTile.x())): rowPos.setY(rowPos.y() + p.rowHeight) while(rowPos.y() <= rect.bottom() and rowTile.y() < self.map().height()): lines.append(QLineF(rowPos + oct[1], rowPos + oct[2])) lines.append(QLineF(rowPos + oct[2], rowPos + oct[3])) lines.append(QLineF(rowPos + oct[3], rowPos + oct[4])) isStaggered = p.doStaggerX(startTile.x()) lastRow = rowTile.y() == self.map().height() - 1 lastColumn = rowTile.x() == self.map().width() - 1 bottomLeft = rowTile.x() == 0 or (lastRow and isStaggered) bottomRight = lastColumn or (lastRow and isStaggered) if (bottomRight): lines.append(QLineF(rowPos + oct[5], rowPos + oct[6])) if (lastRow): lines.append(QLineF(rowPos + oct[6], rowPos + oct[7])) if (bottomLeft): lines.append(QLineF(rowPos + oct[7], rowPos + oct[0])) painter.drawLines(lines) lines.resize(0) rowPos.setY(rowPos.y() + p.tileHeight + p.sideLengthY) rowTile.setY(rowTile.y() + 1) startPos.setX(startPos.x() + p.columnWidth) startTile.setX(startTile.x() + 1) else: # Odd row shifting is applied in the rendering loop, so un-apply it here if (p.doStaggerY(startTile.y())): startPos.setX(startPos.x() - p.columnWidth) while(startPos.y() <= rect.bottom() and startTile.y() < self.map().height()): rowTile = QPoint(startTile) rowPos = QPoint(startPos) if (p.doStaggerY(startTile.y())): rowPos.setX(rowPos.x() + p.columnWidth) while(rowPos.x() <= rect.right() and rowTile.x() < self.map().width()): lines.append(QLineF(rowPos + oct[0], rowPos + oct[1])) lines.append(QLineF(rowPos + oct[1], rowPos + oct[2])) lines.append(QLineF(rowPos + oct[3], rowPos + oct[4])) isStaggered = p.doStaggerY(startTile.y()) lastRow = rowTile.y() == self.map().height() - 1 lastColumn = rowTile.x() == self.map().width() - 1 bottomLeft = lastRow or (rowTile.x() == 0 and not isStaggered) bottomRight = lastRow or (lastColumn and isStaggered) if (lastColumn): lines.append(QLineF(rowPos + oct[4], rowPos + oct[5])) if (bottomRight): lines.append(QLineF(rowPos + oct[5], rowPos + oct[6])) if (bottomLeft): lines.append(QLineF(rowPos + oct[7], rowPos + oct[0])) painter.drawLines(lines) lines.resize(0) rowPos.setX(rowPos.x() + p.tileWidth + p.sideLengthX) rowTile.setX(rowTile.x() + 1) startPos.setY(startPos.y() + p.rowHeight) startTile.setY(startTile.y() + 1)