QPainter绘制图形

painerarea.h

    #ifndef PAINERAREA_H
    #define PAINERAREA_H
    
    #include <QWidget>
    //QPen 钢板(画笔)是基本的图形对象,绘制直线,曲线,多边形等形状
    #include<QPen>
    //QBrush画刷是基本的图形对象,主要用于填充,比如举行矩形、多边形等形状
    #include<QBrush>
    #include<QPainter>
    class PainerArea : public QWidget
    {
        Q_OBJECT
    public:
        explicit PainerArea(QWidget *parent = nullptr);
        //绘制直线、长方形(矩形)
        enum shape{Line,Rectangle};
        void setShape(shape);//此函数实现绘制形状
        void setPen(QPen);
        void setBrush(QBrush);
    
        void paintEvent(QPaintEvent *);//重绘事件
        void setfillrule(Qt::FillRule);//填充规则
    
    
    
    signals:
    public slots:
    private:
    
        shape shp;
        QPen pen;
        QBrush qbrsh;
        Qt::FillRule fillrle;
    };
    
    #endif // PAINERAREA_H

widget.hMAIN

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include"painerarea.h"
#include<QLabel>
#include<QComboBox>
#include<QGridLayout>
#include<QColorDialog>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
private:
    PainerArea *labelshapeArea;

    QLabel *labelshape;
    QComboBox *comboboxshape;
    QGridLayout *gridlayout;

private slots:
    void disShapeFunc(int );
};
#endif // WIDGET_H

main.cpp

#include "widget.h"

#include <QApplication>

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

 painerarea.cpp

#include "painerarea.h"

PainerArea::PainerArea(QWidget *parent) : QWidget(parent)
{
    setPalette(QPalette(Qt::blue));//调色板来改变背景颜色
    setAutoFillBackground(true);//Qt窗口是否需要绘制背景颜色

    //设置最小尺寸
    setMinimumSize(410,410);

}
void PainerArea::setShape(shape sh)//此函数实现绘制形状
{
    shp =sh;
    update();
}
void PainerArea::setPen(QPen sh)
{
    pen=sh;
    update();
}
void PainerArea::setBrush(QBrush sh)

{
    qbrsh=sh;
    update();
}

void PainerArea::paintEvent(QPaintEvent * )//重绘事件
{
     QPainter p(this);
     p.setPen(pen);
     p.setBrush(qbrsh);

    QRect rect(55,110,290,180);
    switch (shp) {
    case Line:
       p.drawLine(rect.topLeft(),rect.bottomRight());
        break;
     case Rectangle:
        p.drawRect(rect);
        break;
    default:
        break;

    }

}
void PainerArea::PainerArea::setfillrule(Qt::FillRule rle)//填充规则
{
        fillrle=rle;
        update();//重新绘制区域窗体
}

widget.h

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    labelshapeArea =new PainerArea;
    gridlayout=new QGridLayout(this);//控制面板的布局
    setWindowTitle("Qt绘图框架测试:");
    labelshape =new QLabel("绘图形状");//右边有一个下拉列表框
    comboboxshape=new QComboBox;
    comboboxshape->addItem("Line",PainerArea::Line);
    comboboxshape->addItem("Rectangle",PainerArea::Rectangle);

    connect(comboboxshape,SIGNAL(activated(this)),this,SLOT(disShapeFunc(int )));
    gridlayout->addWidget(labelshape,0,0);
    gridlayout->addWidget(comboboxshape,0,1);

     QHBoxLayout *mainlayout=new QHBoxLayout(this);//整体布局
     mainlayout->addWidget(labelshape);//向布局添加控件
        mainlayout->addLayout(gridlayout);//向整体布局添加布局
}

Widget::~Widget()
{
    delete ui;
}
    void Widget::disShapeFunc(int v)
    {
   PainerArea::shape sp=PainerArea::shape(comboboxshape->itemData(v,Qt::UserRole).toInt());
   labelshapeArea->setShape(sp);

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值