Qt-QCustomplot畫靜態、動態曲線教程圖解
https://blog.csdn.net/qqwangfan/article/details/93897902
如上面加入QCustomplot源文件,在QtCreator中將QWidget提升爲QCustomPlot窗口後,在QCustomPlot上面畫了圖像,出現想清空圖像清空不了的情況。估計是QCustomPlot的一個bug,解決辦法如下:
因QCustomPlot默認背景色爲白色,如果代碼修改了QCustomPlot的背景色,則必須也同時在QtCreator中將QCustomPlotWidget
樣式表設置相應的背景色。見下圖,下面的代碼將窗口樣式表也修改爲透明後,就可以清除圖像了。
//在QCustomPlot上面,鼠標左鍵畫點,右鍵在點上點擊後清除點
// .h文件
#ifndef PLOTCURSE_H
#define PLOTCURSE_H
#include <QWidget>
#include <qlist.h>
namespace Ui {
class PlotCurse;
}
class PlotCurse : public QWidget
{
Q_OBJECT
public:
explicit PlotCurse(QWidget *parent = 0);
~PlotCurse();
private slots:
void on_customW_mousePress(QMouseEvent *event);
private:
void InitGrap();
void UpdateGrap();
private:
Ui::PlotCurse *ui;
QVector<double> m_xVec;
QVector<double> m_yVec;
};
#endif // PLOTCURSE_H
// .cpp文件
#include <qcursor.h>
#include <qpainter.h>
#include "plotcurse.h"
#include "ui_plotcurse.h"
const int g_pointSize = 8;
PlotCurse::PlotCurse(QWidget *parent) :
QWidget(parent),
ui(new Ui::PlotCurse)
{
ui->setupUi(this);
InitGrap();
}
PlotCurse::~PlotCurse()
{
delete ui;
}
void PlotCurse::InitGrap()
{
QColor TextColor = QColor(255,255,0,90);
QColor white = QColor(255,255,0);
int TextWidth = 2;
ui->customW->xAxis->setLabelColor(white);
ui->customW->yAxis->setLabelColor(white);
ui->customW->xAxis->setTickLabelColor(white);
ui->customW->yAxis->setTickLabelColor(white);
ui->customW->xAxis->setBasePen(QPen(TextColor,TextWidth));
ui->customW->yAxis->setBasePen(QPen(TextColor,TextWidth));
ui->customW->xAxis->setTickPen(QPen(TextColor,TextWidth));
ui->customW->yAxis->setTickPen(QPen(TextColor,TextWidth));
ui->customW->xAxis->setSubTickPen(QPen(TextColor,TextWidth));
ui->customW->yAxis->setSubTickPen(QPen(TextColor,TextWidth));
// 此處修改了默認白色背景色爲透明,必須在QtCreator上也設置對應的customW窗口背景色,
// 後面清除圖像才能成功,否則會一直清除不到!!
ui->customW->setBackground(Qt::transparent);
ui->customW->xAxis->setRange(0,1.0);
ui->customW->yAxis->setRange(0,1.0);
// ui->customW->plotLayout()->insertRow(0);
// ui->customW->plotLayout()->addElement(0,0,new QCPTextElement(ui->customW,"point graph"));
// ui->customW->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom|QCP::iSelectAxes|QCP::iSelectLegend|QCP::iSelectPlottables);;
}
void PlotCurse::UpdateGrap()
{
ui->customW->clearGraphs();// 背景色會導致QCustomWidget窗口不會清除圖層。如發現沒有清除,可能是背景色問題
ui->customW->addGraph();
ui->customW->graph(0)->setPen(QPen(Qt::green));
//ui->customW->graph(0)->setBrush(Qt::green);
ui->customW->graph(0)->setLineStyle(QCPGraph::lsNone);
ui->customW->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle,g_pointSize));//set points size
ui->customW->graph(0)->addData(m_xVec,m_yVec);
qDebug("xvec:%d, yvec:%d",m_xVec.size(),m_yVec.size());
/* ui->customW->addGraph();
ui->customW->graph(1)->setPen(QPen(Qt::white));
ui->customW->graph(1)->addData(m_xVec,m_yVec,false);*/
ui->customW->repaint();
ui->customW->replot();
}
void PlotCurse::on_customW_mousePress(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
double px = event->x();
double py = event->y();
double cx = ui->customW->xAxis->pixelToCoord(px); // 將像素值轉爲座標值
double cy = ui->customW->yAxis->pixelToCoord(py);
m_xVec.push_back(cx);
m_yVec.push_back(cy);
// m_pointLs.push_back(QPointF(cx,cy));
qDebug("px:%.2f,py:%.2f, cx:%.2f,cy:%.2f",px,py,cx,cy);
}
if(event->button() == Qt::RightButton)
{
double x = event->x(); // 此處取得的是像素值 //ui->customW->xAxis->pixelToCoord(event->x());
double y = event->y();//ui->customW->yAxis->pixelToCoord(event->y());
qDebug("x:%.2f,y:%.2f",x,y);
for(int i=m_xVec.size()-1; i>=0; i--) // 從後往前刪
{
double cx = m_xVec[i];
double cy = m_yVec[i];
double px = ui->customW->xAxis->coordToPixel(cx); // 將座標轉回像素,方便與像素比較
double py = ui->customW->yAxis->coordToPixel(cy);
if(x< px + g_pointSize && // 判斷鼠標右鍵是否在 以點爲圓心,g_pointSize爲半徑的圓內
x > px - g_pointSize &&
y < py + g_pointSize &&
y > py - g_pointSize)
{
m_xVec.remove(i);
m_yVec.remove(i);
}
}
}
UpdateGrap();
}