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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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 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 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.

        mrjjM 1 Reply Last reply
        0
        • ? A Former User

          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.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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 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 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
              0
              • ? A Former User

                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 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 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
                  0
                  • ? A Former User

                    @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 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 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
                      0
                      • ? A Former User

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

                        • Login

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