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 _DesignLayerList
member:
QList<WorkSystem::GraphicShape> *_DesignLayerList;
You are not declaring an actual QList
instance. 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 another QList
instance, 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 QList
rather than a pointer to a QList
:
QList<WorkSystem::GraphicShape> _DesignLayerList;
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 QList
instance it points to. However, I would not recommend this as it adds overhead and additional complexity for no benefit:
shapeObject = (*DesignLayerList)[i]
As an example of the problems common to using raw pointers like this: while you create the QList
instance, you never actually delete it and as such this code leaks memory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.