QLegendMarker signal/slot connect not working
-
Hello, I am trying to make a chart where the user can hide and show the available line series by clicking the respective legend marker, like this:
QList<QLineSeries*> lineSeries; for(int i = 0; i < nbCharts; i++) { QLineSeries *series = new QLineSeries; lineSeries.append(series); chart->addSeries(series); series->attachAxis(axisX); series->attachAxis(axisY); QLegendMarker *marker = chart->legend()->markers(series)[0]; marker->setLabel(QString::number(i)); connect(marker, SIGNAL(clicked()), this, SLOT(markerClicked())); connect(marker, SIGNAL(hovered(bool)), this, SLOT(markerHovered(bool))); }
However, this doesn't seem to work and I don't understand why. I have the two slot functions in the
public slots
section in header file, I have no error notifications regarding theconnect
when debugging and when I putqDebug <<
before theconnect
lines, the debugger gives metrue
.I tried the code in the example in https://doc.qt.io/archives/qt-5.7/qtcharts-legendmarkers-example.html like this:
for(int i = 0; i < nbCharts; i++) { QLineSeries *series = new QLineSeries; lineSeries.append(series); chart->addSeries(series); series->attachAxis(axisX); series->attachAxis(axisY); QLegendMarker *marker = chart->legend()->markers(series)[0]; marker->setLabel(QString::number(i)); } const auto markers = chart->legend()->markers(); for (QLegendMarker *marker : markers) { connect(marker, SIGNAL(clicked()), this, SLOT(markerClicked())); connect(marker, SIGNAL(hovered(bool)), this, SLOT(markerHovered(bool))); }
but it hasn't worked either. Any ideas why this doesn't work?
Edit: the chart works fine. I am able to add points to the series and print them in the chart and do other things. It's just the
connect
that doesn't seem to work. -
I made a
QPushButton
that would simulate clicking theQLegendMarker
with aconnect
:QPushButton *butt = new QPushButton("activate marker0"); QLegendMarker *marker = chart->legend()->markers()[0]; connect(butt, &QPushButton::clicked, marker, &QLegendMarker::clicked); butt->show();
This actually worked and I can see the first line series disappear and reappear with the click of the button.
I realized that I did override themousePressEvent
,mouseMoveEvent
andmouseReleaseEvent
functions of the class (which inherits fromQChartView
), so could this mean that I can't override these functions AND use theclicked
andhovered
signals fromQLegendMarker
? -
@diego-qt said in QLegendMarker signal/slot connect not working:
so could this mean that I can't override these functions AND use the clicked and hovered signals from QLegendMarker?
You can but when you override a function you should call the base class when you want it's functionality.