gpt4 book ai didi

c++ - QGraphicsItem 自定义类-> type() 不工作

转载 作者:行者123 更新时间:2023-11-30 02:54:28 25 4
gpt4 key购买 nike

我在 QGraphicsScene 上放置了两个自定义类型,这是它们的声明:

class FotoGebouw : public QGraphicsItem
{
public:
explicit FotoGebouw();
~FotoGebouw();
Gebouw *linkGebouw;
enum ItemType { TypeFotoGebouw = UserType + 1, TypeFotoPlantage = UserType + 2};
int type(){ return TypeFotoGebouw; }
signals:
public slots:
};

class FotoPlantage : public QGraphicsItem
{
public:
explicit FotoPlantage();
~FotoPlantage();
Plantage *linkPlantage;
enum ItemType { TypeFotoGebouw = UserType + 1, TypeFotoPlantage = UserType + 2};
int type(){ return TypeFotoPlantage; }
signals:
public slots:
};

现在,当我在 QGraphicsScene 上选择一个项目时,我想找出它是这两个类中的哪种类型,但我该怎么做呢?我尝试了以下方法,但它总是返回相同的类型...:S 提前致谢

    QGraphicsItem *item = bordscene->selectedItems().at(0);
if (item->type()==7)
checkGebouwSelectie();
else if (item->type()==8)
checkPlantageSelectie();

最佳答案

您实际上并没有覆盖类型函数。你的int type()函数是非常量的,而 the class documentation显示虚拟 QGraphicsItem 函数是常量。 constness 需要与您的函数匹配以覆盖 QGraphicsItem 函数。

如果你有 C++11 编译器,你可以指定 override如果您的函数实际上没有重写虚方法,以确保它是一个编译器错误。从Qt5开始,有一个宏Q_DECL_OVERRIDEQtGlobal 中定义对于支持它的编译器,它将成为覆盖关键字,对于不支持它的编译器,它将成为无。

我还注意到你也在检查 item->type()==7item->type()==8 .在我手边的 Qt 版本(4.7.2)中,这些类型值分别对应于 QGraphicsPixmapItem 和 QGraphicsTextItem。您确定这些是您要查找的值吗?我希望比较是 item->type() == FotoGebouw::TypeFotoGebouwitem->type() == FotoGebouw::TypeFotoPlantage .

关于c++ - QGraphicsItem 自定义类-> type() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17070270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com