Why is the clicked() signal for the QPieSlice not

2019-07-25 03:53发布

I'm using Qt charts module to draw a Nested Donuts chart just like the example in Qt Charts. enter image description here

And I want every components (QPieSlice) response to the mouse event, the hovered() signals work well, however, the clicked() signals only work for the QpieSlices in the last added QPieSerie. It seems other QpieSlices do not emit the signals, since if I explicitly call the clicked() function, the slot response correctly. This piece of code shows the issue

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;
}

What am I doing wrong? Can someone help me?

0条回答
登录 后发表回答