Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当我初始化了指针属性allready时,如何将QGraphicsItem向下转换到已创建的类?_C++_Qt_Casting - Fatal编程技术网

C++ 当我初始化了指针属性allready时,如何将QGraphicsItem向下转换到已创建的类?

C++ 当我初始化了指针属性allready时,如何将QGraphicsItem向下转换到已创建的类?,c++,qt,casting,C++,Qt,Casting,我有自己的QGraphicsPixmapItem,它由一个QGraphicsPixmapItem和一些附加属性组成(如std::string name) 构造函数是这样的: MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) : QGraphicsPixmapItem(pixmap, parent), name(name){

我有自己的QGraphicsPixmapItem,它由一个QGraphicsPixmapItem和一些附加属性组成(如
std::string name

构造函数是这样的:

MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) :
QGraphicsPixmapItem(pixmap, parent), name(name){
...
}
    MyQGraphicsPixmapItem* item = static_cast<MyQGraphicsPixmapItem*>(QGraphicsScene::itemAt(...));
    std::cout << item->getName() << std::endl;
问题是:我在
qgraphicscene
中添加了一堆
MyQGraphicsPixmapItem
。但是当我使用方法
qgraphicscene::itemAt(constqpointf&position,constqtransform&deviceTransform)const
时,它返回
QGraphicsItem*
(而不是
MyQGraphicsPixmapItem*
)。所以我想我必须使用向下投射,对吗?但即使在使用了这种下压法之后:

MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const QPixmap &pixmap, std::string name, QGraphicsItem *parent) :
QGraphicsPixmapItem(pixmap, parent), name(name){
...
}
    MyQGraphicsPixmapItem* item = static_cast<MyQGraphicsPixmapItem*>(QGraphicsScene::itemAt(...));
    std::cout << item->getName() << std::endl;
MyQGraphicsPixmapItem*item=static_cast(qgraphicscene::itemAt(…);

std::cout getName()您应该能够使用
动态\u cast
,但是Qt也提供了自己的cast,如果项目属于给定类型,则返回该类型的项目,否则返回0

文件中的说明:

要使此函数与自定义项一起正确工作,请重新实现 每个自定义QGraphicsItem子类的type()函数

示例类:

class MyQGraphicsPixmapItem : public QGraphicsPixmapItem
{
public:
    ...
    enum { Type = UserType + 1 };
    int type() const override { return Type; }
    ...
};
示例测试:

MyQGraphicsPixmapItem myItem;
qDebug() << &myItem;
QGraphicsItem *item = &myItem;
MyQGraphicsPixmapItem *castedItem = qgraphicsitem_cast<MyQGraphicsPixmapItem*>(item);
if (castedItem) {
    qDebug() << castedItem;
} else {
    qWarning() << "casting failed";
}
MyQGraphicsPixmapItem myItem;

qDebug()一般来说:静态_cast可能会稍微快一点,但这是因为它不会被检查,动态_cast更可取,也许更可取的是我尝试了动态和QGraphicsItem cast,但仍然不起作用。我更新了我的问题,使它更简单。如果你发现了什么新东西,请检查一下并告诉我。我对
this.name=name
有点困惑。首先,为什么不在初始值设定项列表中指定它?其次
这个
是一个指针,所以我希望
这个->名称
.1。你说的初始化列表是什么意思?属性应该在构造函数中赋值。2.我更正了错误。初始值设定项列表由
引入。您可以执行以下操作:
MyQGraphicsPixmapItem::MyQGraphicsPixmapItem(const-QPixmap&pixmap,std::string name,QGraphicsItem*parent):QGraphicsPixmapItem(pixmap,parent),name(name){}
请查看我使用了您的类型转换(包括动态类型转换),但仍然不起作用。我还更新了问题,使其更易于理解。亲爱的上帝,当我读到“指定位置最上面可见的项目”时,我的内心触发了某种东西。事实上,我有一个光标,在我点击地图的正方形后会立即移动。这就是为什么字符串是空的,游标从未分配过他的
名称。我用
ScenePos
做了最后一次测试(它告诉我没有问题)。我还只打印指定了
名称的项目(这告诉我它们存在),这就是我感到困惑的原因。谢谢你的帮助。@A.Neo我想这可能是个原因。这就是为什么我写了,然后它应该有一个名称定义,而不是一个空字符串,如果你定义了你的所有自定义项目的名称。很高兴听到你把问题解决了。