精华内容
下载资源
问答
  • Qt实现的简单画图,可选择图形、更改颜色、撤回
  • qt painter教学ppt

    2014-12-10 15:58:00
    经典QT painter教程,里面是关于qt painter 和thread 的教学资源
  • QTpainter类中,我们可以方便的进行一些图形绘制。其中绘制圆弧的3个命令如下: voiddrawArc(const QRectF &rectangle, int startAngle, int spanAngle) voiddrawArc(const QRect &rectangle, int ...

    在QT的painter类中,我们可以方便的进行一些图形绘制。其中绘制圆弧的3个命令如下:

    void drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
    void drawArc(const QRect &rectangle, int startAngle, int spanAngle)
    void drawArc(int x, int y, int width, int height, int startAngle, int spanAngle)

    可以轻易看出,所有的绘制命令都是通过一个矩形和起始角和结束角来绘制的(具体参数用法略),并没有CAD中的3点绘制圆弧、圆心半径等圆弧绘制方法。

    下面是我绘制的用方法3绘制的一个圆弧,如下:

    大家可以看出,drawArc命令绘制的圆弧,其实是一个椭圆的圆弧。这里为了方便,就把整个圆都绘制出来了。

    所以,在QT painter中,绘制弧线的思路如下:

    “圆”统一到了“椭圆”中,只要把绘制椭圆的矩形定制成正方形就可以绘制出圆。

    如果要绘制”圆“的圆弧,只要把矩形定制成正方形即可绘制出圆弧,否则为椭圆弧。

    这种方法很巧妙,但是对于想3点绘制标准圆弧的画法却不太实用。而且需要把思维转换为矩形,并不是很方便实用(个人之言)。 

    展开全文
  • Qt Painter 画板

    2013-07-29 17:45:32
    这个一个关于Qt涂鸦板的程序,实现简单的画图、划线、颜色等操作。
  • QT painter.7z

    2021-08-15 16:56:10
    QT painter.7z
  • Qt:QtPainter设置画笔

    2018-09-04 17:36:00
    #include #include void MainWindow::paintEvent(QPaintEvent *) { QPainter painter; painter.begin(this); QPen pen(Qt::black); pen.setStyle(Qt::SolidLine); pen.setWidth(20); QPointF p1[3] = { QPointF(30.0...
    #include <QPainter>
    #include <QFont>
    void MainWindow::paintEvent(QPaintEvent *)
    {
        QPainter painter;
        painter.begin(this);
    
        QPen pen(Qt::black);
        pen.setStyle(Qt::SolidLine);
        pen.setWidth(20);
    
        QPointF p1[3] = {
            QPointF(30.0, 80.0),QPointF(20.0, 40.0), QPointF(80.0, 60.0)
        };
        QPointF p2[3] = {
            QPointF(130.0, 80.0), QPointF(120.0, 40.0), QPointF(180.0, 60.0)
        };
        QPointF p3[3] = {
            QPointF(230.0, 80.0), QPointF(220.0, 40.0), QPointF(280.0, 60.0)
        };
    
        pen.setJoinStyle(Qt::MiterJoin);
        painter.setPen(pen);
        painter.drawPolyline(p1, 3);
    
        pen.setJoinStyle(Qt::BevelJoin);
        painter.setPen(pen);
        painter.drawPolyline(p2, 3);
    
        pen.setJoinStyle(Qt::RoundJoin);
        painter.setPen(pen);
        painter.drawPolyline(p3, 3);
    
        painter.end();
    }
    

    8d2bc65ac0164a48a7b132bd3175d26e411.jpg

    画笔的连接风格

    #include <QPainter>
    #include <QFont>
    void MainWindow::paintEvent(QPaintEvent *)
    {
        QPainter painter;
        painter.begin(this);
    
    
        QPen pen(Qt::black);
        pen.setStyle(Qt::SolidLine);
        pen.setWidth(20);
    
            pen.setCapStyle(Qt::FlatCap);
            painter.setPen(pen);
            painter.drawLine(30, 30, 200, 30);
    
            pen.setCapStyle(Qt::SquareCap);
            painter.setPen(pen);
            painter.drawLine(30, 70, 200, 70);
    
            pen.setCapStyle(Qt::RoundCap);
            painter.setPen(pen);
            painter.drawLine(30, 110, 200, 110);
    
        painter.end();
    }
    

    6deff8ab4e913c5b4f28ff7f45ceb6f6b53.jpg

    画笔的三种边角。

    void MainWindow::paintEvent(QPaintEvent *)
    {
        QPainter painter;
        painter.begin(this);
    
        QRect rect1(10.0, 20.0, 80.0, 50);
        QRect rect2(110.0, 20.0, 80.0, 50.0);
        QRect rect3(210.0, 20.0, 80.0, 50.0);
        QRect rect4(10.0, 100.0, 80.0, 50.0);
        QRect rect5(110.0, 100.0, 80.0, 50.0);
        QRect rect6(210.0, 100.0, 80.0, 50.0);
    
            QPen pen(Qt::blue, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
            painter.setPen(pen);
            painter.drawEllipse(rect1);  //画椭圆
    
            pen.setStyle(Qt::DashLine);
            painter.setPen(pen);
            painter.drawEllipse(rect2);
    
            pen.setStyle(Qt::DotLine);
            painter.setPen(pen);
    
            painter.drawEllipse(rect3);
    
            pen.setStyle(Qt::DashDotLine);
            painter.setPen(pen);
    
            painter.drawEllipse(rect4);
    
            pen.setStyle(Qt::DashDotDotLine);
            painter.setPen(pen);
            painter.drawEllipse(rect5);
    
            pen.setStyle(Qt::CustomDashLine);
            painter.setPen(pen);
            painter.drawEllipse(rect6);
        painter.end();
    }

    045ea8dc64d0c145fadc0bde52fb70b238c.jpg

    设置画笔的轮廓

    --

     

    展开全文
  • 1、新建QT GUI, 基类选...

    1、新建QT GUI, 基类选择QWidget,取消“创建界面”

    2、在widget.h中添加#include <QPainter>和重写绘画事件定义:
     * protected:
        void paintEvent(QPaintEvent *event);
    3、在widget.cpp中添加重绘事件void Widget::paintEvent(QPaintEvent *event)

    4、线性渐变QLinearGradient

    
    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter;
    
        painter.begin(this);
        QLinearGradient ling(QPointF(70, 70), QPoint( 140, 140 ) );  //从起点到终点的渐变
        ling.setColorAt(0, Qt::blue);  //起点到中心要显示的颜色
        ling.setColorAt(1, Qt::green);
        ling.setSpread( QGradient::PadSpread );   //默认显示模式
    
        QBrush brush(ling);
        painter.setBrush(brush);
        painter.drawRect(0, 0, 200, 200);
    
        painter.end();
    }
    

    b6f33a50abdd0f62f8ba766f59529b45c3a.jpg

    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter;
    
        painter.begin(this);
        QLinearGradient ling(QPointF(70, 70), QPoint( 140, 140 ) );  //从起点到终点的渐变
        ling.setColorAt(0, Qt::blue);  //起点到中心要显示的颜色
        ling.setColorAt(1, Qt::green);
        ling.setSpread( QGradient::RepeatSpread );   //默认显示模式
    
        QBrush brush(ling);
        painter.setBrush(brush);
        painter.drawRect(0, 0, 200, 200);
    
        painter.end();
    }
    

    2a1251ee16dacfb2918a95a207106d2d9b8.jpg

    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter;
    
        painter.begin(this);
        QLinearGradient ling(QPointF(70, 70), QPoint( 140, 140 ) );  //从起点到终点的渐变
        ling.setColorAt(0, Qt::blue);  //起点到中心要显示的颜色
        ling.setColorAt(1, Qt::green);
        ling.setSpread( QGradient::ReflectSpread );   //默认显示模式
    
        QBrush brush(ling);
        painter.setBrush(brush);
        painter.drawRect(0, 0, 200, 200);
    
        painter.end();
    }
    

    00e754828b1a1e6cd582cc925880f6a4df7.jpg

    5、圆心渐变QRadialGradient

    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter;
        painter.begin(this);
    
        QRadialGradient radg(100, 100, 50, 120, 120);  //在圆心(100, 100),半径50的圆上渐变。渐变起点是(120, 120)
        radg.setColorAt( 0, Qt::black );
        radg.setColorAt( 1, Qt::white );
        //radg.setSpread(QGradient::PadSpread);   //默认
        QBrush brush(radg);
        painter.setBrush(brush);
        painter.drawRect(0, 0, 200, 200);
        painter.end();
    }
    

    0eaec383eec476427f07448aba36047eb41.jpg

     

    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter;
        painter.begin(this);
    
        QRadialGradient radg(100, 100, 50, 120, 120);  //在圆心(100, 100),半径50的圆上渐变。渐变起点是(120, 120)
        radg.setColorAt( 0, Qt::black );
        radg.setColorAt( 1, Qt::white );
        radg.setSpread(QGradient::RepeatSpread);
        QBrush brush(radg);
        painter.setBrush(brush);
        painter.drawRect(0, 0, 200, 200);
        painter.end();
    }

    ecf0468788a6659f383698f70dd8938a622.jpg

    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter;
        painter.begin(this);
    
        QRadialGradient radg(100, 100, 50, 120, 120);  //在圆心(100, 100),半径50的圆上渐变。渐变起点是(120, 120)
        radg.setColorAt( 0, Qt::black );
        radg.setColorAt( 1, Qt::white );
        radg.setSpread(QGradient::ReflectSpread);
        QBrush brush(radg);
        painter.setBrush(brush);
        painter.drawRect(0, 0, 200, 200);
        painter.end();
    }

    a821d19ed104822fbf983f51169cd2f8412.jpg

    6、坐标角度渐变QConicalGradient

    void Widget::paintEvent(QPaintEvent *event)
    {
        QPainter painter;
        painter.begin(this);
    
        QConicalGradient cong( 100, 100, 45.0 );  //在(100, 100)坐标上以45°为中心渐变
        cong.setColorAt( 0, Qt::black );
        cong.setColorAt( 1, Qt::white );
    
        QBrush brush(cong);
        painter.setBrush(brush);
        painter.drawRect(0, 0, 200, 200);
    
    
        painter.end();
    }

    dd55b92bf994e67f657aec22e6421f53281.jpg

    展开全文
  • 写上位机软件的时候,经常会用到指示灯控件,奈何Qt设计师提供的widget控件中没有指示灯,拖拖拖无计可施?那就自己画吧。painter画一个简单的指示灯,轮廓颜色和内部填充颜色搞定。 特点 1、内置几种常用的颜色,...

    前言

    写上位机软件的时候,经常会用到指示灯控件,奈何Qt设计师提供的widget控件中没有指示灯,拖拖拖无计可施?那就自己画吧。painter画一个简单的指示灯,轮廓颜色和内部填充颜色搞定。新建一个类,继承QWidget,带不带ui都行,我这里选择带ui了(代码中体现),重写paintEvent即可,其他的就是一些自己定义的接口了。
    在这里插入图片描述

    特点

    1、内置几种常用的颜色,红绿蓝黄白;
    2、提供自定义颜色接口,轮廓+内部填充;
    3、右键菜单自选颜色。

    代码

    colorfullamp.h

    #ifndef COLORFULLAMP_H
    #define COLORFULLAMP_H
    
    #include <QWidget>
    #include <QPainter>
    #include <QPainterPath>
    #include <QPen>
    #include <QBrush>
    #include <QColor>
    #include <QPoint>
    #include <QMouseEvent>
    #include <QMenu>
    #include <QColorDialog>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class ColorfulLamp; }
    QT_END_NAMESPACE
    
    class ColorfulLamp : public QWidget
    {
        Q_OBJECT
        //Q_PROPERTY(int lamp_type READ Lamp_type WRITE set_Lamp_type)
    
    public:
        ColorfulLamp(QWidget *parent = nullptr);
        ~ColorfulLamp();
    
        void red_lamp();
        void green_lamp();
        void blue_lamp();
        void yellow_lamp();
        void white_lamp();
        void custom_lamp(const QColor& border = QColor(Qt::white), const QBrush& inside = QBrush(Qt::black));
    
        void set_inside_color(const QColor& color);
        void set_border_color(const QColor& color);
        void set_pen_width(int width);
    
        QBrush get_inside_color();
        QColor get_border_color();
        int get_pen_width();
    
    protected:
        void paintEvent(QPaintEvent *);
        void mousePressEvent(QMouseEvent *);
    
    private:
        void select_inside_color();
        void select_border_color();
    private:
        Ui::ColorfulLamp *ui;
        QMenu* menu;
    
        QBrush inside_color;
        QColor border_color;
        int pen_width = 4;
    
    };
    #endif // COLORFULLAMP_H
    

    colorfullamp.cpp

    #if _MSC_VER >= 1600
    #pragma execution_character_set("utf-8")
    #endif
    
    #include "colorfullamp.h"
    #include "ui_colorfullamp.h"
    #include <QDebug>
    
    #define sys_out qDebug()
    
    ColorfulLamp::ColorfulLamp(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::ColorfulLamp)
    {
        ui->setupUi(this);
        setAttribute(Qt::WA_TranslucentBackground); //背景透明
        setWindowFlag(Qt::FramelessWindowHint); //设置无边框,背景才会透明,否则显示黑色
        menu = new QMenu(this);
    
        menu->addAction("红色", this, &ColorfulLamp::red_lamp);
        menu->addAction("绿色", this, &ColorfulLamp::green_lamp);
        menu->addAction("蓝色", this, &ColorfulLamp::blue_lamp);
        menu->addAction("黄色", this, &ColorfulLamp::yellow_lamp);
        menu->addAction("白色", this, &ColorfulLamp::white_lamp);
        menu->addAction("选择轮廓颜色", this, &ColorfulLamp::select_border_color);
        menu->addAction("选择指示灯颜色", this, &ColorfulLamp::select_inside_color);
    //    menu->addAction("关闭", [this]{close();});
    
        border_color = QColor(Qt::white);
        inside_color = QBrush(Qt::red);
    
    }
    
    ColorfulLamp::~ColorfulLamp()
    {
        delete ui;
    }
    
    void ColorfulLamp::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);    //开启抗锯齿
    
        QPen pen;
        pen.setWidth(pen_width);    //轮廓线宽,默认4px
        pen.setColor(border_color);
        painter.setPen(pen);
    
        int cur_width = width();    //widget宽度
        int cur_height = height();  //widget高度
    
        QPoint center(cur_width/2, cur_height/2);   //圆心
        int radius = __min(cur_width, cur_height)/2 - pen_width/2;    //半径,取宽高的较小者
    
        painter.drawEllipse(center, radius, radius);    //画轮廓
    
        QPainterPath path;
        path.addEllipse(center, radius - pen_width/2, radius - pen_width/2);
        painter.fillPath(path, inside_color);   //填充圆
    }
    
    void ColorfulLamp::mousePressEvent(QMouseEvent *event)
    {
        if(event->button() == Qt::RightButton)
        {
            menu->exec(event->globalPos());
        }
    }
    
    void ColorfulLamp::set_inside_color(const QColor& color)
    {
        inside_color.setColor(color);
    }
    void ColorfulLamp::set_border_color(const QColor& color)
    {
        inside_color.setColor(color);
    }
    
    void ColorfulLamp::red_lamp()
    {
        inside_color.setColor(Qt::red);
        update();
    }
    void ColorfulLamp::green_lamp()
    {
        inside_color.setColor(Qt::green);
        update();
    }
    
    void ColorfulLamp::blue_lamp()
    {
        inside_color.setColor(Qt::blue);
        update();
    }
    
    void ColorfulLamp::yellow_lamp()
    {
        inside_color.setColor(Qt::yellow);
        update();
    }
    
    void ColorfulLamp::white_lamp()
    {
        inside_color.setColor(Qt::white);
        update();
    }
    
    void ColorfulLamp::custom_lamp(const QColor& border, const QBrush& inside)
    {
        border_color = border;
        inside_color = inside;
        update();
    }
    
    void ColorfulLamp::select_inside_color()
    {
        QColor selection = QColorDialog::getColor();
        if(selection.isValid())
        {
            inside_color.setColor(selection);
            update();
        }
    }
    void ColorfulLamp::select_border_color()
    {
        QColor selection = QColorDialog::getColor();
        if(selection.isValid())
        {
            border_color = selection;
            update();
        }
    }
    
    QBrush ColorfulLamp::get_inside_color()
    {
        return inside_color;
    }
    
    QColor ColorfulLamp::get_border_color()
    {
        return border_color;
    }
    
    void ColorfulLamp::set_pen_width(int width)
    {
        if(width > 0)
        {
            pen_width = width;
            update();
        }
    }
    int ColorfulLamp::get_pen_width()
    {
        return pen_width;
    }
    
    展开全文
  • Qt:QtPainter设置画刷

    2018-09-04 18:08:00
    /* * 1、新建QT GU...
  • Qt之QtPainter:画圆角矩形和多边形

    千次阅读 2018-09-04 17:26:00
    painter.setPen(Qt::blue); QRectF rect(120, 10.0, 80.0, 80.0); painter.drawRoundedRect(rect, 20, 20); // round rect QPointF p2[3] = { QPointF(120.0, 110.0), QPointF(220.0, 110.0), QPointF(220.0...
  • painter.setBrush(QBrush(Qt::green, Qt::Dense3Pattern)); painter.drawEllipse(10, 10, 100,100); painter.setBrush(Qt::NoBrush); painter.drawEllipse(80, 10, 100, 100); painter.end(); } 资源...
  • QPen pen(Qt::blue, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); painter.setPen(pen); painter.drawEllipse(rect1); //画椭圆 pen.setStyle(Qt::DashLine); painter.setPen(pen); painter.drawEllipse...
  • PaintingApp 时间:2019-05-10标签:c ++ qtpainter
  • Qt Painter和Image保存

    千次阅读 2014-02-26 19:17:42
    void playPainter() { QSize size(200,200); QImage picture(size, QImage::Format_RGB32); picture.fill(32); QPainter painter; painter.begin(&picture);... painter.setRenderHint(QPain
  • qt painter多个点的曲线

    2018-11-05 15:13:00
    plot.h #ifndef PLOT_H #define PLOT_H ...QPainter painter(this);... painter.setPen(pen);... painter.drawPoints(pointf, 10); } 转载于:https://www.cnblogs.com/fuhang/p/9909280.html
  • Qt painter drawPolyline in paintEvent

    千次阅读 2013-06-20 10:35:21
    void MainWindow::paintEvent(QPaintEvent *w) { QPainter p(this);... p.setPen(QPen(Qt::red,10)); QPointF pt(19,19); QPointF pts[10]; int t=QTime::currentTime().second(); qsrand(t);
  • QTpainter的基本

    2020-04-19 23:07:54
    } //绘图事件 void Widget::paintEvent(QPaintEvent *) { QPainter painter(this); //画资源图片 //如果超出屏幕,强制变为 0位置 QPixmap pix(":/Image/Luffy.png"); if(posX > this->width()) { posX = -pix....
  • 比如当你Qt中用QPainter进行window和viewport,逻辑和物理坐标分离的形式进行绘图
  • //反锯齿 painter.setPen(QPen(Qt::red, 2, Qt::DashLine)); painter.setBrush(Qt::yellow); painter.setFont(QFont("aaa", 40, 700, true)); //"aaa"系统会默认挑选一个预先定义的字体 painter.drawText(QPoint...
  • QT 触发painter函数

    2020-11-04 11:15:16
    1.update(); 2.repaint();
  • /* * 1、新建QT GU...
  • painter.setPen(Qt::blue); QRectF rect(120, 10.0, 80.0, 80.0); painter.drawRoundedRect(rect, 20, 20); // round rect QPointF p2[3] = { QPointF(120.0, 110.0), QPointF(220.0, 110.0), QPointF(220.0, 190.0...
  • QtPainter与QLabel

    2018-09-05 19:37:00
    label->setPixmap(QPixmap("F:\\MyCode\\qtblog.png")); } void Widget::slotButtonClicked() { } Widget::~Widget() { } void Widget::mousePressEvent(QMouseEvent *ev) { startPos = ev->pos(); update(); } ...
  • painter.setPen(QPen(Qt::blue, 1, Qt::DashLine)); painter.drawRect(0, 0, 100, 100); QTransform transform; transform.translate(50, 50); //将坐标从当前坐标原点沿着x轴前进50,沿着y轴前进50 painter....
  • painter在控件上绘图无反应(eventFilter的使用)1、为控件安装事件过滤器2、在头文件中添加绘制函数和事件过滤函数3、函数实现4、效果如下 1、为控件安装事件过滤器 ui->lblImage->installEventFilter(this);...
  • 1、新建QT GUI, 基类选择QWidget,取消“创建界面” 2、在widget.h中添加#include 和重写绘画事件定义: * protected: void paintEvent(QPaintEvent *event); 3、在widget.cpp中添加重绘事件void Widget::...
  • QT雷达扫描代码

    2019-02-22 21:14:29
    简单的雷达扫描图绘制,使用QTimer、QPaintEvent、QResizeEvent和渐变填充,用按钮进行暂停和开始扫描
  • Qt错误:Painter not active

    千次阅读 2019-08-03 16:03:29
    Qt错误:Painter not active 使用Qt进行2D绘图时,运行结果界面空白,同时提示Painter not active这样的错误,如下图所示: 原因: 虽然定义了Qpainter绘画对象,但是并没有将其关联到绘画设备。因此提示Painter...
  • 简介: QT学习之路 2 ——Qt “涂鸦师” 之 QPainter painter(this); Qt “涂鸦师” (QPainter ) 是专门用来负责在Qt界面上的图形&图片的添加的。 Qt “涂鸦师” 之 神笔(QPen) QPen pen1(Qt::green); /...
  • 在使用qt中的painter直接进行绘制时,有时会用到fillrect进行填充矩形的展示,此函数预设了很多的颜色,在网上我暂时没找到直接能够查看的颜色展示,在此我写一个程序用于直接展示出来,截图在此,方便自己以及有...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 12,968
精华内容 5,187
关键字:

qtpainter