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);
}