Why is the clicked() signal for the QPieSlice not emitted?



  • I'm using Qt charts module to draw a Nested Donuts chart just like the example in Qt Charts.
    alt text
    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;
    }
    

    The press(), doubleClicked() of QPieSlice and QPieSeries::clicked(QPieSlice*) signals also have the same problem. What am I doing wrong? Can someone help me?



  • I get the same behaviour.
    You should probably report it to https://bugreports.qt.io saying that to test it they just have to add connect(slice,&QPieSlice::clicked,[]()->void{qDebug("clicked");}); before connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool))); in the nested donuts example



  • @VRonin Thank you for your rely. I have reported this bug. And I wonder if there's any other way to deal with the mouse event. I try to overwrite the mousePressEvent() function, but it doesn't work. It seems that the QChartView has already accept the event. Do you know any other solution?



  • I'm pretty sure you can still intsall an event filter on it http://doc.qt.io/qt-5/eventsandfilters.html#event-filters


  • Lifetime Qt Champion

    Hi,

    Can you also share the link to the bug report ?



  • @SGaist Sure. QTBUG-56037 and it has already been fixed.



  • @Cheery

    Everything is my guess...

    The cause of this bug is thought to be the QGraphicsItem::ItemIsSelectable flag when creating the items(QPieSeries, QPieSlice).

    // line 66, in piechartitem.cpp
    setFlag(QGraphicsItem::ItemIsSelectable);
    
    // line 59, in piesliceitem.cpp
    setFlag(QGraphicsItem::ItemIsSelectable);
    

    The "emit pressed()" is omitted because it performs the "setSelected()" when clicking the item with the mouse(left button). Exclude last appended items (Top-most items)

    // line 7169, in QGraphicsItem.cpp
    void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable)) {
    

    If you want to perform that function with the current version(5.7), you can use some tricks. If RightButton is not needed...

    void MyChartView::mousePressEvent(QMouseEvent *event)
    {
        if (event->button() == Qt::LeftButton) {
            QMouseEvent ev(event->type(), event->localPos(), Qt::RightButton, Qt::RightButton, event->modifiers());
    
            QChartView::mousePressEvent(&ev);
        }
        else
            QChartView::mousePressEvent(event);
    }
    


  • @Devopia53 said in Why is the clicked() signal for the QPieSlice not emitted?:

    void MyChartView::mousePressEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton) {
    QMouseEvent ev(event->type(), event->localPos(), Qt::RightButton, Qt::RightButton, event->modifiers());

        QChartView::mousePressEvent(&ev);
    }
    else
        QChartView::mousePressEvent(event);
    

    }

    Thank you for your rely. This method do work well.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.