Tooltip for each Qplieslice in a QPieSeries
-
Hi all,
My widget includes Qpieslices in a QpieSeries. I need to show the slices' values while hovering on them. I need a tooltip to show the value of the current slice that the mouse is hovering on it. Also, as long as the cursor is on a slice, the tooltip should constantly show its value.
I used different methods including the following code.
Widget::Widget(QWidget *parent) : QWidget(parent) { setMouseTracking(true); // some code QPieSlice *slice = new QPieSlice("Space", value); //some code connect(slice, &QPieSlice::hovered, this, &Widget::explodeSlice); //some code
In
explodeSlice
I do some works that is not related to the tooltip.bool Widget::event (QEvent *ev) { label_ev = new QLabel; label_ev->setWindowFlag(Qt::ToolTip); QHelpEvent *helpEvent = static_cast<QHelpEvent *>(ev); label_ev->move(helpEvent->globalPos()); label_ev->setText(txt); label_ev->show(); return QWidget::event(ev); }
In this code, I have got two problems. First, a delay in the tooltip to pop up (I enabled mouse-tracking mode in my widget). Second, Where can I hide it? the tooltip remains on the slice and wherever I want to hide it in my code it does not work. No way to hide it.
Could anyone help me with this? -
In terms of when to hide, Qt 6.6 has a different hovered() signature which includes a bool, which is set to true on mouse over and false on mouse exit. so:
void xyzObject::slotSliceHover(bool hovered){ QPieSlice* hoverSlice = qobject_cast<QPieSlice*>(QObject::sender()); if (hoverSlice != nullptr){ if (hovered) hoverSlice->setExploded(true); else hoverSlice->setExploded(false); } }