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进行不同的操作。