Copy Qvector Local to Qvector Class

Asked

Viewed 160 times

1

Here’s what I’m doing in the nail a Linechart chart chart for a class assignment, I’m having huge difficulties with the C++ language, in some part of the code step by parameter a QVector<QPoint>, and I want to paint in function DrawLine these points, but for that I thought that copy the QVector<QPoint> I send to the method, to a QVector<QPoint> global class but can’t copy, someone give me a hand ?

#pragma once
#include <QFrame>
#include <QWidget>
#include <QHBoxLayout>
#include <QPaintEvent>
#include <QPainter>
#include <QMainWindow>
#include <QLabel>
#include <QVector>

class LineChart : public QFrame
{
    Q_OBJECT
    public:
        LineChart();
        void addSeries(QVector<QPoint> series, QColor color);
        void setAxisX(QVector<QString> values);
        void setAxisY(QVector<QString> values);
        void setLabelX(QString name);
        void setLabelY(QString name);
        void setTitle(QString title);
        void paintEvent(QPaintEvent* paint);

        // keyPressEvent(QKeyEvent *event);
    private:
        //QFrame *frame;
        QGridLayout *layout;
        QLabel *label;
        int x0,x1,y0,y1;
        QPoint *point;
        QPoint *point2;
        QVector<QPoint> *vector;
        //QPushButton *button;
};

void LineChart::addSeries(QVector<QPoint> series, QColor color){
    vector = new QVector<QPoint>;
    vector = series;
}

void LineChart::paintEvent(QPaintEvent *event){
    QFrame::paintEvent(event);
    QPainter painter(this);

    qDebug() << point->rx() ;
    qDebug() << point->ry();
    qDebug() << point2->rx() ;
    qDebug() << point2->ry();

    //painter.drawLine(point->rx(),point2->ry(),point->rx(),point2->ry());
    for(int i = 0 ; i< vector->length() ; i++){

    }

    qDebug() << "paintEventinvocado!";
}
  • You can merely assign one Qvector to the other, by operator=. Tried that?

1 answer

1

First, this example code shows some problems in basic concepts of the C++ language (You seem to have a knowledge of the Java language and are using C++ as if it were it). It would be interesting to study these concepts by making more basic programs before creating a complex application with graphical interface.

Some of these problems are:

  • Excessive use of pointers for cases where no need exists. (QVector<QPoint> * vector declared as a member variable of LineChart, for example)
  • No definition of copy constructor, assignment operator, and destructor when its class has pointers. Not having this causes memory to be leaked at times ( C++ does not do garbage collection, you have to call delete when you are done using dynamically allocated memory spaces new) and that internal class objects are shared between copies of this.

Having said that, the problem in your code that makes you unable to copy the vector is in the following section:

vector = new QVector<QPoint>;
vector = series;

Here vector is a QVector<QPoint> * and then in the next row you try to assign a variable of type QVector<QPoint> to the pointer, which is not valid. To make the copy you need to override the pointer as in the following code:

*vector = series; // *vector é um QVector<QPoint>

An even more idiomatic solution using C++ features is to use the class copy constructor QVector, so the entire code of the function would become:

vector = new QVector<QPoint>(series);

Creating a new object that is already a copy of the original.

  • 2

    Note: QVector implements copy-on-write. So creating copies of this object is light and does not carry copies of all elements.

  • @Guilhermebernal I edited my reply to remove the reservation about the cost of the copy. Thank you for the information! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.