zoukankan      html  css  js  c++  java
  • 用Qt 画一个心形

    MainWindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QTimer>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        void timerEvent(QTimerEvent *);
        void paintEvent(QPaintEvent *event);
    
    private:
        int m_x,m_y;
        int m_k;
        float m_t;
    };
    
    #endif // MAINWINDOW_H
    MainWindow.cpp
    #include "mainwindow.h"
    #include <QDebug>
    #include <math.h>
    #include <QPainter>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        this->setFixedSize(600,600);
        m_k = 10;
        m_t = 0;
        startTimer(1);
    
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    
    void MainWindow::timerEvent(QTimerEvent *)
    {
        qDebug() << "timer event " << m_t;
        if (m_t > 500)
        {
            update();
            m_t = 0;
        }
        m_x = 16 * m_k * sin(m_k*m_t)*sin(m_k*m_t)*sin(m_k*m_t);
        m_y = 13 * m_k * cos(m_k*m_t) - 5 * m_k * cos(2 * m_k * m_t) - 2 * m_k * cos(3 * m_k * m_t) - cos(4 * m_k * m_t);
        m_x += this->width() / 2;
        m_y -= this->height() / 2;
        m_y *= -1;
        update(m_x,m_y,1,1);
    
        m_t += 0.1;
    }
    
    #if 1
    void MainWindow::paintEvent(QPaintEvent *event)
    {
        qDebug() << "paintEvent " << m_x << " " << m_y;
        QPainter painter(this);
        painter.setPen(Qt::red);
        painter.drawPoint(m_x, m_y);
    }
    #else
    void MainWindow::paintEvent(QPaintEvent *event)
    {
       double k = 10;
       QPainter painter(this);
       painter.setRenderHint(QPainter::Antialiasing, true);
       QColor my_color(237, 162, 255, 255);
       QBrush my_brush(my_color);
       painter.setPen(Qt::red);
       /*painter.setBrush(my_brush);*/
       painter.translate(this->width()/2, this->height()/2);
       QPainterPath polygonPath;
       polygonPath.setFillRule(Qt::WindingFill);
       float x = 16 * k * sin(0.0)*sin(0.0)*sin(0.0);
       float y = 13 * k * cos(0.0) - 5 * k*cos(0.0) - 2 * k*cos(0.0) - cos(0.0);
       polygonPath.moveTo(x, -y);
       for (double t = 0.01; t < 100; t += 0.05)
       {
           x = 16 * k * sin(k*t)*sin(k*t)*sin(k*t);
           y = 13 * k * cos(k*t) - 5 * k * cos(2 * k * t) - 2 * k * cos(3 * k * t) - cos(4 * k * t);
           polygonPath.lineTo(x, -y);
    #if 0
           painter.drawPoint(x,-y);
           painter.drawLine(0,0,x,-y);
           painter.drawLine(0,0,x+100,-y);
    #endif
       }
       painter.drawPath(polygonPath);
    }
    #endif
  • 相关阅读:
    linux 解压tgz 文件指令
    shell 脚本没有执行权限 报错 bash: ./myshell.sh: Permission denied
    linux 启动solr 报错 Your Max Processes Limit is currently 31202. It should be set to 65000 to avoid operational disruption.
    远程查询批量导入数据
    修改 MZTreeView 赋权节点父节点选中子节点自动选中的问题
    关于乱码的问题解决记录
    我的网站优化之路
    对设计及重构的一点反思
    我的五年岁月
    奔三的路上
  • 原文地址:https://www.cnblogs.com/nanqiang/p/11542164.html
Copyright © 2011-2022 走看看