Qt21基础图形的绘制

paintarea

paintarea.h

#ifndef PAINTAREA_H
#define PAINTAREA_H

#include <QWidget>
#include <QBrush>
#include <QPen>
#include <QPainter>
#include <QPainterPath>

class PaintArea : public QWidget
{
    Q_OBJECT
public:
    enum Shape{Line,Rectangle,RoundRect,Ellipse,Polygon,PolyLine,Points,Arc,Path,Text,Pixmap};

    explicit PaintArea(QWidget *parent = nullptr);
    void SetShape(Shape);
    void SetPen(QPen);
    void SetBrush(QBrush);
    void SetFillRule(Qt::FillRule);
    void paintEvent(QPaintEvent*);

signals:

private:
    Shape shape;
    QPen pen;
    QBrush brush;
    Qt::FillRule FillRule;

};

#endif // PAINTAREA_H

paintarea.cpp

#include "paintarea.h"


PaintArea::PaintArea(QWidget *parent)
    : QWidget{parent}
{
    setPalette(QPalette(Qt::white));
    setAutoFillBackground(true);
    setMinimumSize(400,400);
}

void PaintArea::SetShape(Shape s)
{
    shape =s;
    update();
}

void PaintArea::SetPen(QPen p)
{
    pen  = p;
    update();
}

void PaintArea::SetBrush(QBrush b)
{
    brush = b;
    update();
}

void PaintArea::SetFillRule(Qt::FillRule Rule)
{
    FillRule = Rule;
    update();
}

void PaintArea::paintEvent(QPaintEvent *)
{
    QPainter p(this);//创建一个画板
    p.setPen(pen);//设置画笔
    p.setBrush(brush);//设置画刷

    QRect Rect(50,100,300,200);
    static const QPoint points[4] =
    {
        QPoint(150,100),
        QPoint(300,150),
        QPoint(350,250),
        QPoint(100,300)
    };
    int StartAngle = 30*16;
    int SpanAngle = 120*16;
    QPainterPath path;
    path.addRect(150,150,100,100);
    path.moveTo(100,100);
    path.cubicTo(300,100,200,200,300,300);
    path.cubicTo(100,300,200,200,100,100);
    path.setFillRule(FillRule);
    switch(shape)
    {
        case Line://直线
        {
            p.drawLine(Rect.topLeft(),Rect.bottomRight());
            break;
        }
        case Rectangle://长方形
        {
            p.drawRect(Rect);
            break;
        }
        case RoundRect://圆角方形
        {
            p.drawRoundedRect(Rect,20,20);
            break;
        }
        case Ellipse://椭圆形
        {
            p.drawEllipse(Rect);
            break;
        }
        case Polygon://多边形
        {
            p.drawPolygon(Rect);
            break;
        }
        case Points://点
        {
            p.drawPoints(points,4);
            break;
        }
        case Arc://弧
        {
            p.drawArc(Rect,StartAngle,SpanAngle);
            break;
        }
        case Path://路径
        {
            p.drawPath(path);
            break;
        }
        case Text://文字
        {
            p.drawText(Rect,Qt::AlignCenter,tr("第一"));
            break;
        }
        case Pixmap://文字
        {
            p.drawPixmap(150,150,QPixmap("312.ico"));
            break;
        }
        default:break;
    }

}

paintex

paintex.h

#ifndef PAINTEX_H
#define PAINTEX_H

#include <QWidget>
#include "paintarea.h"
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QPushButton>
#include <QGridLayout>
#include <QGradient>

class PaintEx : public QWidget
{
    Q_OBJECT

public:
    PaintEx(QWidget *parent = nullptr);
    ~PaintEx();
private:
    PaintArea* paintArea;
    QLabel* ShapeLabel;
    QComboBox* shapeComboBox;
    QLabel* PenWidthLabel;
    QSpinBox* PenWidthSpinBox;
    QLabel* PenColorLabel;
    QFrame* PenColorFrame;
    QPushButton* PenColorBtn;
    QLabel* PenStyleLabel;
    QComboBox* PenStyleComboBox;
    QLabel* PenCapLabel;
    QComboBox* PenCapComboBox;
    QLabel* PenJoinLabel;
    QComboBox* PenJoinComboBox;
    QLabel* FillRuleLabel;
    QComboBox* FillRuleComboBox;
    QLabel* SpreadLabel;
    QComboBox* SpreadComboBox;
    QGradient::Spread spread;
    QLabel* BrushStyleLabel;
    QComboBox *BrushStyleComboBox;
    QLabel* BrushColorLabel;
    QFrame* BrushColorFrame;
    QPushButton* BrushColorBtn;
    QGridLayout* RightLayout;
protected slots:
    void ShowShape(int);
    void ShowPenWidth(int);
    void ShowPenColor();
    void ShowPenStyle(int);
    void ShowPenCap(int);
    void ShowPenJoin(int);
    void ShowSpreadStyle();
    void ShowFillRule();
    void ShowBrushColor();
    void ShowBrush(int);


};
#endif // PAINTEX_H

paintex.cpp

#include "paintex.h"
#include <QColorDialog>

PaintEx::PaintEx(QWidget *parent)
    : QWidget(parent)
{
    paintArea = new PaintArea;
    ShapeLabel = new QLabel(tr("形状:"));
    shapeComboBox =new QComboBox;
    shapeComboBox->addItem(tr("Line"),PaintArea::Line);
    shapeComboBox->addItem(tr("Rectangle"),PaintArea::Rectangle);
    shapeComboBox->addItem(tr("RoundedRect"),PaintArea::RoundRect);
    shapeComboBox->addItem(tr("Ellipse"),PaintArea::Ellipse);
    shapeComboBox->addItem(tr("Polygon"),PaintArea::Polygon);
    shapeComboBox->addItem(tr("PolyLine"),PaintArea::PolyLine);
    shapeComboBox->addItem(tr("Points"),PaintArea::Points);
    shapeComboBox->addItem(tr("Arc"),PaintArea::Arc);
    shapeComboBox->addItem(tr("Path"),PaintArea::Path);
    shapeComboBox->addItem(tr("Text"),PaintArea::Text);
    shapeComboBox->addItem(tr("Pixmap"),PaintArea::Pixmap);
    connect(shapeComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, &PaintEx::ShowShape);
    PenColorLabel = new QLabel("画笔颜色:");
    PenColorFrame = new QFrame;
    PenColorFrame->setAutoFillBackground(true);
    PenColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    PenColorFrame->setPalette(QPalette(Qt::blue));
    PenColorBtn = new QPushButton(tr("更改"));
    connect(PenColorBtn,&QPushButton::clicked,this,&PaintEx::ShowPenColor);

    PenWidthLabel = new QLabel(tr("画笔线宽|:"));
    PenWidthSpinBox = new QSpinBox;
    PenWidthSpinBox->setRange(0,20);
    connect(PenWidthSpinBox,static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),this,&PaintEx::ShowPenWidth);

    PenStyleLabel  = new QLabel(tr("画笔风格:"));
    PenStyleComboBox = new QComboBox;
    PenStyleComboBox->addItem(tr("SolidLine"),static_cast<int>(Qt::SolidLine));
    PenStyleComboBox->addItem(tr("DashLine"),static_cast<int>(Qt::DashLine));
    PenStyleComboBox->addItem(tr("DotLine"),static_cast<int>(Qt::DotLine));
    PenStyleComboBox->addItem(tr("DashDotLine"),static_cast<int>(Qt::DashDotDotLine));
    PenStyleComboBox->addItem(tr("DashDotDotLine"),static_cast<int>(Qt::DashDotDotLine));
    PenStyleComboBox->addItem(tr("CustomDashLine"),static_cast<int>(Qt::CustomDashLine));
    connect(PenStyleComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, &PaintEx::ShowPenStyle);
    PenCapLabel = new QLabel(tr("画笔顶帽:"));
    PenCapComboBox  = new QComboBox;
    PenCapComboBox->addItem(tr("SquareCap"),Qt::SquareCap);
    PenCapComboBox->addItem(tr("FlatCap"),Qt::FlatCap);
    PenCapComboBox->addItem(tr("RoundCap"),Qt::RoundCap);
    connect(PenCapComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, &PaintEx::ShowPenCap);

    PenJoinLabel = new QLabel(tr("画笔连接点:"));
    PenJoinComboBox = new QComboBox;
    PenJoinComboBox->addItem(tr("BevelJoin"),Qt::BevelJoin);
    PenJoinComboBox->addItem(tr("MiterJoin"),Qt::MiterJoin);
    PenJoinComboBox->addItem(tr("RoundJoin"),Qt::RoundJoin);
    connect(PenJoinComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, &PaintEx::ShowPenJoin);

    FillRuleLabel  = new QLabel(tr("填充模式:"));
    FillRuleComboBox = new QComboBox;
    FillRuleComboBox->addItem(tr("Old Even"),Qt::OddEvenFill);
    FillRuleComboBox->addItem(tr("Winding"),Qt::WindingFill);
    connect(FillRuleComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, &PaintEx::ShowFillRule);

    SpreadLabel = new QLabel(tr("铺展效果:"));
    SpreadComboBox = new QComboBox;
    SpreadComboBox->addItem(tr("PadSpread"),QGradient::PadSpread);
    SpreadComboBox->addItem(tr("Repeat"),QGradient::RepeatSpread);
    SpreadComboBox->addItem(tr("ReflectSpread"),QGradient::ReflectSpread);
    connect(SpreadComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, &PaintEx::ShowSpreadStyle);

    BrushColorLabel = new QLabel(tr("画刷颜色:"));
    BrushColorFrame = new QFrame;
    BrushColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    BrushColorFrame->setAutoFillBackground(true);
    BrushColorFrame->setPalette(QPalette(Qt::green));
    BrushColorBtn = new QPushButton(tr("更改"));
    connect(BrushColorBtn,&QPushButton::clicked,this,&PaintEx::ShowBrushColor);

    BrushStyleLabel = new QLabel(tr("画刷风格:"));
    BrushStyleComboBox = new QComboBox;
    BrushStyleComboBox->addItem(tr("SolidPattern"),static_cast<int>(Qt::SolidPattern));
    BrushStyleComboBox->addItem(tr("Dense1Pattern"),static_cast<int>(Qt::Dense1Pattern));
    BrushStyleComboBox->addItem(tr("Dense2Pattern"),static_cast<int>(Qt::Dense2Pattern));
    BrushStyleComboBox->addItem(tr("Dense3Pattern"),static_cast<int>(Qt::Dense3Pattern));
    BrushStyleComboBox->addItem(tr("Dense4Pattern"),static_cast<int>(Qt::Dense4Pattern));
    BrushStyleComboBox->addItem(tr("Dense5Pattern"),static_cast<int>(Qt::Dense5Pattern));
    BrushStyleComboBox->addItem(tr("Dense6Pattern"),static_cast<int>(Qt::Dense6Pattern));
    BrushStyleComboBox->addItem(tr("Dense7Pattern"),static_cast<int>(Qt::Dense7Pattern));
    BrushStyleComboBox->addItem(tr("HorPattern"),static_cast<int>(Qt::HorPattern));
    BrushStyleComboBox->addItem(tr("VerPattern"),static_cast<int>(Qt::VerPattern));
    BrushStyleComboBox->addItem(tr("CrossPattern"),static_cast<int>(Qt::CrossPattern));
    BrushStyleComboBox->addItem(tr("BDiagPattern"),static_cast<int>(Qt::BDiagPattern));
    BrushStyleComboBox->addItem(tr("FDiagPattern"),static_cast<int>(Qt::FDiagPattern));
    BrushStyleComboBox->addItem(tr("DiagCrossPattern"),static_cast<int>(Qt::DiagCrossPattern));
    BrushStyleComboBox->addItem(tr("LinearGradientPattern"),static_cast<int>(Qt::LinearGradientPattern));
    BrushStyleComboBox->addItem(tr("ConicalGradientPattern"),static_cast<int>(Qt::ConicalGradientPattern));
    BrushStyleComboBox->addItem(tr("RadialGradientPattern"),static_cast<int>(Qt::RadialGradientPattern));
    BrushStyleComboBox->addItem(tr("TexturePattern"),static_cast<int>(Qt::TexturePattern));
    connect(BrushStyleComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
            this, &PaintEx::ShowBrush);

    //布局开始
    RightLayout = new QGridLayout;
    RightLayout->addWidget(ShapeLabel,0,0);
    RightLayout->addWidget(shapeComboBox,0,1);
    RightLayout->addWidget(PenColorLabel,1,0);
    RightLayout->addWidget(PenColorFrame,1,1);
    RightLayout->addWidget(PenColorBtn,1,2);
    RightLayout->addWidget(PenWidthLabel,2,0);
    RightLayout->addWidget(PenWidthSpinBox,2,1);
    RightLayout->addWidget(PenStyleLabel,3,0);
    RightLayout->addWidget(PenStyleComboBox,3,1);
    RightLayout->addWidget(PenCapLabel,4,0);
    RightLayout->addWidget(PenCapComboBox,4,1);
    RightLayout->addWidget(PenJoinLabel,5,0);
    RightLayout->addWidget(PenJoinComboBox,5,1);
    RightLayout->addWidget(FillRuleLabel,6,0);
    RightLayout->addWidget(FillRuleComboBox,6,1);
    RightLayout->addWidget(SpreadLabel,7,0);
    RightLayout->addWidget(SpreadComboBox,7,1);
    RightLayout->addWidget(BrushColorLabel,8,0);
    RightLayout->addWidget(BrushColorFrame,8,1);
    RightLayout->addWidget(BrushColorBtn,8,2);
    RightLayout->addWidget(BrushStyleLabel,9,0);
    RightLayout->addWidget(BrushStyleComboBox,9,1);
    QHBoxLayout* MainLayout = new QHBoxLayout(this);
    MainLayout->addWidget(paintArea);
    MainLayout->addLayout(RightLayout);
    MainLayout->setStretchFactor(paintArea,1);
    MainLayout->setStretchFactor(RightLayout,0);

    ShowShape(shapeComboBox->currentIndex());










}

