Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Call to temporary is a no-op
QtWS25 Last Chance

Call to temporary is a no-op

Scheduled Pinned Locked Moved Solved General and Desktop
qlineseriesqchartqchartview
4 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wasawi2
    wrote on 25 Sept 2023, 14:00 last edited by
    #1

    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,
    w

    linewidget.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;
    };
    
    K 1 Reply Last reply 25 Sept 2023, 14:52
    0
    • W wasawi2
      25 Sept 2023, 14:00

      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,
      w

      linewidget.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;
      };
      
      K Offline
      K Offline
      kkoehne
      Moderators
      wrote on 25 Sept 2023, 14:52 last edited by
      #2

      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. like

      auto points = this->m_series->points();
      points.replace(2,QPoint(15,value));
      this->m_series.replace(points);
      

      Director R&D, The Qt Company

      W 1 Reply Last reply 26 Sept 2023, 06:41
      5
      • K kkoehne
        25 Sept 2023, 14:52

        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. like

        auto points = this->m_series->points();
        points.replace(2,QPoint(15,value));
        this->m_series.replace(points);
        
        W Offline
        W Offline
        wasawi2
        wrote on 26 Sept 2023, 06:41 last edited by
        #3

        @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

        W 1 Reply Last reply 26 Sept 2023, 06:53
        0
        • W wasawi2
          26 Sept 2023, 06:41

          @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

          W Offline
          W Offline
          wasawi2
          wrote on 26 Sept 2023, 06:53 last edited by
          #4

          @wasawi2 wooops sorry my newby fault!!

          I just had to remove the auto in the cpp file.
          Therefore:

          linewidget.cpp

              //![1]
              m_series = new QLineSeries;
              //![1]
          

          linewidget.h

             QLineSeries* m_series = nullptr;
          

          Everything under control now.
          Thanks again for your fast answer @kkoehne.

          1 Reply Last reply
          1
          • W wasawi2 has marked this topic as solved on 26 Sept 2023, 09:34
          • W wasawi2 referenced this topic on 27 Sept 2023, 13:30

          4/4

          26 Sept 2023, 06:53

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved