目录 1、QPainter绘图类 2、QPen绘图类 3、QBrush绘图类 4、QPixmap绘图类 ---- 本篇博文主要介绍如何实现在窗口中绘图,在 PyQt5中,一般可以通过QPainter、QPen 绘图类 QPen(钢笔)是一个基本的图形对象,用于绘制直线、曲线或者给轮廓画出矩形、椭圆形、多边形及其他形状等。 来看看QPen绘图类的示例,效果如下所示: 示例中,使用6种不同的线条样式绘制了6条线,其中前5条线使用的是预定义的线条样式。也可以自定义线条样式,最后一条线就是使用自定义的线条样式绘制的。 以下代码创建了一个QPen对象。为了能更清晰地看清各线之间的差异,将颜色设置成黑色,宽度设置为2像素(px)。Qt.SolidLine是预定义的线条样式之一。 pen = QPen(Qt.black, 2, Qt.SolidLine) 以下代码自定义了一种线条样式。
(Qt::white)); customPlot->yAxis->setTickPen(QPen(Qt::white)); customPlot->yAxis->setSubTickPen(QPen (Qt::white, 1)); customPlot->yAxis->setBasePen(QPen(Qt::white, 1)); customPlot->xAxis->setTickPen (QPen(Qt::white, 1)); customPlot->yAxis->setTickPen(QPen(Qt::white, 1)); customPlot->xAxis->setSubTickPen (QPen(Qt::white, 1)); customPlot->yAxis->setSubTickPen(QPen(Qt::white, 1)); customPlot->xAxis->setTickLabelColor (QColor(0, 0, 0))); candlesticks->setPenNegative(QPen(QColor(0, 0, 0))); // create ohlc chart:
QPainter可以在QImage、QOpenGLPaintDevice、QWidget上进行绘制图形 QPainter只能在类对象的paintEvent()函数中绘制图形 QPainter类的成员角色有: QPen QPainter painter(this); painter.setPen( QPen(QColor(11,67,127),3)); QRectF rect(40.0, 40.0, 100.0 painter(this); painter.setRenderHint(QPainter:: Antialiasing, true); //设置渲染,启动反锯齿 painter.setPen( QPen QPainter painter(this); painter.setPen(QPen(QColor(11,67,127),3)); QRectF rect(40.0, 40.0, 100.0, 100.0 QPainter painter(this); painter.setPen(QPen(QColor(11,67,127),3)); QRectF rect(40.0, 40.0, 100.0, 100.0
(TextColor, TextWidth)); plot->yAxis->setBasePen(QPen(TextColor, TextWidth)); plot->xAxis ->setTickPen(QPen(TextColor, TextWidth)); plot->yAxis->setTickPen(QPen(TextColor, TextWidth)) ; plot->xAxis->setSubTickPen(QPen(TextColor, TextWidth)); plot->yAxis->setSubTickPen( graph(0)->setScatterStyle( QCPScatterStyle(QCPScatterStyle::ssCircle, QPen graph(1)->setScatterStyle( QCPScatterStyle(QCPScatterStyle::ssCircle, QPen
customplot->axisRect()->setupFullAxesBox();//四边安装轴并显示 2.轴线颜色 代码例子: customplot->xAxis->setBasePen(QPen (Qt::red,4)); customplot->yAxis->setBasePen(QPen(Qt::blue,4)); customplot->xAxis2->setBasePen (QPen(Qt::yellow,4)); customplot->yAxis2->setBasePen(QPen(Qt::green,4)); 3.网格线颜色 代码例子: customplot ->axisRect()->setBackground(QBrush(Qt::black));//背景黑色 customplot->xAxis->grid()->setPen(QPen(QColor (180, 180, 180), 1, Qt::PenStyle::DashLine));//网格白色虚线 customplot->yAxis->grid()->setPen(QPen(QColor
这里主要牵涉到QPainter,QPen,QBrush三个类,用法也很简单。下面主要是实现一个全面显示各种图形的程序,定义了两个类,一个负责绘图区,一个负责用户交互,这里先介绍绘图区的类。 widget.h painter.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QBrush> #include<QPen RoundRect,Ellipse,Polygon,Polyline,Points,Arc,Path,Text,Pixmap}; void setShape(Shape); void setPen(QPen ); //重写重绘事件 void paintEvent(QPaintEvent*); private : Shape shape; QBrush brush; QPen Painter::setShape(Shape s){ shape=s; update();//调用qwidget的update()来更新状态 } void Painter::setPen(QPen
painter->drawEllipse(_pinPoint1, 5,5); painter->drawEllipse(_pinPoint2, 5,5); painter->setPen(QPen painter->drawLine(_pinPoint2.x(), _pinPoint2.y(), p.x(), p.y()); //液压臂 painter->setPen(QPen painter->drawEllipse(_pinPoint1,5,5); painter->drawEllipse(_pinPoint2,5,5); painter->setPen(QPen painter->drawLine(_pinPoint2.x(), _pinPoint2.y(), p.x(), p.y()); //液压臂 painter->setPen(QPen painter->drawEllipse(_pinPoint2,5,5); painter->drawEllipse(_pinPoint3,5,5); painter->setPen(QPen
axisRect()->setBackground(axisRectGradient); //设置QCPAxis轴的风格 customPlot->xAxis->setBasePen(QPen (Qt::white,2));// x轴线的画笔颜色和粗细 customPlot->xAxis->setTickPen(QPen(Qt::white,3));// x轴线上的主刻度线(有数字的刻度线 ;//setLowerEnding设置轴线开始时的风格 //设置QCPGrid网格的风格,每条网格对应一个刻度 customPlot->xAxis->grid()->setPen(QPen (QColor(255,0,255),1,Qt::SolidLine));//实线 customPlot->yAxis->grid()->setPen(QPen(QColor(255,0,255 (Qt::black,3));// 设置刻度为0时的网格线的画笔 customPlot->yAxis->grid()->setZeroLinePen(QPen(Qt::black,3));
里面放大缩小后你绘制矩形框和多边形框会跟随适应图片 main.py from PyQt5.QtCore import QPoint, QRect, Qt from PyQt5.QtGui import QPen src_w, src_h, width, height) rect = QRect(new_points[0], new_points[1]) painter.setPen(QPen painter.drawRect(rect) shape.py from PyQt5.QtCore import QPoint, QRect, Qt from PyQt5.QtGui import QPen src_w, src_h, width, height) rect = QRect(new_points[0], new_points[1]) painter.setPen(QPen
path1.cubicTo(333, 277, 353, 286, 367, 270); painter.drawPath(path1); painter.setPen(QPen path4.cubicTo(418, 257, 438, 215, 398, 208); painter.drawPath(path4); painter.setPen(QPen path5.cubicTo(365, 244, 372, 202, 398, 208); painter.drawPath(path5); painter.setPen(QPen path35.cubicTo(523, 76, 567, 413, 333, 405); painter.drawPath(path35); painter.setPen(QPen path37.cubicTo(525, 78, 548, 415, 338, 400); painter.drawPath(path37); painter.setPen(QPen
yAxis); fossil = new QCPBars(ui->customPlot->xAxis,ui->customPlot->yAxis); //设置画笔的颜色 minBar->setPen(QPen (Qt::red)); maxBar->setPen(QPen(Qt::green)); fossil->setPen(QPen(Qt::blue)); minBar->setBrush(Qt::red (Qt::blue)); keysAxis->grid()->setZeroLinePen(QPen(Qt::red)); keysAxis->setUpperEnding(QCPLineEnding: (Qt::green)); valuesAxis->grid()->setSubGridPen(QPen(Qt::gray)); valuesAxis->grid()->setSubGridVisible (Qt::blue)); fermateSpiral1->setBrush(QBrush(QColor(0,0,255,20))); fermateSpiral2->setPen(QPen(QColor
但是它们不能满足所有要求,我们就可以通过绘图来实现自定义图形 API核心类 说明 QPainter 用来绘画的对象,可以允许我们绘制各种图形 QPaintDevice 描述QPainter把图形画到哪个对象上 QPen painter.setPen(Qt::blue); //画文本 painter.drawText(QRect(50,100,800,200),"超级小小怪-s_little_monster"); (5)画笔 QPen 画笔颜色可以在实例化画笔对象时进行设置,画笔的宽度是通过setWidth()方法进行设置,画笔的风格是通过setSytle()方法进行设置,设置画刷主要是通过setBrush()方法 //画笔颜色 QPen ::QPen(const QColor& color); //画笔宽度 void QPen::setWidth(int width); //画笔风格 void QPen::setSytle(Qt::PenSytle style); 以下是画笔的风格: (6)画刷 用QBrush类描述,大多用于填充,具有样式、颜色、渐变、纹理等属性 以下是画刷的风格: //设置画笔 QPen pen(QColor(155,155,255
random import choice, randint from PySide6.QtCore import QPoint, Qt from PySide6.QtGui import QPainter, QPen import sys from PySide6.QtCore import QRect, Qt from PySide6.QtGui import QBrush, QColor, QPainter, QPen datetime import datetime from PySide6.QtCore import Qt from PySide6.QtGui import QFont, QPainter, QPen pos.y() return # 在画布上绘制图线 painter = QPainter(self.pixmap) pen = QPen pos.y() return # 在画布上绘制图线 painter = QPainter(self.pixmap) pen = QPen
void setPen(const QPen &pen) 设置绘制折线时使用的笔。 QPen pen() const 返回当前用于绘制折线的笔。 首先我们先来实现对绘制线条的自定义,在创建序列线条时,我们通常会自定义线条的颜色,颜色的自定义可以使用QPen类来指定,以下是 QPen 类中常用的方法的说明和概述: 方法 描述 QPen() 默认构造函数 QPen(Qt::PenStyle style) 使用指定的画笔风格创建画笔。 QPen(const QColor &color) 使用指定的颜色创建画笔。 当有了QPen类就可以对颜色进行自定义了,读者需要注意,曲线画笔中有一个setStyle属性,该属性是用于指定画笔风格的枚举值,常用于设置 QPen 的风格。 QPen gridLinePen() const 返回用于绘制网格线的笔。 QPen linePen() const 返回轴线的笔。
转换成正确类型 BRectangle *item=static_cast<BRectangle *>(CurrentSelectedItem_stu.item); QPen 转换成正确类型 BEllipse *item=static_cast<BEllipse *>(CurrentSelectedItem_stu.item); QPen QPointF m_center; QPointF m_edge; ItemType m_type; BPointItemList m_pointList; QPen m_pen_isSelected; QPen m_pen_noSelected; }; 重写type()函数时,需要加上override关键字,不然会有警告。
QPen “画笔” ==> 描述了 QPainter 画出来的线是什么样的. QBrush “画刷” ==> 描述了 QPainter 填充一个区域是什么样的. 在 Qt 中,QPen 类中定义了 QPainter 应该如何绘制形状、线条和轮廓。同时通过 QPen 类可以设置画笔的线宽、颜色、样式、画刷等。 设置画笔颜色:QPen::QPen(const QColor &color) 画笔的颜色主要是通过 QColor 类设置; 设置画笔宽度:void QPen::setWidth(int width) 设置画笔风格 :void QPen::setStyle(Qt::PenStyle style) 画笔的风格有: 【案例】:基于之前操作 QPen pen; // 设置成红色的线条 pen.setColor(QColor 如下图示: 设置画刷主要通过 void QPen::setBrush(const QBrush &brush) 方法,其参数为画刷的格式。
QCPScatterStyle QCPcs1(QCPScatterStyle::ssSquare, QColor(255,0,0),QColor(255,0,0),3);//设置折线图的点的形状及颜色 QPen qPen1(QColor(255,0,0)); // graph_1->setScatterStyle(QCPcs1); graph1->setPen(qPen1);//设置画笔颜色 QCPScatterStyle QCPcs2(QCPScatterStyle::ssCircle, QColor(0,255,0),QColor(0,255,0),3);//设置折线图的点的形状及颜色 QPen qPen2(QColor(0,255,0)); // graph2->setScatterStyle(QCPcs2); graph2->setPen(qPen2);//设置画笔颜色
pen); void setBrush(const QBrush &brush); void setText(const QString &text); void setLabelPen(const QPen plot); tracer->setStyle(QCPItemTracer::tsPlus);//可以选择设置追踪光标的样式,这个是小十字,还有大十字,圆点等样式 // tracer->setPen(QPen QCPItemPosition::ptAxisRectRatio); label->setBrush(QBrush(QColor(244, 244, 244, 100))); label->setPen(QPen (QCPItemPosition::ptPlotCoords); label->setBrush(QBrush(QColor(244, 244, 244, 100))); label->setPen(QPen tracer) plot->removeItem(tracer); if (label) plot->removeItem(label); } void myTracer::setPen(const QPen
, 25), QPointF(m_sanjiaoLeftRect.x()+m_sanjiaoLeftRect.width(), 35), }; QPen col_Kuang); painter.setPen(pen); painter.drawPolygon(points, 3); //三角加边 QPen QPointF(m_sanjiaoRightRect.x(), 25), QPointF(m_sanjiaoRightRect.x(), 35), }; QPen col_Kuang); painter.setPen(pen); painter.drawPolygon(points, 3); //内容 QPen m_textRightRect,m_msg,option); } else if(m_userType == User_Type::User_Time) { // 时间 QPen
m_confirmSeries = new QLineSeries(); m_confirmSeries->setName(tr("确诊")); m_confirmSeries->setPen(QPen m_mathFunction = new QLineSeries; m_mathFunction->setName(tr("函数模型")); m_mathFunction->setPen(QPen QLineSeries(); m_increaseConfirmSeries->setName(tr("新增确诊")); m_increaseConfirmSeries->setPen(QPen