qt 접선도 표시 데이터

앞에도 접선에 관한 게 하나 썼는데 그게 너무...여름방학에 할 일이 없어서 집에서 하나 더 만들었는데 적응성과 확장성이 좀 있었으면 좋겠어요.
                    
다음은 코드 구현입니다.
     
/*************************************************

     

author   

2012-8-14

*************************************************/

#ifndef LINEGRAPHWIDGET_H

#define LINEGRAPHWIDGET_H

#include <QWidget>

#include <QPen>

#include <QPainter>

#include <QResizeEvent>

//#include <QDebug>

#include <QTimer>

struct point

{

int x;

int y;

};

class LineGraphWidget : public QWidget

{

Q_OBJECT

public:

//mix max    y      

explicit LineGraphWidget(int mix, int max,QWidget *parent = 0);

void setSingleUnitX(const int &value) {singleUnitX = value;}//  x            ,  10

//         

void startUpdataTimer(int msecond);

void stopUpdataTimer();

signals:

//      

void newDataCome();//                 

public slots:

void showData(const int &data);//         

protected:

void paintEvent(QPaintEvent *);

void resizeEvent(QResizeEvent *e);

private:

QList<int> xList;

QList<int> yList;

int Counter;

//      

int widgetX;

int widgetY;

//        

int rangeMin;

int rangeMax;

//             ,x ,y 

int singleUnitX;

int singleUnitY;

//            ,x ,y 

int sumUnitX;

int sumUnitY;

point basePoint;//      

point xAxisEndPoint;//X    

point yAxisEndPoint;//Y    

QTimer *timer;

void getSumUnitX();

void getSingleUnitY();

void initGraph();

};

#endif // LINEGRAPHWIDGET_H

#include "linegraphwidget.h"

LineGraphWidget::LineGraphWidget(int mix, int max, QWidget *parent) :

QWidget(parent),

rangeMin(mix),

rangeMax(max),

singleUnitX(10),

timer(NULL)

{

connect(this,SIGNAL(newDataCome()),this,SLOT(update()));

}

void LineGraphWidget::paintEvent(QPaintEvent *)

{

int i;

int a,b,c,d;

Counter = 0;

QPixmap pix(widgetX,widgetY);

QPainter painter(&pix);

pix.fill(Qt::white);

QPen pen;

pen.setBrush(Qt::darkGreen);

painter.setPen(pen);

QPen pen1;

pen1.setBrush(Qt::black);

painter.setRenderHint(QPainter::Antialiasing);

for(i = 0;i < sumUnitX; ++i) {

a = basePoint.x + i*singleUnitX;

b = basePoint.y;

c= a;

d = yAxisEndPoint.y;

painter.drawLine(a,b,c,d);

}

for(i = 0; i < sumUnitY+1; ++i) {

a = basePoint.x;

b = basePoint.y - singleUnitY*i;

c = xAxisEndPoint.x;

d = b;

painter.drawLine(a,b,c,d);

}

painter.setPen(pen1);

for (i = 0;i < (sumUnitY/2+1);++i) {

QRectF rectF(0,basePoint.y - singleUnitY*i*2 - 5,basePoint.x,(widgetY - basePoint.y)/3);

painter.drawText(rectF,Qt::AlignCenter,QString::number(rangeMin+i*20));

}

for (i = 0;i < sumUnitX/5;++i) {

QRectF rectF(basePoint.x+singleUnitX*(5*i+3),basePoint.y,singleUnitX*4,widgetY-basePoint.y);

painter.drawText(rectF,Qt::AlignCenter,QString::number(5*(i+1)*singleUnitX));

}

int size = xList.size();

while(Counter < (size -1)) {

painter.drawLine(basePoint.x + xList.value(Counter)*singleUnitX,

basePoint.y - ((yList.value(Counter) - rangeMin)/10.0)*singleUnitY,

basePoint.x + xList.value(Counter + 1)*singleUnitX,

basePoint.y - ((yList.value(Counter + 1) - rangeMin)/10.0)*singleUnitY);

Counter++;

}

painter.end();

painter.begin(this);

painter.drawPixmap(0,0,pix);

painter.end();

Counter = 0;

}

void LineGraphWidget::resizeEvent(QResizeEvent * e)

{

if (e->oldSize() != size()) {

initGraph();

}

}

void LineGraphWidget::showData(const int &data)

