I got a issue in GraphicsList.cpp in Qt 5.4
#include "GraphicsList.h"
GraphicsList::GraphicsList()
{
_DesignLayerList=new QList<WorkSystem::GraphicShape>();
}
void GraphicsList::Draw(){
for(int i=this->_DesignLayerList->count();i>=0;--i){
WorkSystem::GraphicShape shapeObject=(WorkSystem::GraphicShape)_DesignLayerList[i];
// WorkSystem::GraphicShape shapeObject=_DesignLayerList[i];
shapeObject.Draw();
}
}
QQQ/GraphicsList.cpp:9: error: no matching conversion for C-style cast from 'QList' to 'WorkSystem::GraphicShape' WorkSystem::GraphicShape shapeObject=(WorkSystem::GraphicShape)_DesignLayerList[i]; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My GraphicList.h
#ifndef GRAPHICSLIST_H
#define GRAPHICSLIST_H
#include "GraphicShape.h"
class GraphicsList
{
public:
GraphicsList();
~GraphicsList();
void Draw();
private:
QList<WorkSystem::GraphicShape> *_DesignLayerList;
};
#endif // GRAPHICSLIST_H
my GraphicShap.h
#ifndef GRAPHICSHAPE_H
#define GRAPHICSHAPE_H
#include <QDebug>
#include <QPainter>
namespace WorkSystem {
class GraphicShape
{
public:
GraphicShape();
~GraphicShape();
QColor _penColor;
virtual void Draw();
};
}
#endif // GRAPHICSHAPE_H
My GraphicShape.cpp
#include "GraphicShape.h"
#include <QDebug>
WorkSystem::GraphicShape::GraphicShape()
{
_penColor=Qt::white;
}
void WorkSystem::GraphicShape::Draw(){
qDebug()<<"DrawDrawDrawDraw";
}
WorkSystem::GraphicShape::~GraphicShape()
{
}
Please give me any suggestion.
shapeLine.h
#ifndef SHAPELINE_H
#define SHAPELINE_H
#include <QDebug>
#include "GraphicShape.h"
namespace WorkSystem {
class shapeLine : public GraphicShape
{
public:
shapeLine();
~shapeLine();
protected:
void Draw();
};
}
#endif // SHAPELINE_H
shapeLine.cpp ...
The problem appears to be a bit of a misuse of pointers. If you look at the declaration for your
_DesignLayerListmember:You are not declaring an actual
QListinstance. Instead, you are declaring a pointer to a QList. Thus when you use_DesignLayerList[i], you aren't actually trying to look into the list, but instead doing pointer arithmetic to look up anotherQListinstance, which is not what you are expecting.Instead, you should declare your member variable without the star, meaning will be an actual instance of a
QListrather than a pointer to aQList:This will then function as expected. I would also recommend reviewing your understanding of the difference between pointers and values, as this is a fundamental of C++. In modern C++, it is recommended to avoid the use of raw pointers as much as possible and instead use smart pointers, references, and value types as they are generally more appropriat and safer.
An alternative, if you insist on using pointers, is to perform a lookup by first de-referencing the pointer so you are referring to the
QListinstance it points to. However, I would not recommend this as it adds overhead and additional complexity for no benefit:As an example of the problems common to using raw pointers like this: while you create the
QListinstance, you never actually delete it and as such this code leaks memory.