QT--绘制正弦波与余弦波切换视图

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT
private:
    QTimer *t1;
    int count;
    QPushButton *btn_sin;
    QPushButton *btn_cos;
public slots:
    void handleTimeoue();
protected:
    void paintEvent(QPaintEvent *);
public:
    Widget(QWidget *parent = 0);
    ~Widget();


};

#endif // WIDGET_H
#include "Widget.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
Widget::Widget(QWidget *parent)
    : QWidget(parent),count(0)
{
//    btn_sin = new QPushButton(this);
//    btn_sin->move(10,0);
//    btn_sin->setText("Sin");

    t1 = new QTimer(this);
    connect(t1,SIGNAL(timeout()),this,SLOT(handleTimeoue()));
    t1->start(1000);


}
void Widget::handleTimeoue()
{
    count++;
    count = count % 2;
    update();
    qDebug() << "Widget::handleTimeoue()"<<count;
}
void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPen pen;

    pen.setColor(Qt::green);
    pen.setStyle(Qt::SolidLine);
    pen.setWidthF(0.01);

    painter.setPen(pen);

    painter.setViewport(50, 50, width()-100, height()-100);
    painter.setWindow(-10, 2, 20, -4); // (-10, 2)    (10, -2)

    painter.fillRect(-10, 2, 20, -4, Qt::black);

    painter.drawLine(QPointF(-10, 0), QPointF(10, 0));   // x
    painter.drawLine(QPointF(0, 2), QPointF(0, -2));     // y
    if(count == 1)
    {
       for(float x=-10; x<10; x+=0.01)
       {
           float y = qSin(x);

           painter.drawPoint(QPointF(x, y));
       }
    }
    else if(count == 0 )
    {
        for(float x= -10; x<10; x+=0.01)
        {
            float y = qCos(x);
            painter.drawPoint(QPointF(x, y));
        }
    }
}

Widget::~Widget()
{
    
}
#include 
#include "Widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    
    return a.exec();
}

你可能感兴趣的:(QT)