{

//                ,        

if (data > rangeMax || data < rangeMin) {

return;

}

int xsize = xList.size();

int yValue = data;

if (xsize == sumUnitX) {

xList.clear();

yList.clear();

xsize = xList.length();

}

if (xsize >= 1) {

xList.insert(xsize,xsize);

yList.insert(xsize,yValue);

emit newDataCome();

}

else {

xList.insert(xsize,xsize);

yList.insert(xsize,yValue);

}

}

void LineGraphWidget::getSumUnitX()

{

if ((static_cast<int>(widgetX*0.85))%singleUnitX == 0){

sumUnitX = (static_cast<int>(widgetX*0.85))/singleUnitX;

}

else {

sumUnitX = (static_cast<int>(widgetX*0.85))/singleUnitX + 1;

}

}

void LineGraphWidget::getSingleUnitY()

{

int range = rangeMax - rangeMin;

if (range == 0) {

sumUnitY = range/10 + 1;

}

else {

sumUnitY = range/10 + 2;

}

singleUnitY = (static_cast<int>(widgetY*0.85))/sumUnitY;

}

void LineGraphWidget::initGraph()

{

widgetX = size().width();

widgetY = size().height();

basePoint.x = static_cast<int>(widgetX*0.1);

basePoint.y = static_cast<int>(widgetY*0.9);

yAxisEndPoint.x = basePoint.x;

yAxisEndPoint.y = static_cast<int>(widgetY*0.05);

xAxisEndPoint.x = static_cast<int>(widgetX*0.95);

xAxisEndPoint.y = basePoint.y;

getSumUnitX();

getSingleUnitY();

}

void LineGraphWidget::startUpdataTimer(int msecond)

{

if (timer != NULL) {

if (timer->isActive()) {

timer->stop();

}

delete timer;

}

disconnect(SIGNAL(newDataCome()));

timer = new QTimer;

connect(timer,SIGNAL(timeout()),this,SLOT(update()));

timer->start(msecond);

}

void LineGraphWidget::stopUpdataTimer()

{

if (timer == NULL) {

}

else {

timer->stop();

delete timer;

}

}

이 블로그는 작년 여름방학에 쓴 것입니다. 다음 인형들은main 함수와 어떻게 쓰는지 고민하고 있습니다. 저는...나는 이것이 문제가 아니라고 생각한다.
제가 코드를 주석해 드릴게요.
/*************************************************
       
  author   
  2012-8-14

        

   void showData(int data);         
   void setSingleUnitX(const int &value);  x      
   void startUpdataTimer(int msecond);        
   void stopUpdataTimer();     

  *************************************************/

#ifndef LINEGRAPHWIDGET_H
#define LINEGRAPHWIDGET_H

#include <QWidget>
#include <QPen>
#include <QPainter>
#include <QResizeEvent>
//#include <QDebug>
#include <QTimer>

struct point
{
    int x;
    int y;
};

class LineGraphWidget : public QWidget
{
    Q_OBJECT
public:
    //mix max    y      
    explicit LineGraphWidget(int min, int max, QWidget *parent = 0);
    void setSingleUnitX(const int &value) {singleUnitX = value;}//  x            ,  10
    //         
    void startUpdataTimer(int msecond);
    void stopUpdataTimer();
signals:
    //      
    void newDataCome();//                 
public slots:
    /**
     * @brief showData
     * @param adress   
     * @param _data   
     */
    void showData(const QString& adress,int _data);//         
    /**
     * @brief slt_showData       
     * @param name    
     * @param _data   
     */
    void slt_showData(ushort name,int _data);
protected:
    void paintEvent(QPaintEvent *);
    void resizeEvent(QResizeEvent *e);
private:
    QString  m_currentNodeAdress;//          
    ushort m_currentNodeName;//        

    QList<int> xList;
    QList<int> yList;
    int Counter;



    //      
    int widgetX;
    int widgetY;
    //        
    int rangeMin;
    int rangeMax;
    //             ,x ,y 
    int singleUnitX;
    int singleUnitY;

    //            ,x ,y 
    int sumUnitX;
    int sumUnitY;

    point basePoint;//      

    point xAxisEndPoint;//X    

    point yAxisEndPoint;//Y    

    QTimer *timer;

    void getSumUnitX();
    void getSingleUnitY();

    void initGraph();

    void flush();//                    ,    ,      


};

#endif // LINEGRAPHWIDGET_H

좋은 웹페이지 즐겨찾기