Copy local QVector to Class QVector

1

This is what I'm doing in the Nail LineChart chart for a class work, I'm having a lot of difficulties with the C ++ language, in a certain part of the code I pass by a QVector<QPoint> parameter, and I want to paint on the DrawLine function these points, but for this I thought that copying the QVector<QPoint> that I send to the method, for a QVector<QPoint> of the class but I can not copy, does anyone give me a help?

#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!";
}
    
asked by anonymous 26.05.2014 / 02:38

1 answer

1

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

Some of these issues are:

  • Excessive use of pointers for cases where they are not needed. ( QVector<QPoint> * vector declared as member variable of LineChart , for example)
  • No definition of copy constructor, assignment operator, and destructor when your class has pointers. Not having this causes memory to be leaked in some moments (C ++ does not garbage collection, you have to call delete when you finish using memory spaces dynamically allocated with new ) and that internal objects of the class are shared between copies of this.

Having said this, the problem in your code that causes you to not be able to copy the vector is in the following snippet:

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

Here vector is a QVector<QPoint> * and on the next line you try to assign a variable of type QVector<QPoint> to the pointer, which is not valid. To make the copy it is necessary to derrefer the pointer as in the following code:

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

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

vector = new QVector<QPoint>(series);

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

    
27.05.2014 / 14:16