简体   繁体   中英

Qt: Signals and Slots in QGraphicsPixmapItem class

I have inherited a class called GraphicsPixmapItem from QGraphicsPixmapItem in order to override/create some mouse events. The problem is that I want to emit a signal after some calculations are performed, but it looks like it's not possible unless some hacks are performed, since this class is not a QObject .

To do so, I have tried to inherit the aformentioned new class from QObject as well, but I keep getting compiler errors.

My attemp:

Header file ( graphicspixmapitem.h ):

#ifndef GRAPHICSPIXMAPITEM_H
#define GRAPHICSPIXMAPITEM_H
#include <QObject>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneMouseEvent>

class GraphicsPixmapItem : public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    explicit GraphicsPixmapItem(QGraphicsItem *parent = 0);
    virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
signals:
    void translationVector(QPointF info);
};

#endif // GRAPHICSPIXMAPITEM_H

Source file ( graphicspixmapitem.cpp ):

#include "graphicspixmapitem.h"

GraphicsPixmapItem::GraphicsPixmapItem(QGraphicsItem *parent) :
    QGraphicsPixmapItem(parent)
{

}

void GraphicsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
    //Code
}

void GraphicsPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
    QPointF info;
    //Code
    emit(translationVector(info));
}

And I get the following linker errors:

undefined reference to `vtable for GraphicsPixmapItem'

undefined reference to `GraphicsPixmapItem::translationVector(QPointF)'

Any clue about how to proceed accordingly?

Side note:

I am aware that QGraphicsObject may be a good alternative, but as discussed here , performance looks severely affected by the amount of signals that are emitted when operating with them, where most of them will not be used in my case. This is the reason why I prefer to create my own class with base QGraphicsItem , instead of QGraphicsObject .

Many thanks in advance.

It looks like the meta object compiler (moc) wasn't run over the code, or that moc's result wasn't included when linking.

  • Have you added graphicspixmapitem.h to qmake's HEADERS variable?
  • Have you re-run qmake and tried a clean build in general?
  • Is moc run on graphicspixmapitem.h? Check your compile log.
  • Is graphicspixmapitem_moc.o included when linking? Check your compile log.

I have finally found out the problem involving the linkage error. In this sense, I must give my thanks to Thomas McGuire for pointing out the key idea to look for the source of the problem.

The reason is that few days ago I attempted to subclass QGraphicsPixmapItem (for other purposes) with the exact same name than this one, namely, GraphicsPixmapItem (with header file graphicspixmapitem.h and source file graphicspixmapitem.cpp ).

When I did that, I finally figured out that I could do things in a different way and I no longer needed that inherited subclass, hence I removed these files from the project. But doing this is a major mistake if you do not make sure to clean the project before removing the files from the project, because the files generated by qmake and moc ( *.o , moc_*.cpp , moc_*.o ) will remain in the build/debug directory unless you remove them by hand, because they will not be deleted by cleaning the project.

Therefore, it looks like in that situation, qmake detects that the files already exist and it does not generate the correct ones from the updated class, causing the linking error expounded above.

In summary, if you are going to delete some files from your project, make sure to clean it previously, especially if you are going to remove a class with the Q_OBJECT macro.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM