QObject::sender()和QCustomPlot::setInteractions

1、QCustomPlot::setInteractions

       void QCustomPlot::setInteraction(const Interactions & interactions)

       Axis range manipulation is controlled via iRangeDrag and iRangeZoom

       可以用iRangeDrag 和 iRangeZoom对图的坐标轴进行自动调节

       iRangeDrag:Axis ranges are draggable(可拖放的)

   iRangeZoom :Axis ranges are zoomable with the mouse wheel(可缩放的)

   eg:

   ui->channel0->setInteraction(QCP::iRangeDrag);
   ui->channel0->setInteraction(QCP::iRangeZoom);

  这样就实现了对图像坐标轴的鼠标滑动缩放


2、QObject::sender()

      sender might be useful when many signals are connected to a single slot

      当多个信号需要关联到同一个槽上时,如何区分是哪一个信号触发的槽,此时sender()就派上了用场

      Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0.

      QObject::Sender()返回发送信号的对象的指针,这样就能知道是哪一个对象发送的信号,从而可以做出不同的操作

     

    connect(ui->channel0, SIGNAL(titleClick(QMouseEvent*,QCPPlotTitle*)), this, SLOT(onTitleClick()));
    connect(ui->channel1, SIGNAL(titleClick(QMouseEvent*,QCPPlotTitle*)), this, SLOT(onTitleClick()));
    connect(ui->channel2, SIGNAL(titleClick(QMouseEvent*,QCPPlotTitle*)), this, SLOT(onTitleClick()));
    connect(ui->channel3, SIGNAL(titleClick(QMouseEvent*,QCPPlotTitle*)), this, SLOT(onTitleClick()));
      可以看到通过点击不同的channel发出信号从而触发槽

      要判断是哪个channel发出的信号,那么久可以使用sender()

      

void MainWindow::onTitleClick()
{
    QCustomPlot *channel = qobject_cast<QCustomPlot*>(sender());
    if (channel == ui->channel0)
        m_rescale[0] = !m_rescale[0];
    if (channel == ui->channel1)
        m_rescale[1] = !m_rescale[1];
    if (channel == ui->channel2)
        m_rescale[2] = !m_rescale[2];
    if (channel == ui->channel0)
        m_rescale[3] = !m_rescale[3];
}
      如上所示,返回的就是触发槽函数的channe,返回类型是QCustomPlot,可以根据触发槽函数的channel进行不同的操作。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值