您的位置:首页 > 编程语言 > Qt开发

重写Qt中paintEvent事件画5个参数的椭圆

2015-05-30 10:43 288 查看
.h文件

#ifndef QPAINTERELLIPSE_H
#define QPAINTERELLIPSE_H

#include <QtWidgets/QWidget>
#include "ui_qpainterellipse.h"

class QPaintEvent;

class QPainterEllipse : public QWidget
{
Q_OBJECT

public:
QPainterEllipse(QWidget *parent = 0);
~QPainterEllipse();

void paintEvent(QPaintEvent *event);

private:
Ui::QPainterEllipseClass ui;
};

#endif // QPAINTERELLIPSE_H


.cpp文件

#include "qpainterellipse.h"
#include <QtGui/QtGui>
#include <iostream>

const int m = 300;  //center's coordinate x of ellipse
const int n = 300;  //center's coordinate y of ellipse
const int a = 100;  //half of the length of long axis
const int b = 50;   //half of the length of short axis
const double theta = -45; //angle between long axis and x axis
const double PI = 3.1415926;

QPainterEllipse::QPainterEllipse(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}

QPainterEllipse::~QPainterEllipse()
{

}

void QPainterEllipse::paintEvent(QPaintEvent *event)
{
QPixmap inputImage;
inputImage.load("inputImage.bmp");
QPainter painter(&inputImage);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::white, 2, Qt::SolidLine, Qt::RoundCap));

QList<QPoint> m_listPoint;
for (double x = 200; x <= 400; )
{
double temp = sqrt((1 - (x - m)*(x - m) / (a*a)) * (b*b));
double y1 = temp + n;
m_listPoint.append(QPoint(x,y1));
x += 3;
}
for (double x = 400; x >= 200;)
{
double temp = sqrt((1 - (x - m)*(x - m) / (a*a)) * (b*b));
double y2 = -temp + n;
m_listPoint.append(QPoint(x, y2));
x -= 3;
}

double alpha = theta * PI / 180;
for (auto it = m_listPoint.begin(); it != m_listPoint.end(); it++)
{
(*it).setX((((*it).x() - m)*cos(alpha) - ((*it).y() - n)*sin(alpha)+m));
(*it).setY((((*it).x() - m)*sin(alpha) + ((*it).y() - n)*cos(alpha)+n));
}

for (auto it = m_listPoint.cbegin(); it != m_listPoint.cend();it++)
{
painter.drawPoint(*it);
}
inputImage.save("outputPointImage.bmp");

for (auto it = m_listPoint.cbegin(); it != m_listPoint.cend() - 1; it++)
{
painter.drawLine(*it, *(it + 1));
}
inputImage.save("outputLineImage.bmp");

for (auto it = m_listPoint.cbegin(); it != m_listPoint.cend(); it++)
{
qDebug() << *it;
}
}


需要读入一个inputImage.bmp,在这个图上画。当然也可以设置一个画布。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: