QGraphicsItem Custom class-> type() 不起作用
QGraphicsItem custom class-> type() not working
我在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 thanks in advance
QGraphicsItem *item = bordscene->selectedItems().at(0);
if (item->type()==7)
checkGebouwSelectie();
else if (item->type()==8)
checkPlantageSelectie();
实际上并没有重写类型函数。您的int type()
函数是非const的,而类文档显示虚拟QGraphicsItem函数是const的。constness需要匹配你的函数来覆盖QGraphicsItem函数。
如果你有一个c++ 11编译器,你可以指定override,以确保如果你的函数实际上没有覆盖一个虚方法,这是一个编译器错误。从Qt5开始,在QtGlobal中定义了一个宏Q_DECL_OVERRIDE
,它将成为支持它的编译器的override关键字,或者不支持它的编译器的override关键字。
我还注意到你也检查了item->type()==7
和item->type()==8
。在Qt的版本我有方便(4.7.2),这些类型值分别对应于QGraphicsPixmapItem和QGraphicsTextItem。你确定这就是你想要的价值观吗?我希望比较是item->type() == FotoGebouw::TypeFotoGebouw
和item->type() == FotoGebouw::TypeFotoPlantage
。
当您使用qgraphicsitem_cast
与您的方法时,会出现问题,
template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
{
return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
|| (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
}
您应该遵循文档http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#type
中的示例声明Type
并将其作为结果返回int type() const
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- QGraphicsItem Custom class-> type() 不起作用
- 当比较sizeof(type) == constant时,c++模板特化不起作用