重写Qt中paintEvent事件画5个参数的椭圆
2015-05-30 10:43
288 查看
.h文件
.cpp文件
需要读入一个inputImage.bmp,在这个图上画。当然也可以设置一个画布。
#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,在这个图上画。当然也可以设置一个画布。
相关文章推荐
- Qt小软件:TsTranslationTool(ts字符串文件翻译器)
- qt程序移植到开发板
- Qt 加载动态链接库
- QT使用UAC(经过验证)
- PyQt4学习
- 初学Qt
- QString之arg使用一则(QTBUG19027)
- Qt5.3 For Andoid 安装过程
- Qt Creator编码设置--win7-vs2010
- 更新采购订单和销售订单qty
- QT创建透明对话框
- ubuntu12.04下Qt调试器的使用
- VS2013建立Qt程序错误处理
- Qt在Mac OS X下的编程环境搭建
- Qt环境配置
- QT 之 sendEvent & PostEvent
- qt中全局变量的用法
- PyQt4初体验
- Qt信号与槽
- QT之数据库 和 GraphicsView使用