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. Compiler Error with Q_DECLARE_METATYPE
Forum Updated to NodeBB v4.3 + New Features

Compiler Error with Q_DECLARE_METATYPE

Scheduled Pinned Locked Moved Solved General and Desktop
qvariantmetatype
16 Posts 4 Posters 8.8k Views 3 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 29 May 2017, 19:02 last edited by A Former User
    #1

    Hi fellow Qters,
    I have a small class to describe a dataset, which I would like to use in the context of the model/view system and thus my class needs to act like a QVariant. I have read the Qt documentation of the respective classes and my code looks like this (simplified to the basic parts):

    class Dataset
    {
      public:
        
        enum DatasetType
        {
    	TypeA = 0,
    	TypeB = 1,
    	TypeC = 2,
    	TypeD = 3,
        };
        
        Dataset();
        Dataset(const Dataset& _data);
        ~Dataset();
         ...
    
      private:
    
        ...
        DatysetType m_type;
        ...
    
    }
    
    Q_DECLARE_METATYPE(Dataset)
    

    After declaring Dataset a Metatype, I'm using it in my code like this:

    ...
    QVariant var;
    Dataset data;
    var.setValue(data);
    ...
    

    When I'm trying to compile the code, I get this compile error, however:

    /usr/include/QtCore/qmetatype.h(169): error: class "QMetaTypeId<Dataset *>" has no member "qt_metatype_id"
          static inline int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
                                                                      ^
         detected during:
         instantiation of "int QMetaTypeId2<T>::qt_metatype_id() [with T=Dataset *]" at line 230
         instantiation of "int qMetaTypeId(T *) [with T=Dataset *]" at line 463 of "/usr/include/QtCore/qvariant.h"
         instantiation of "void qVariantSetValue(QVariant &, const T &) [with T=Dataset *]" at line 528 of "/usr/include/QtCore/qvariant.h"
         instantiation of "void QVariant::setValue(const T &) [with T=Dataset *]" at line 18 of "/home/goetz/Projects/playground/source/Shared/GUI/DatasetTreeModel.cpp"
    
    compilation aborted for /home/goetz/Projects/playground/source/Shared/GUI/DatasetTreeModel.cpp (code 2)
    

    Has anyone seen this before and knows how to get around? I haven't found anything on google, that would help me out.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 May 2017, 19:10 last edited by mrjj
      #2

      Hi
      @wieland suggested
      he has this:
      Q_DECLARE_METATYPE(Dataset)
      but uses this:
      QMetaTypeId<Dataset *> ( the :setValue(const T &) [with T=Dataset *] )

      he needs a Q_DECLARE_METATYPE(Dataset*) too

      Can you try that ?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on 29 May 2017, 19:24 last edited by
        #3

        Hi,

        good idea, but adding Q_DECLARE_METATYPE(Dataset*) didn't help either. The same compiler error persits.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on 29 May 2017, 19:32 last edited by
          #4

          I have

          #ifndef DATASET_H
          #define DATASET_H
          
          #include <QMetaType>
          
          class Dataset
          {
          public:
              enum DatasetType
              {
                  TypeA = 0,
                  TypeB = 1,
                  TypeC = 2,
                  TypeD = 3,
              };
          
              Dataset();
              Dataset(const Dataset& _data);
              ~Dataset();
          
          private:
              DatasetType m_type;
          };
          
          Q_DECLARE_METATYPE(Dataset)
          
          #endif // DATASET_H
          

          and

          #include "dataset.h"
          
          Dataset::Dataset()
          {
          }
          
          Dataset::Dataset(const Dataset &_data)
          {
              Q_UNUSED(_data)
          }
          
          Dataset::~Dataset()
          {
          }
          

          and use it with this:

          void MainWindow::on_pushButton_clicked()
          {
              QVariant var;
              Dataset data;
              var.setValue(data);
              qDebug() << var.canConvert(QMetaType::Bool);
          }
          

          Can't reproduce the problem so far.

          1 Reply Last reply
          1
          • ? Offline
            ? Offline
            A Former User
            wrote on 29 May 2017, 19:47 last edited by
            #5

            If I do this:

            void MainWindow::on_pushButton_clicked()
            {
                QVariant var;
                Dataset * data = new Dataset;
                var.setValue(data);
                qDebug() << var.canConvert(QMetaType::Bool);
            }
            
            

            I get a similar compile error, but that can be fixed by adding: Q_DECLARE_METATYPE(Dataset*)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 29 May 2017, 19:48 last edited by
              #6

              Thank you @Wieland
              Also tried it in Qt 5.7. mingw and it compiled and ran.

              Just to be sure. Please delete the build folder completely and rebuild.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on 29 May 2017, 19:54 last edited by
                #7

                Just to be sure it's not just a typo, because I've noticed you made the same typo in your cross-posting on qtcentre: there needs to be a semicolon after your class definition (before the QT_DECLARE_METATYPE).

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on 29 May 2017, 22:55 last edited by
                  #8

                  It's just a typo in the post. I do have a a semicolon after the class declaration and before the Q_DECLARE_METATYPE macro.

                  I've also tried to add Q_DECLARE_METATYPE(Dataset) and Q_DECLARE_METATYPE(Dataset*) without success. I still get the same compiler error.

                  M 1 Reply Last reply 29 May 2017, 23:12
                  0
                  • ? A Former User
                    29 May 2017, 22:55

                    It's just a typo in the post. I do have a a semicolon after the class declaration and before the Q_DECLARE_METATYPE macro.

                    I've also tried to add Q_DECLARE_METATYPE(Dataset) and Q_DECLARE_METATYPE(Dataset*) without success. I still get the same compiler error.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 29 May 2017, 23:12 last edited by
                    #9

                    @tobiSF
                    There must be something more we dont see.
                    Can you try this test project that compiles for me
                    https://www.dropbox.com/s/60h20wkvacjbwag/DECLAREMETATYPE.zip?dl=0

                    1 Reply Last reply
                    1
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on 30 May 2017, 15:45 last edited by
                      #10

                      @mrjj, I downloaded the project and I could compile it. But only after I have removed the trailing coma from the enum and after putting a comment around this line

                      qDebug() << var.canConvert(QMetaType::Bool);
                      

                      Otherwise the compiler would show the following error message:

                      mainwindow.cpp(12): error: no instance of overloaded function "QVariant::canConvert" matches the argument list
                                  argument types are: (QMetaType::Type)
                                  object type is: QVariant
                            qDebug() << var.canConvert(QMetaType::Bool);
                                            ^
                      
                      compilation aborted for mainwindow.cpp (code 2)
                      Makefile:209: recipe for target 'mainwindow.o' failed
                      make: *** [mainwindow.o] Error 2
                      

                      But I guess the part that counts - the metatype declaration - seems to work.

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on 30 May 2017, 16:05 last edited by
                        #11

                        Quick update, I have changed the following line

                        Dataset* data;
                        QVariant child;
                        child.setValue(data);
                        

                        to this

                        Dataset* data;
                        QVariant child = QVariant::fromValue(data);
                        

                        and now it's compiling fine. Any idea what might cause this?

                        kshegunovK 1 Reply Last reply 31 May 2017, 07:37
                        0
                        • ? A Former User
                          30 May 2017, 16:05

                          Quick update, I have changed the following line

                          Dataset* data;
                          QVariant child;
                          child.setValue(data);
                          

                          to this

                          Dataset* data;
                          QVariant child = QVariant::fromValue(data);
                          

                          and now it's compiling fine. Any idea what might cause this?

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on 31 May 2017, 07:37 last edited by
                          #12

                          @tobiSF said in Compiler Error with Q_DECLARE_METATYPE:

                          You have:

                          Q_DECLARE_METATYPE(Dataset)
                          

                          but then use a pointer to that type:

                          Dataset* data;
                          QVariant child = QVariant::fromValue(data);
                          

                          those are not interchangeable. It's evident even from your original post (as @Wieland noted):

                          instantiation of "void QVariant::setValue(const T &) [with T=Dataset *]" at line 18 of "/home/goetz/Projects/playground/source/Shared/GUI/DatasetTreeModel.cpp"

                          The instantiation is with a pointer to Dataset not with a Dataset object.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          2
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on 31 May 2017, 20:17 last edited by
                            #13

                            @kshegunov thanks, as you said, @Wieland pointed it out and I fixed that right away (without updating the code in the initial post, however. The compiler error persisted and only went away after switching from setValue(data) to QVariant::fromValue(data). Is still can't compile the code when I use the setValue method, but with the QVariant::fromValue I can do what I wanted to.

                            kshegunovK 1 Reply Last reply 31 May 2017, 20:38
                            0
                            • ? A Former User
                              31 May 2017, 20:17

                              @kshegunov thanks, as you said, @Wieland pointed it out and I fixed that right away (without updating the code in the initial post, however. The compiler error persisted and only went away after switching from setValue(data) to QVariant::fromValue(data). Is still can't compile the code when I use the setValue method, but with the QVariant::fromValue I can do what I wanted to.

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on 31 May 2017, 20:38 last edited by kshegunov
                              #14

                              @tobiSF said in Compiler Error with Q_DECLARE_METATYPE:

                              I fixed that right away (without updating the code in the initial post, however

                              Sorry if I seem thick, but I don't see it. I saw you attempted to declare the pointer as a metatype, but not that you tried calling the function with an actual object. There seems to be some confusion, so I'll try to expand. I'm referring to this error:

                              instantiation of "void QVariant::setValue(const T &) [with T=Dataset *]" at line 18 of "/home/goetz/Projects/playground/source/Shared/GUI/DatasetTreeModel.cpp"
                              

                              which suggests you instantiate QVariant::setValue with a pointer to the type, not with the actual type you intend to use. Do you mind providing line18 and the few preceding lines of DatasetTreeModel.cpp (where the argument passed to the function is declared) exactly as it appears when you get the compile error? Because this error somehow doesn't correspond to the source sample you provided in the OP.

                              PS.
                              QVariant::setValue and QVariant::fromValue do exactly the same thing for non-qt types (i.e. the ones you declare).

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              1
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on 31 May 2017, 21:29 last edited by
                                #15

                                @kshegunov to be more precise, I fixed the metatype declaration:

                                ...
                                Q_DECLARE_METATYPE(Dataset)
                                Q_DECLARE_METATYPE(Dataset*)
                                ...
                                

                                I was and still am instantiating the QVariant with a pointer to the type and not an actual type, which is why the initial declaration of the type as metatype didn't work. Now that I have both declared as metatype, it works with pointers.

                                kshegunovK 1 Reply Last reply 31 May 2017, 22:12
                                0
                                • ? A Former User
                                  31 May 2017, 21:29

                                  @kshegunov to be more precise, I fixed the metatype declaration:

                                  ...
                                  Q_DECLARE_METATYPE(Dataset)
                                  Q_DECLARE_METATYPE(Dataset*)
                                  ...
                                  

                                  I was and still am instantiating the QVariant with a pointer to the type and not an actual type, which is why the initial declaration of the type as metatype didn't work. Now that I have both declared as metatype, it works with pointers.

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on 31 May 2017, 22:12 last edited by
                                  #16

                                  Ah, okay, so it's just me being dense.

                                  Read and abide by the Qt Code of Conduct

                                  1 Reply Last reply
                                  0

                                  1/16

                                  29 May 2017, 19:02

                                  • Login

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