Closed StoneBang closed 5 months ago
能否给出相关代码?
h文件 `#ifndef CUSTOMPLOTITEM_H
class CustomPlotItem : public QQuickPaintedItem { Q_OBJECT public: CustomPlotItem(QQuickItem parent = nullptr); virtual ~CustomPlotItem(); void paint( QPainter painter ); Q_INVOKABLE void initCustomPlot(); protected: void routeMouseEvents( QMouseEvent event ); virtual void mousePressEvent( QMouseEvent event ); virtual void mouseReleaseEvent( QMouseEvent event ); virtual void mouseMoveEvent( QMouseEvent event ); virtual void mouseDoubleClickEvent( QMouseEvent* event );
private: QCustomPlot *m_CustomPlot = nullptr;
private slots: void onCustomReplot(); void updateCustomPlotSize(); };
`
cpp文件 `#include "customplotitem.h"
CustomPlotItem::CustomPlotItem( QQuickItem* parent ) : QQuickPaintedItem( parent ) , m_CustomPlot( new QCustomPlot() ) {
setFlag( QQuickItem::ItemHasContents, true );
setAcceptedMouseButtons( Qt::AllButtons );
// connect( this, &QQuickPaintedItem::widthChanged, this, &CustomPlotItem::updateCustomPlotSize );
// connect( this, &QQuickPaintedItem::heightChanged, this, &CustomPlotItem::updateCustomPlotSize );
}
CustomPlotItem::~CustomPlotItem() { delete m_CustomPlot; m_CustomPlot = nullptr; }
void CustomPlotItem::initCustomPlot() {
updateCustomPlotSize();
QCustomPlot* customPlot = m_CustomPlot;
// add two new graphs and set their look:
customPlot->addGraph();
customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
customPlot->addGraph();
customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
// generate some points of data (y0 for first, y1 for second graph):
QVector<double> x(251), y0(251), y1(251);
for (int i=0; i<251; ++i)
{
x[i] = i;
y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosine
y1[i] = qExp(-i/150.0); // exponential envelope
}
// configure right and top axis to show ticks but no labels:
// (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
customPlot->xAxis2->setVisible(true);
customPlot->xAxis2->setTickLabels(false);
customPlot->yAxis2->setVisible(true);
customPlot->yAxis2->setTickLabels(false);
// make left and bottom axes always transfer their ranges to right and top axes:
connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
// pass data points to graphs:
customPlot->graph(0)->setData(x, y0);
customPlot->graph(1)->setData(x, y1);
// let the ranges scale themselves so graph 0 fits perfectly in the visible area:
customPlot->graph(0)->rescaleAxes();
// same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
customPlot->graph(1)->rescaleAxes(true);
// Note: we could have also just called customPlot->rescaleAxes(); instead
// Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
connect( m_CustomPlot, &QCustomPlot::afterReplot, this, &CustomPlotItem::onCustomReplot );
}
void CustomPlotItem::paint( QPainter* painter ) { if (m_CustomPlot) { // QPixmap picture( boundingRect().size().toSize() ); // QCPPainter qcpPainter( &picture );
// m_CustomPlot->replot();
// m_CustomPlot->toPainter( &qcpPainter );
// painter->drawPixmap( QPoint(), picture );
QCPPainter qcpPainter(painter);
m_CustomPlot->toPainter(&qcpPainter);
}
}
void CustomPlotItem::mousePressEvent( QMouseEvent* event ) { routeMouseEvents( event ); }
void CustomPlotItem::mouseReleaseEvent( QMouseEvent* event ) { routeMouseEvents( event ); }
void CustomPlotItem::mouseMoveEvent( QMouseEvent* event ) { routeMouseEvents( event ); }
void CustomPlotItem::mouseDoubleClickEvent( QMouseEvent* event ) { // routeMouseEvents( event ); }
// void CustomPlotItem::graphClicked( QCPAbstractPlottable* plottable ) // { // qDebug() << Q_FUNC_INFO << QString( "Clicked on graph '%1 " ).arg( plottable->name() ); // }
void CustomPlotItem::routeMouseEvents( QMouseEvent event ) { if (m_CustomPlot) { QMouseEvent newEvent = new QMouseEvent( event->type(), event->localPos(), event->button(), event->buttons(), event->modifiers() ); QCoreApplication::sendEvent( m_CustomPlot, newEvent ); // QCoreApplication::postEvent( m_CustomPlot, newEvent );
}
}
void CustomPlotItem::updateCustomPlotSize() { if (m_CustomPlot) { m_CustomPlot->setGeometry( 0, 0, width(), height() ); } }
void CustomPlotItem::onCustomReplot() { update(); } `
你这个用的不是本仓库的代码哦
哥,我错了,没抄对
https://github.com/MrHulu/QmlQCustomPlot/assets/15672420/fe22401d-6e4c-4586-9e23-516baf1e6490