Call to temporary is a no-op
-
Attention I'm just a newby!
Hello everyone,
So I am modifying one of the provided examples, in particular the lineWidget example (charts/chartsgallery). My intention was to modify the QLineSeries by using a QSlider, so that when an event is received the chart modifies some of its values. I seem to have the signal and slot working well, but when I try to change the QLineSeries object i get a crash. More in detail the IDE shows me a warning that something in not quite right. It reads:
C:\TestPlot\linewidget.cpp:44:5: Call to temporary is a no-op: QList<QPointF>::replace [clazy-writing-to-temporary]
So I'm afraid I'm missing some basic C++ concept here...
I would really appreciate if an expert eye could have a look and let me know what is what I'm missing. I guess I might be trying to access a dangling pointer but I'm not completely sure.
Thank you so much for any help,
wlinewidget.cpp
// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "linewidget.h" #include <QLineSeries> #include <QChart> #include <QChartView> LineWidget::LineWidget(QWidget *parent) { //![1] auto m_series = new QLineSeries; //![1] //![2] m_series->append(0, 6); m_series->append(1, 4); m_series->append(2, 8); m_series->append(3, 4); m_series->append(4, 5); //![2] m_series->setName("plot"); //![3] auto m_chart = new QChart; m_chart->legend()->setAlignment(Qt::AlignBottom); m_chart->addSeries(m_series); m_chart->createDefaultAxes(); m_chart->setTitle("Simple Line Chart"); //![3] m_defaultChartView = new QChartView(m_chart, this); } void LineWidget::resizeEvent(QResizeEvent *) { m_defaultChartView->resize(size()); } void LineWidget::setValue(int value) { qDebug() << value; this->m_series->points().replace(2,QPoint(15,value)); }
linewidget.h
// Copyright (C) 2023 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #ifndef LINEWIDGET_H #define LINEWIDGET_H #include <QWidget> QT_FORWARD_DECLARE_CLASS(QLineSeries) QT_FORWARD_DECLARE_CLASS(QChart) QT_FORWARD_DECLARE_CLASS(QChartView) class LineWidget : public QWidget { Q_OBJECT public: explicit LineWidget(QWidget *parent = nullptr); protected: void resizeEvent(QResizeEvent *) override; public slots: void setValue(int value); private: QLineSeries* m_series = nullptr; //new QLineSeries; QChart* m_chart = nullptr; //new QChart; QChartView *m_defaultChartView = nullptr; }; #endif
TestPlot.cpp
#include "TestPlot.h" #include "linewidget.h" #include <QApplication> #include <QHBoxLayout> #include <QSlider> TestPlot::TestPlot(QWidget *parent) : QWidget(parent) { m_Widget = new LineWidget(this); setMinimumSize(800, 400); resize(1200, 600); m_slider = new QSlider(this); m_slider->setFixedSize(20, 200); auto layout = new QHBoxLayout(this); layout->addWidget(m_Widget); layout->addWidget(m_slider); setLayout(layout); connect(m_slider, &QSlider::valueChanged, m_Widget, &LineWidget::setValue); } TestPlot::~TestPlot() {}
TestPlot.h
#pragma once #include "linewidget.h" #include <QWidget> QT_FORWARD_DECLARE_CLASS(QSlider) //class LineWidget; class TestPlot : public QWidget { Q_OBJECT public: explicit TestPlot(QWidget *parent = nullptr); ~TestPlot(); private: LineWidget* m_Widget = nullptr; QSlider* m_slider = nullptr; };
-
I probably can explain the clazy warning:
@wasawi2 said in Call to temporary is a no-op:
this->m_series->points().replace(2,QPoint(15,value));
this->m_series is a QLineSeries pointer. QLineSeries::points() returns a QList:
https://doc.qt.io/qt-6/qxyseries.html#points
QList is a value type ... That is, whatever you do to
this->m_series->points()
, you're doing it o a temporary copy. If you want to actually change the points in the series, you need to do s.th. likeauto points = this->m_series->points(); points.replace(2,QPoint(15,value)); this->m_series.replace(points);
-
@kkoehne Ecellent!
Yes that was exactly the problem. I didn't know about value types... I will look into it. Thank you!
BTW I also had to change one more thing. The instantiation of QLineSeries has to be done in the header file:
linewidget.h
QLineSeries* m_series = new QLineSeries;
Instead of being done in the cpp file. I thought it was awkward to do it in the header.... is it a common practice? Why doesn't it work in the cpp file even if the variable is declared in the .h? does the QLineSeries instance get deleted when it reaches the end of the scope?
Thank you again for ani hints!!
Best,
w -
-
-