PaintEx::~PaintEx() {}

void PaintEx::ShowShape(int value)
{
    PaintArea::Shape shape = PaintArea::Shape(shapeComboBox->itemData(value,Qt::UserRole).toInt());
    paintArea->SetShape(shape);

}

void PaintEx::ShowPenWidth(int nValue)
{
    QColor Color = PenColorFrame->palette().color(QPalette::Window);
    Qt::PenStyle Style = Qt::PenStyle(PenStyleComboBox->itemData(PenStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle Cap = Qt::PenCapStyle(PenCapComboBox->itemData(PenCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle Join = Qt::PenJoinStyle(PenJoinComboBox->itemData(PenJoinComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->SetPen(QPen(Color,nValue,Style,Cap,Join));
}

void PaintEx::ShowPenColor()
{
    QColor Color = QColorDialog::getColor(static_cast<int>(Qt::blue));
    PenColorFrame->setPalette(QPalette(Color));
    int nValue = PenWidthSpinBox->value();
    Qt::PenStyle Style = Qt::PenStyle(PenStyleComboBox->itemData(PenStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle Cap = Qt::PenCapStyle(PenCapComboBox->itemData(PenCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle Join = Qt::PenJoinStyle(PenJoinComboBox->itemData(PenJoinComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->SetPen(QPen(Color,nValue,Style,Cap,Join));

}

void PaintEx::ShowPenStyle(int StyleValue)
{
    QColor Color = PenColorFrame->palette().color(QPalette::Window);
    int nValue = PenWidthSpinBox->value();
    Qt::PenStyle Style = Qt::PenStyle(PenStyleComboBox->itemData(StyleValue,Qt::UserRole).toInt());
    Qt::PenCapStyle Cap = Qt::PenCapStyle(PenCapComboBox->itemData(PenCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle Join = Qt::PenJoinStyle(PenJoinComboBox->itemData(PenJoinComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->SetPen(QPen(Color,nValue,Style,Cap,Join));

}

void PaintEx::ShowPenCap(int capValue)
{
    QColor Color = PenColorFrame->palette().color(QPalette::Window);
    int nValue = PenWidthSpinBox->value();
    Qt::PenStyle Style = Qt::PenStyle(PenStyleComboBox->itemData(PenStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle Cap = Qt::PenCapStyle(PenCapComboBox->itemData(capValue,Qt::UserRole).toInt());
    Qt::PenJoinStyle Join = Qt::PenJoinStyle(PenJoinComboBox->itemData(PenJoinComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->SetPen(QPen(Color,nValue,Style,Cap,Join));
}

void PaintEx::ShowPenJoin(int JoinValue)
{
    QColor Color = PenColorFrame->palette().color(QPalette::Window);
    int nValue = PenWidthSpinBox->value();
    Qt::PenStyle Style = Qt::PenStyle(PenStyleComboBox->itemData(PenStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle Cap = Qt::PenCapStyle(PenCapComboBox->itemData(PenCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle Join = Qt::PenJoinStyle(PenJoinComboBox->itemData(JoinValue,Qt::UserRole).toInt());
    paintArea->SetPen(QPen(Color,nValue,Style,Cap,Join));
}

void PaintEx::ShowSpreadStyle()
{
    spread = QGradient::Spread(SpreadComboBox->itemData(SpreadComboBox->currentIndex(),Qt::UserRole).toInt());

}

void PaintEx::ShowFillRule()
{
    Qt::FillRule rule =Qt::FillRule(FillRuleComboBox->itemData(FillRuleComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->SetFillRule(rule);
}

void PaintEx::ShowBrushColor()
{
    QColor Color = QColorDialog::getColor(static_cast<int>(Qt::blue));
    BrushColorFrame->setPalette(QPalette(Color));
    ShowBrush(BrushStyleComboBox->currentIndex());

}

void PaintEx::ShowBrush(int value)
{
    QColor Color = BrushColorFrame->palette().color(QPalette::Window);
    Qt::BrushStyle style = Qt::BrushStyle(BrushStyleComboBox->itemData(value,Qt::UserRole).toInt());
    if(style == Qt::LinearGradientPattern)
    {
        QLinearGradient LinearGradient(0,0,400,400);
        LinearGradient.setColorAt(0.0,Qt::white);
        LinearGradient.setColorAt(0.2,Color);
        LinearGradient.setColorAt(1.0,Qt::black);
        LinearGradient.setSpread(spread);
        paintArea->SetBrush(LinearGradient);
    }
    else if(style == Qt::RadialGradientPattern)
    {
        QRadialGradient RadialGradient(200,200,150,150,100);
        RadialGradient.setColorAt(0.0,Qt::white);
        RadialGradient.setColorAt(0.2,Color);
        RadialGradient.setColorAt(1.0,Qt::black);
        RadialGradient.setSpread(spread);
        paintArea->SetBrush(RadialGradient);
    }
    else if(style == Qt::ConicalGradientPattern)
    {
        QConicalGradient conicalGradient(200,200,30);
        conicalGradient.setColorAt(0.0,Qt::white);
        conicalGradient.setColorAt(0.2,Color);
        conicalGradient.setColorAt(1.0,Qt::black);
        conicalGradient.setSpread(spread);
        paintArea->SetBrush(conicalGradient);
    }
    else if(style == Qt::TexturePattern)
    {
        paintArea->SetBrush(QBrush(QPixmap("312.png")));

    }
    else
    {
        paintArea->SetBrush(QBrush(Color,style));
    }
}

main.cpp

#include "paintex.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    PaintEx w;
    w.show();
    return a.exec();
}

运行图

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt是一个强大的跨平台应用程序开发框架,其中包括对串口通讯的支持。在Qt中,你可以使用QSerialPort模块来进行串口数据的接收和发送。图形绘制方面,Qt提供了QGraphicsView和QGraphicsScene等组件,用于创建直观的界面来显示串口数据。 以下是使用Qt进行串口通讯和图形绘制的基本步骤: 1. **设置串口连接**: - 创建QSerialPort对象并打开串口。 ```cpp QSerialPort serialPort; if (!serialPort.open(QIODevice::ReadWrite)) { qCritical() << "Failed to open serial port: " << serialPort.errorString(); } ``` 2. **数据接收与处理**: - 定义一个槽函数处理接收到的数据,并可能将其转换成图形可显示的形式(例如,电压、温度等数值)。 ```cpp void handleDataReceived(const QByteArray &data) { // 处理接收到的数据,并将其添加到图形模型中 } serialPort.readyRead.connect(handleDataReceived); ``` 3. **图形绘制**: - 使用QGraphicsScene和QGraphicsView创建一个可视化的区域。可以使用QGraphicsLineItem、QGraphicsPixmapItem或其他图形元素来表示数据点或趋势线。 ```cpp QGraphicsScene scene; QGraphicsView view.setScene(&scene); QGraphicsRectItem plotItem = new QGraphicsRectItem(0, 0, width, height, &scene); ``` - 根据数据更新图形,如在plotItem的位置上画线或点。 4. **定时刷新和数据更新**: - 可能需要设置一个定时器定期获取新数据并更新图形。 ```cpp QTimer timer; timer.singleShot(0, []() { handleNewData(); timer.start(interval); }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值