// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->plot->setInteraction(QCP::iRangeDrag, true); //鼠标单击拖动
ui->plot->setInteraction(QCP::iRangeZoom, true); //滚轮滑动缩放
ui->plot->addGraph();
ui->plot->graph(0)->setScatterStyle(QCPScatterStyle::ssCircle);
ui->plot->graph(0)->setLineStyle(QCPGraph::lsNone);
//connect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), SLOT(clickedGraph(QMouseEvent*))); //单击
connect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), SLOT(clickedGraph(QMouseEvent*))); //双击
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addPoint(double x, double y)
{
qv_x.append(x);
qv_y.append(y);
}
void MainWindow::clearData()
{
qv_x.clear();
qv_y.clear();
}
void MainWindow::plot()
{
ui->plot->graph(0)->setData(qv_x,qv_y);
ui->plot->replot();
ui->plot->update();
}
void MainWindow::clickedGraph(QMouseEvent *event)
{
QPoint point = event->pos();
qDebug()<<ui->plot->xAxis->pixelToCoord(point.x())<<ui->plot->yAxis->pixelToCoord(point.y());
addPoint(ui->plot->xAxis->pixelToCoord(point.x()), ui->plot->yAxis->pixelToCoord(point.y()));
plot();
}