#ifndef RENDERAREA_H
#define RENDERAREA_H
#include <QWidget>
class RenderArea : public QWidget
{
Q_OBJECT
public:
explicit RenderArea(QWidget *parent = 0);
void setDrawType(QString type);
protected:
void paintEvent(QPaintEvent *);
private:
QString drawType;
signals:
public slots:
};
#endif // RENDERAREA_H
#include "renderarea.h"
#include <QtWidgets>
#include <QPixmap>
#include <QPainter>
RenderArea::RenderArea(QWidget *parent) : QWidget(parent)
{
}
void RenderArea::setDrawType(QString type)
{
drawType = type;
update();
}
void RenderArea::paintEvent(QPaintEvent *)
{
static const QPoint points[4] ={
QPoint(10,8),
QPoint(20,100),
QPoint(80,30),
QPoint(90,70)
};
QRect rect(10,20,80,60);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.fillRect(this->rect(),Qt::white); //设置背景
painter.setBrush(Qt::red); //设置字体
QFont font("Times",15,QFont::Bold);
painter.setPen(Qt::blue); //设置画笔颜色
painter.setFont(font);
if(this->drawType == "Line")
{
// painter.drawLine(10,10,100,10);
painter.drawLine(rect.bottomLeft(),rect.topRight());
}
else if(drawType =="Points")
{
painter.drawPoints(points,4);
}
else if(drawType == "Polyline")
{
painter.drawPolyline(points,4);
}
else if(drawType == "Polygon")
{
painter.drawPolygon(points,4);
}
else if(drawType == "Rect")
{
painter.drawRect(30,40,120,80);
}
else if(drawType == "Ellipse")
{
painter.drawRect(30,40,120,80);
painter.drawEllipse(30,40,120,80);
}
else if(drawType == "Arc")//圆弧
{
painter.drawArc(30,40,120,80,60*16,120*16);//坐标 长宽 起始角 圆弧角度
}
else if(drawType == "Chord")
{
painter.drawChord(30,40,120,80,60*16,120*16);
}
else if(drawType == "Pie")
{
painter.drawPie(30,40,120,80,60*16,120*16);
}
else if(drawType == "RoundedRect")
{
painter.drawRoundedRect(30,40,120,80,50,50,Qt::RelativeSize);//50 50 是弧形率大小
painter.drawRoundedRect(160,40,120,80,100,100,Qt::RelativeSize);
// painter.drawRoundedRect(160,40,120,80,100,100,Qt::AbsoluteSize);//相对
}
else if(drawType == "Text")
{
painter.drawText(30,40,120,80,Qt::AlignCenter,tr("Hello Qt!"));
}
else if(drawType == "Pixmap")
{
painter.drawPixmap(10,10,QPixmap(":/new/prefix1/8.png"));
}
else if(drawType == "Image")
{
QImage image("/mnt/Qt/test/untitled18/8.png");
painter.drawImage(10,10,image);
}
else if(drawType == "Twoline")
{
QPainterPath path;
path.moveTo(points[3]); //起点
path.quadTo(points[2],points[1]);//一个控制点 终点
painter.drawPath(path);
}
else if(drawType == "Threeline")
{
QPainterPath path;
path.moveTo(points[0]);//起点
path.cubicTo(points[1],points[2],points[3]); //两个控制点 终点
painter.drawPath(path);
}
else if(drawType == "Path")
{
QPainterPath path;
path.moveTo(20,80);
path.lineTo(50,400);
path.cubicTo(80,0,50,50,80,80);
painter.drawPath(path);
}
}
/*--------------------------------------------------------------------------------------*/
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtWidgets>
#include "renderarea.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void onRadioButtonClicked();
private:
Ui::Widget *ui;
QGroupBox *groupBox ;
RenderArea *renderArea;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QObject>
static const QString drawType[16]={
"Line" ,"Points","Polyline","Polygon",
"Rect","Ellipse","Arc","Chord",
"Pie","RoundedRect","Text","Pixmap",
"Image","Twoline","Threeline","Path"
};
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
groupBox = new QGroupBox(this);
QVBoxLayout *layout = new QVBoxLayout;
for(int i=0;i<16;++i)
{
QRadioButton *radioButton = new QRadioButton(drawType[i]);
connect(radioButton,SIGNAL(clicked(bool)),this,SLOT(onRadioButtonClicked()));
layout->addWidget(radioButton);
}
groupBox->setLayout(layout);
renderArea = new RenderArea(this);
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->setColumnStretch(0,3);
gridLayout->setColumnStretch(1,1);
gridLayout->addWidget(renderArea);
gridLayout->addWidget(groupBox);
setLayout(gridLayout);
resize(500,500);
}
void Widget::onRadioButtonClicked()
{
QRadioButton *sender = (QRadioButton*)QObject::sender();
renderArea->setDrawType(sender->text());
}
Widget::~Widget()
{
delete ui;
}
/*------------------------------------------------------------------------------------*/
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}