最近要将之前做的QtCharts的柱状图改成用QCustomPlot
要实现之前的功能,结果发现QCustomPlot里面只有这些信号:
而在我们使用的柱子类QCPBars里面却只有三个信号:
直接 做connect也只有这些
粗暴一点直接 connect(bars ,SIGNAL(plottableClick()),this, SLOT());
会提示:没有这个信号...
所以回到这个信号selectionChanged :
This signal is emitted when the selection state of this plottable has changed, either by user interaction or by a direct call to setSelection. The parameter selection holds the currently selected data ranges.
只能说先勉强用着吧。
使用方法:
先设置customplot的plottable (绘图层)可选:
customplot->setInteractions( QCP::iSelectPlottables );
只有就可以双击选中柱子,触发这个信号。
所以对bars做一个connect连接
connect(bars,SIGNAL(selectionChanged(QCPDataSelection)),this,SLOT(barsClickedSlot()));
//槽函数
...barsClickedSlot()
{
qDebug()<<"asdasdasdasdadsasdasd";
}
结果如下:
暂时先这样。效果是全选而不是点击某一个柱子会有这个效果
/*........................................................*/
看了一下官方例子发现是自己走进误区了!
上面的做法是改变选择模式就会触发信号,也就是说一次点击实际上会触发两次
我并不需要让QCPBars发送信号,我直接用QCustomPlot发送信号就好了。。。
connect(customplot1,SIGNAL(plottableClick(QCPAbstractPlottable*,int,QMouseEvent*)),this,SLOT(bars2ClickedSlot(QCPAbstractPlottable*,int)));
直接让QCustomPlot 发送plottableClick信号,这个信号可以在plottable被点击时发送!