Using Q_DECLARE_METATYPE
-
Hi,
I'm derivating a class from QGraphicsView. When I add some mouse event function in this class as the following
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);I received the following error
Error C2338 Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system (compiling source file GeneratedFiles\Debug\moc_MyFile.cpp)
It seems I need to add Q_DECLARE_METATYPE to my code. The compiler error is in the moc file.
1 - Do I need to add it in the moc file or in MyFile.h?
2 -For the mouse event, what metatype I need to define?
-
@grsabourin
no line of code you've posted makes use of any meta-types
So the problem comes from somewhere else in your code i guessdon't declare your mouse-handlers as slots
-
I don't see any issue with your above function. You need to use this macro to register if there is a new user defined data type you are using. We suspect that you are using some User defined data type somewhere. It may be coming from signal/slots.
Please help us with complete class file. It should help us to find the issue.
-
1 - Do I need to add it in the moc file or in MyFile.h?
you never ever need to edit the moc file, since this is generated again in the next build anyway.
2 -For the mouse event, what metatype I need to define?
The overridden mouse event handlers do not need a meta-type to be registered.
What are you doing in your reimplemented mouse handlers?
A meta type needs to be registered when you want to put a custom type into a QVariant or emit a signal with a custom type parameter via a queued connection. -
Thank you for your answers. Yes I understand that the moc file will be rewrite at each build
This is complete code of my class. For now it is just a starting point is the class is doing mostly nothing
MyFile.h
#pragma once
#include <QGraphicsView>
#include <QMouseEvent>
#include <QMetaType>
#include <Qt>#include <fstream>
class MyFile : public QGraphicsView
{
Q_OBJECTpublic:
MyFile(QWidget * parent = Q_NULLPTR);
~MyFile();void LoadViewImage(QImage &in_QImage); void Refresh(); private slots : void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);
private:
QImage m_image;
};
MyFile.cpp
#include "MyFile.h"MyFile::MyFile(QWidget * parent) : QGraphicsView ( parent )
{}
MyFile::~MyFile()
{}
void MyFile::LoadViewImage(QImage &in_image)
{
m_image = in_image;
}void MyFile::Refresh()
{
resetCachedContent();
QGraphicsScene * graphic = new QGraphicsScene(this);
graphic->addPixmap(QPixmap::fromImage( displayImage ) );
setScene(graphic);
}void MyFile::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton)
{} else if (event->buttons() == Qt::RightButton) { }
}
void ImageView::mouseMoveEvent(QMouseEvent *event)
{
}void ImageView::mouseReleaseEvent(QMouseEvent *event)
{}
-
@grsabourin
no line of code you've posted makes use of any meta-types
So the problem comes from somewhere else in your code i guessdon't declare your mouse-handlers as slots
-
You are right. It solve my issue. Thank you for your help.
-
@grsabourin Glad you figured it out. So please mark this topic as SOLVED too.
Thanks!