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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바스크립트로 타이머 만들기JavaScript와 HTML만 사용하여 간단한 타이머를 만들어 보겠습니다. 먼저 인터페이스를 만들고 HTML만 사용하여 간단한 작업을 수행합니다. HTML 구조에서 시간 정보를 표시하기 위해 일부span가 생성되었...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.