728x90

QCustomPlot은 Qt에서 사용할 수 있는 데이터 시각화 위젯입니다.

 

Qt Plotting Widget QCustomPlot - Introduction

QCustomPlot is a Qt C++ widget for plotting and data visualization. It has no further dependencies and is well documented. This plotting library focuses on making good looking, publication quality 2D plots, graphs and charts, as well as offering high perfo

www.qcustomplot.com

 

 

QCustomPlot 2.0.1 Documentation

If you are new to QCustomPlot and just want to start using it, it's recommended to look at the tutorials and examples at http://www.qcustomplot.com/ This documentation is especially helpful as a reference, when you're familiar with the basics of data visua

www.qcustomplot.com

먼저 홈페이지의 좌측 배너에서 Download로 들어가 알집 파일을 다운받아줍니다.

다운 후 압축을 해제한 후 Project를 우클릭하여 Add Existing Files로 qcustomplot 헤더파일과 소스파일을 추가합니다.

프로젝트 파일에 아래와 같이 추가합니다.

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
TARGET = qcustomplot
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    qcustomplot/qcustomplot.cpp

HEADERS  += mainwindow.h \
    qcustomplot/qcustomplot.h

FORMS    += mainwindow.ui

이제 ui파일로 가서 QWidget을 추가하고 우클릭하여 Promote to를 눌러줍니다.

QCustomPlot을 입력 후 Add 후 Promote를 눌러줍니다. Header file에는 경로까지 포함하여 적어줍니다.

이제 (0,0)~(100,100)의 좌표를 그래프에 그려보겠습니다.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    insertValue();

    ui->plot->addGraph();
    ui->plot->graph(0)->setData(vec_x, vec_y);
    ui->plot->xAxis->setLabel("x");
    ui->plot->yAxis->setLabel("y");

    ui->plot->xAxis->setRange(0,100);
    ui->plot->yAxis->setRange(0,100);

    ui->plot->replot();		// 그래프 그림
}

void MainWindow::insertValue()
{
    for(int i = 0; i < 100 ; i++) {
        vec_x.append(i);
        vec_y.append(i);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

이제 여기서 밋밋한 그래프의 모양을 바꿔보겠습니다. 

  • 라인의 모양 바꾸기

graph의 setLineStyle 함수 안에 상수값을 넣어 바꿔줍니다. 기본은 QCPGraph::lsLine입니다.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ...

    ui->plot->graph(0)->setLineStyle(QCPGraph::lsLine);
    ui->plot->replot();
}

QCPGraph::lsStepLeft
QCPGraph::lsStepRight
QCPGraph::lsStepCenter
QCPGraph::lsImpluse

  • 데이터에 점찍기

라인만 있다보니 실제 좌표가 어디인지 구분하기 힘들때가 있습니다. 그럴때 라인위에 좌표에 점을 찍어 보여줄 수 있습니다. 기본은 QCPScatterStyle::ssNone입니다.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ...

    ui->plot->graph(0)->setLineStyle(QCPGraph::lsLine);
    ui->plot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle,10));

    ui->plot->replot();
}

ssCircle, 크기 10
ssCross, 크기 10
ssStar, 크기 20

  • 선 색 바꾸기

QPen을 이용해서 바꿀 수 있습니다. Pen의 색이나 굵기를 바꿀 수 있습니다.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ...

    QPen pen;
    pen.setColor(QColor(255,0,0));
    pen.setWidth(5);
    ui->plot->graph(0)->setPen(pen);

    ui->plot->replot();
}

728x90

'Programming > Qt' 카테고리의 다른 글

Qt 데이터 시각화 도구 QCustomPlot 사용하기(2)  (0) 2021.02.17
Qt random 값 사용  (0) 2021.02.17
Qt 위젯 캡처하는 법  (0) 2021.02.16
[Qt] ProcessEvents  (0) 2020.12.22
Qt 비슷한 파일명을 가진 파일 정보 불러오기  (0) 2020.12.09

+ Recent posts