2

Unfortunately, I can't find any way to catch a signal of mouse click on any QLineSeries at QChartView. This is required for the subsequent increase in the thickness of the curve on the graph.

RAM
  • 2,257
  • 2
  • 19
  • 41
Johny Sins
  • 23
  • 1
  • 5

1 Answers1

3

You can use the clicked signal of the QLineSeries and since it will affect the same series then you could create a derived class

main.cpp

#include <QApplication>
#include <QChartView>
#include <QLineSeries>
#include <random>

QT_CHARTS_USE_NAMESPACE

class LineSeries: public QLineSeries{
public:
    LineSeries(QObject *parent = nullptr):
        QLineSeries(parent)
    {
        connect(this, &QXYSeries::clicked, this, &LineSeries::onClicked);
    }
private slots:
    void onClicked(){
        QPen p = pen();
        p.setWidth(p.width()+1);
        setPen(p);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QChart *chart = new QChart();
    chart->legend()->hide();

    chart->setTitle("Simple line chart example");

    std::random_device rd;     // only used once to initialise (seed) engine
    std::mt19937 rng(rd());
    std::uniform_int_distribution<int> uni(0, 10);

    for(size_t i=0; i< 5; i++){
        LineSeries *series = new LineSeries();
        for(size_t j=0; j < 10; j++){
            *series << QPointF(j, uni(rng));
        }
        chart->addSeries(series);
    }
    chart->createDefaultAxes();

    QChartView chartView(chart);
    chartView.setRenderHint(QPainter::Antialiasing);
    chartView.resize(640, 480);
    chartView.show();

    return a.exec();
}

enter image description here

Update:

you have to connect the series, you do not understand that it is ui-> vchrt but assume that it is a QChartView, but QChartView does not have the clicked signal so it seems strange to me that this code works, on the other hand:

QLineSeries **series;
series = new QLineSeries*[data.size()];

is a bad practice in C ++, you are using the C style in C ++, you should use container as QList , for example:

QList<QLineSeries *> series;

for(size_t i=0; i< data.size(); i++){
    QLineSeries *serie = new QLineSeries();
    for(size_t j=0; j < 10; j++){
        *serie << QPointF(/*values*/);
    }
    series << serie;
    connect(serie, &QXYSeries::clicked, this, &MainWindow::on_series_Clicked);
    chart->addSeries(serie);
}

after that you must modify your slot, only the QObjects are passed the pointer, QPointF must be passed by value, but even so it is not necessary since you will not use that data, and as the one that emits the signal is the QLineSeries this is passed through sender() in the slot, with a casting we get the series and the changes are made.:

*.h

private slots:
    void on_series_Clicked();

*.cpp

void MainWindow::on_series_Clicked(){
    auto serie = qobject_cast<QLineSeries *>(sender());
    QPen p = serie->pen();
    p.setWidth( p.width() == 1 ? 5: 1);
    serie->setPen(p);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • And now your own class LineSeries is no longer needed? Is on_series_Clicked() slot a member of MainWindow class? – Johny Sins Aug 08 '18 at 12:22
  • @JohnySins my own class is no longer needed, on_series_Clicked if it is a MainWindow slot. – eyllanesc Aug 08 '18 at 12:23
  • Thanks, @eyllanesc. How can I get serie index in container for more complex behavior of on_series_Clicked() slot? – Johny Sins Aug 08 '18 at 12:47
  • @JohnySins use [indexOf()](http://doc.qt.io/qt-5/qlist.html#indexOf) method: `int index = your_containter.indexOf(your_serie)`, – eyllanesc Aug 08 '18 at 12:50
  • @JohnySins if my answer helps you do not forget to mark it as correct, but you know do it review the [tour], that is the best way to thank. – eyllanesc Aug 08 '18 at 12:54