QCustomPlot,从动态矢量绘图

时间:2014-12-12 06:54:16

标签: c++ qt qvector qcustomplot

我正在从txt文件中绘制一些数据。我正在根据水平和垂直滑块来改变它。我想重新绘制上部horizo​​ntalSlider和下部verticalSlider区域。要做到这一点,我试图根据horizo​​ntalSlider调整我的向量。所以horizo​​ntalSlider值会发生变化。如何使用此向量的值从0到horizo​​ntalData?image shows what i mean.

#include "itemline.h"
#include "ui_itemline.h"
#include "qcustomplot.h"

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

        QVector<double> v;
        QVector<double> x(100), y(100);  //
        QFile textFile("C:/Users/Kk/Desktop/Ekg100.txt");
        if(textFile.open(QIODevice::ReadOnly))
        {
            double d;
            QTextStream textStream(&textFile);
            while (!textStream.atEnd()) {
                textStream >> d;
                if(textStream.status() == QTextStream::Ok){
                    v.append(d);
                }
                else
                    break;
            }
            for(int i=0; i<100; ++i)
            {
                x[i] = i;
                y[i] = v[i];
            }

            ui->customPlot->addGraph();
            ui->customPlot->graph(0)->setData(x, y);
            ui->customPlot->graph(0)->setPen(QPen(Qt::blue));
            ui->customPlot->xAxis->setLabel("Time");
            ui->customPlot->yAxis->setLabel("EKG");
            ui->customPlot->graph(0)->rescaleAxes();
            ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom |         QCP::iSelectPlottables);
            ui->customPlot->replot();
        }

    itemB = new QCPItemLine(ui->customPlot);
    ui->customPlot->addItem(itemB);
    itemB->setPen(QPen(Qt::red));
    itemB->start->setCoords(0,0);
    itemB->end->setCoords(0,170);

    itemA = new QCPItemLine(ui->customPlot);
    ui->customPlot->addItem(itemA);
    itemA->setPen(QPen(Qt::red));
    itemA->start->setCoords(0,0);
    itemA->end->setCoords(x.size(),0);

    ui->horizontalSlider->setRange(0,100);
    ui->verticalSlider->setRange(0,1590);

    ui->customPlot->replot();

    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(horzSliderChanged(int)));
    connect(ui->verticalSlider, SIGNAL(valueChanged(int)), this, SLOT(vertSliderChanged(int)));
    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(update(int)));
    connect(ui->verticalSlider, SIGNAL(valueChanged(int)), this, SLOT(update(int)));
}

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

void itemLine::horzSliderChanged(int value)
{
    double sliderB;
    sliderB = value;
    //qDebug() << veriB;
    itemB->start->setCoords(sliderB,0);
    itemB->end->setCoords(sliderB,170);
    ui->customPlot->replot();
}

void itemLine::vertSliderChanged(int value)
{
    double sliderA;
    sliderA = value / 10.0;
    //qDebug() << veriA;
    itemA->start->setCoords(0,sliderA);
    itemA->end->setCoords(100,sliderA);
    ui->customPlot->replot();
}

void itemLine::update(int value)
{
    double verticalData;
    int horizontalData;

    QSlider * senderSlider = qobject_cast<QSlider *>(this->sender());

    if (senderSlider == ui->horizontalSlider) {
            horizontalData = value;
            qDebug() << horizontalData;
    }
    else if (senderSlider == ui->verticalSlider) {
            verticalData = value / 10.0;
            qDebug() << verticalData;
    }

    QVector<double> v1;
    QVector<double> x1(100), y1(100);
    QFile textFile("C:/Users/Kk/Desktop/Ekg100.txt");
    if(textFile.open(QIODevice::ReadOnly))
    {
        double d1;
        QTextStream textStream(&textFile);
        while (!textStream.atEnd()) {
            textStream >> d1;
            if(textStream.status() == QTextStream::Ok){
                v1.append(d1);
            }
            else
                break;
            }
        for(int i=0; i<v1.size(); ++i)
        {
            x1[i] = i;
            y1[i] = v1[i];
        }
     }

    /*
        After that I want to replot upper verticalSlider and lower horizontalSlider region with     different colour.
        To do this I am trying to copy y1 vector's values to y2 vector from 0 to horizontalData.     horizontalData
        changes consistently, so y2 vector's size changes consistently too. And it is an error. I mean how can i
        use [0 to horizontalData] values for my calculations and replotting?
    */
}

0 个答案:

没有答案