QCustomPlot清除图形清除不了,需要在qtcreator中设置qcustomWidget窗口背景色才能清除

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();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章