qcustomplot画鼠标十字星

void MainWindow::mouseMoved(QMouseEvent*event)
{
    QVector<double> vx,vy;
    double x = ui->customPlot->xAxis->pixelToCoord(event->pos().x());
    double y = ui->customPlot->yAxis->pixelToCoord(event->pos().y());
    vx<<0<<x<<ui->customPlot->xAxis->range().maxRange;
    vy<<y<<y<<y;
 
    ui->customPlot->graph(1)->setData(vx,vy);
    ui->customPlot->graph(1)->setPen(QPen(Qt::red));
 
    vx.clear();
    vy.clear();
    vx<<x<<x<<x;
    vy<<0<<y<<ui->customPlot->yAxis->range().maxRange;
    ui->customPlot->graph(2)->setData(vx,vy);
    ui->customPlot->graph(2)->setPen(QPen(Qt::red));
    ui->customPlot->replot();
}
 

要通过鼠标轨迹矩形框,可以结合使用QCustomPlot鼠标事件和QCPItemRect类。可以按照以下步骤进行操作: 1. 创建一个QCPItemRect对象: ``` QCPItemRect *rect = new QCPItemRect(customPlot); rect->setPen(QPen(Qt::red)); rect->setBrush(QBrush(Qt::blue)); ``` 2. 实现鼠标事件: ``` void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { rect->topLeft->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); rect->bottomRight->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); rect->setVisible(true); ui->customPlot->replot(); } } void MainWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() == Qt::LeftButton) { rect->bottomRight->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); ui->customPlot->replot(); } } void MainWindow::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { rect->setVisible(false); ui->customPlot->replot(); } } ``` 其中,鼠标按下时记录矩形框左上角的坐标,鼠标移动时更新矩形框右下角的坐标并重新绘制,鼠标释放时隐藏矩形框并重新绘制。 完整的代码示例: ``` void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { rect->topLeft->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); rect->bottomRight->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); rect->setVisible(true); ui->customPlot->replot(); } } void MainWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() == Qt::LeftButton) { rect->bottomRight->setCoords(ui->customPlot->xAxis->pixelToCoord(event->pos().x()), ui->customPlot->yAxis->pixelToCoord(event->pos().y())); ui->customPlot->replot(); } } void MainWindow::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { rect->setVisible(false); ui->customPlot->replot(); } } void MainWindow::setupPlot() { // ... rect = new QCPItemRect(ui->customPlot); rect->setPen(QPen(Qt::red)); rect->setBrush(QBrush(Qt::blue)); rect->setVisible(false); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值