在Qt 5.4中使用Namespace
Using Namespace in Qt 5.4
我在Qt 5.4中的GraphicsList.cpp中遇到了一个问题
#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();
}
}
我GraphicList.hQQQ/GraphicsList.cpp:9:错误:没有c风格强制转换的匹配转换从"QList"到"WorkSystem::GraphicShape"WorkSystem: GraphicShape shapeObject = (WorkSystem:: GraphicShape) _DesignLayerList[我];^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#ifndef GRAPHICSLIST_H
#define GRAPHICSLIST_H
#include "GraphicShape.h"
class GraphicsList
{
public:
GraphicsList();
~GraphicsList();
void Draw();
private:
QList<WorkSystem::GraphicShape> *_DesignLayerList;
};
#endif // GRAPHICSLIST_H
我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
我GraphicShape.cpp #include "GraphicShape.h"
#include <QDebug>
WorkSystem::GraphicShape::GraphicShape()
{
_penColor=Qt::white;
}
void WorkSystem::GraphicShape::Draw(){
qDebug()<<"DrawDrawDrawDraw";
}
WorkSystem::GraphicShape::~GraphicShape()
{
}
请给我一些建议。
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.cppdiv…
问题似乎是指针的误用。如果您查看_DesignLayerList
成员的声明:
QList<WorkSystem::GraphicShape> *_DesignLayerList;
您没有声明实际的QList
实例。相反,您声明了一个指向QList的指针。因此,当您使用_DesignLayerList[i]
时,您实际上并没有试图查看列表,而是使用指针算术来查找另一个QList
实例,这不是您所期望的。
相反,你应该声明没有星号的成员变量,这意味着将是QList
的实际实例,而不是指向QList
的指针:
QList<WorkSystem::GraphicShape> _DesignLayerList;
这将按预期运行。我还建议您回顾一下对指针和值之间区别的理解,因为这是c++的基础。在现代c++中,建议尽可能避免使用原始指针,而使用智能指针、引用和值类型,因为它们通常更合适、更安全。
如果你坚持使用指针,另一种方法是通过先取消对指针的引用来执行查找,这样你就可以指向它所指向的QList
实例。但是,我不建议这样做,因为它增加了开销和额外的复杂性,没有任何好处:
shapeObject = (*DesignLayerList)[i]
作为使用原始指针的常见问题的一个例子:当您创建QList
实例时,您从未实际删除它,因此此代码泄漏内存。
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用Qt C++计算类似Git的SHA1哈希
- Qt focusObjectChanged example?
- 如何在cpp文件之间切换窗口?在Qt中
- "using namespace std;"在C++的作用是什么?
- Qt和C++:将QLineEdit添加到QTabWidget中
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 从数据库实时显示QT c++中的数据
- Qt Quick-如何仅从c++代码与qml属性交互
- 在Qt 5.4中使用Namespace
- 从Qt 5.6切换到Qt 5.7 - "no member 'make_unique' in namespace std"