最近在看QT中图形视图。自己模仿着写了个小例子。由于功能需要,所以要添加信号槽,于是自己就动手添加起来。在
添加的过程中遇到了许多问题,现总结如下:
1.要自定义添加信号槽必须要继承QObject。
2.还要添加宏Q_OBJECT
在继承时需要注意,祖父类必须放在父类之前。
一切准备就绪,再次编译总是出现
Class CalendarItem implements the interface QGraphicsItem but does not list it in Q_INTERFACES. qobject_cast to QGraphicsItem will not work!
这个警告虽然不是错误,但是让我看着很不爽。于是就上网搜索此类答案,
在我的异常网上搜得结果是没有实现下列接口:
void keyPressEvent(QKeyEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);但是当我实现后编译还有错。于是就打开QT帮助文档,展现在我面前的令我眼前一亮。
class BasicToolsPlugin : public QObject,
public BrushInterface,
public ShapeInterface,
public FilterInterface
{
Q_OBJECT
Q_INTERFACES(BrushInterface ShapeInterface FilterInterface)
public:
...
};
于是我终于思得结果。好高兴啊。
以上是小弟的见解,不对之处多多指出,我们一起进步,一起努力。