我使用Qt图表模块绘制嵌套甜甜圈图表,就像Qt Charts中的示例一样。
我希望每个组件(QPieSlice)响应鼠标事件,hovered()信号运行良好,但clicked()信号仅适用于最后添加的QPieSerie中的QpieSlices。似乎其他QpieSlices不发出信号,因为如果我明确调用clicked()函数,则插槽响应正确。 这段代码显示了问题
Widget::Widget(QWidget *parent): QWidget(parent){
QChartView *chartView = new QChartView;
QChart *chart = chartView->chart();
for (int i = 0; i < donutCount; i++) {
QPieSeries *donut = new QPieSeries;
donut->setHoleSize(minSize + i * (maxSize - minSize) / donutCount);
donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutCount);
int sliceCount = 3 + qrand() % 3;
for (int j = 0; j < sliceCount; j++) {
qreal value = 100 + qrand() % 100;
QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value);
slice->setLabelVisible(true);
slice->setLabelColor(Qt::white);
slice->setLabelPosition(QPieSlice::LabelInsideTangential);
connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool)));
connect(slice, SIGNAL(clicked()), this, SLOT(selected()));
donut->append(slice);
}
m_donuts.append(donut);
chartView->chart()->addSeries(donut);
}
void Widget::selected()
{
QPieSlice *slice = qobject_cast<QPieSlice *>(sender());
cout << slice->label().toStdString() << endl;
}
我做错了什么?有人能帮助我吗?