Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using Q_DECLARE_METATYPE
Forum Updated to NodeBB v4.3 + New Features

Using Q_DECLARE_METATYPE

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    grsabourin
    wrote on 29 Aug 2018, 00:58 last edited by
    #1

    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?

    R 1 Reply Last reply 29 Aug 2018, 06:26
    0
    • G grsabourin
      29 Aug 2018, 13:30

      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_OBJECT

      public:
      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)
      {

      }

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 29 Aug 2018, 14:08 last edited by raven-worx
      #5

      @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 guess

      don't declare your mouse-handlers as slots

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 29 Aug 2018, 04:02 last edited by
        #2

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        3
        • G grsabourin
          29 Aug 2018, 00:58

          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?

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 29 Aug 2018, 06:26 last edited by
          #3

          @grsabourin

          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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2
          • G Offline
            G Offline
            grsabourin
            wrote on 29 Aug 2018, 13:30 last edited by
            #4

            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_OBJECT

            public:
            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)
            {

            }

            R 1 Reply Last reply 29 Aug 2018, 14:08
            0
            • G grsabourin
              29 Aug 2018, 13:30

              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_OBJECT

              public:
              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)
              {

              }

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 29 Aug 2018, 14:08 last edited by raven-worx
              #5

              @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 guess

              don't declare your mouse-handlers as slots

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2
              • G Offline
                G Offline
                grsabourin
                wrote on 29 Aug 2018, 14:17 last edited by
                #6

                You are right. It solve my issue. Thank you for your help.

                aha_1980A 1 Reply Last reply 29 Aug 2018, 17:19
                1
                • G grsabourin
                  29 Aug 2018, 14:17

                  You are right. It solve my issue. Thank you for your help.

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 29 Aug 2018, 17:19 last edited by
                  #7

                  @grsabourin Glad you figured it out. So please mark this topic as SOLVED too.

                  Thanks!

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  0

                  1/7

                  29 Aug 2018, 00:58

                  • Login

                  • Login or register to search.
                  1 out of 7
                  • First post
                    1/7
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved