QT5 QCustomPlot 绘制波形图 带竖线游标

根据我的应用需求,同时参考了官方文档QCustomPlot 2.1.1 Documentation ,分享一下QCustomPlot一些样式设置和事件函数。

效果图:

这里我是把游标移动的状态和缩放状态定义一个flag分开操作。

以下基于QCustomPlot库文件已经添加到工程文件里。

首先将一个QWidget提升到QCustomPlot类。

头文件

//写了QLabel的点击
class CursorLabel : public QLabel
{
    Q_OBJECT
public:
    explicit CursorLabel(QWidget *parent = nullptr) : QLabel(parent) {}
protected:
    void mousePressEvent(QMouseEvent *event) override
    {
        emit cursorLabelPress(event);
        QLabel::mousePressEvent(event);
    }

private:
signals:
    void cursorLabelPress(QMouseEvent *event);

};

//这是后面要用到的变量
bool lLineV = false,rLineV = false;
bool nZoomFlag = true;
CursorLabel *label_A,*label_B;
QCPItemStraightLine *m_lineLV; //左游标
QCPItemStraightLine *m_lineRV; //右游标

首先弄个按钮做状态切换

void MainWindow::on_pushButton_3_clicked()
{
    if(!nZoomFlag)
    {
        nZoomFlag = true;
        ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

        lLineV = false;
        rLineV = false;
        ui->pushButton_3->setText("Zoom");
        disconnect(label_A, &CursorLabel::cursorLabelPress, this, &OtdrWdiget::labelAClicked);
        disconnect(label_B, &CursorLabel::cursorLabelPress, this, &OtdrWdiget::labelBClicked);
        disconnect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(cursor_mousePress(QMouseEvent*)));
        disconnect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(cursor_mouseMove(QMouseEvent*)));
        disconnect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(cursor_mouseRelease(QMouseEvent*)));
        connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(graphMouseMove(QMouseEvent*)));
        connect(ui->customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(graphMouseWheel(QWheelEvent*)));

    }
    else
    {
        nZoomFlag = false;
        ui->customPlot->setInteractions(QCP::iNone);
        ui->pushButton_3->setText("Cursor");
        connect(label_A, &CursorLabel::cursorLabelPress, this, &OtdrWdiget::labelA
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RayC02

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值