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. unresolved external symbol "public: virtual struct QMetaObject const *

unresolved external symbol "public: virtual struct QMetaObject const *

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 10.9k Views
  • 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by Perdrix
    #1

    I added this:

    class DSSImageToolBar : public QToolBar
    {
        Q_OBJECT
    
    public:
        explicit DSSImageToolBar(QWidget* parent) :
            QToolBar(parent),
            opacityEffect(this)
        {
        }
    
        ~DSSImageToolBar() {};
    
        void setOpacity(qreal opacity)
        {
            opacityEffect.setOpacity(opacity);
            setGraphicsEffect(&opacityEffect);
            update();
        };
    
    protected:
        void enterEvent(QEvent* e) override
        {
            setOpacity(1.0);
        };
    
        void leaveEvent(QEvent* e) override
        {
            setOpacity(0.5);
        };
    
        QGraphicsOpacityEffect opacityEffect;
    };
    

    to the front of my main.cpp file after the includes.

    For my pains I got:

    1>main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl DSSImageToolBar::metaObject(void)const " (?metaObject@DSSImageToolBar@@UEBAPEBUQMetaObject@@XZ)
    1>main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl DSSImageToolBar::qt_metacast(char const *)" (?qt_metacast@DSSImageToolBar@@UEAAPEAXPEBD@Z)
    1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl DSSImageToolBar::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@DSSImageToolBar@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
    

    If I put into a separate header it all works fine, but I didn't want to externalise that class definition.

    So why does it fail in the cpp but work in a header?

    J.HilkJ 1 Reply Last reply
    0
    • PerdrixP Perdrix

      @J-Hilk said in unresolved external symbol "public: virtual struct QMetaObject const *:

      @Perdrix this is inside main.cpp?

      have you added #include "main.moc" at the end of the file?

      At the end of main.cpp? Looking at the generated main.moc file, it seems probable that this would help. What part of the docs would tell me that I needed to do that? I failed to spot anything in the "Using the Meta-Object Compiler" part

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #9

      @Perdrix said in unresolved external symbol "public: virtual struct QMetaObject const *:

      At the end of main.cpp?

      yes,

      The problem is, that your class definition is located inside a source file, (main.cpp in this case) and that is messing up qmake

      take a look at this stack overflow answer, a very good one
      https://stackoverflow.com/a/34929627

      I'm not sure if its explicitly featured in the Meta-Object-Compiler documentation 🤷‍♂️


      edit:
      just checked, it is mentioned
      https://doc.qt.io/qt-5/moc.html#writing-make-rules-for-invoking-moc
      42840479-782c-4340-a73e-9f0079abd226-image.png


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #2

        Try to clean the project and rebuild

        PerdrixP 1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #3

          When subclassing a QObject, you'd better write .h and .cpp separately.
          Or there maybe some "mocing" problems...

          PerdrixP 1 Reply Last reply
          0
          • gde23G gde23

            Try to clean the project and rebuild

            PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote on last edited by
            #4

            @gde23 said in unresolved external symbol "public: virtual struct QMetaObject const *:

            Try to clean the project and rebuild

            Tried that :(

            1 Reply Last reply
            0
            • B Bonnie

              When subclassing a QObject, you'd better write .h and .cpp separately.
              Or there maybe some "mocing" problems...

              PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by
              #5

              @Bonnie said in unresolved external symbol "public: virtual struct QMetaObject const *:

              When subclassing a QObject, you'd better write .h and .cpp separately.
              Or there maybe some "mocing" problems...

              So I see - not ideal ...

              1 Reply Last reply
              0
              • PerdrixP Perdrix

                I added this:

                class DSSImageToolBar : public QToolBar
                {
                    Q_OBJECT
                
                public:
                    explicit DSSImageToolBar(QWidget* parent) :
                        QToolBar(parent),
                        opacityEffect(this)
                    {
                    }
                
                    ~DSSImageToolBar() {};
                
                    void setOpacity(qreal opacity)
                    {
                        opacityEffect.setOpacity(opacity);
                        setGraphicsEffect(&opacityEffect);
                        update();
                    };
                
                protected:
                    void enterEvent(QEvent* e) override
                    {
                        setOpacity(1.0);
                    };
                
                    void leaveEvent(QEvent* e) override
                    {
                        setOpacity(0.5);
                    };
                
                    QGraphicsOpacityEffect opacityEffect;
                };
                

                to the front of my main.cpp file after the includes.

                For my pains I got:

                1>main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl DSSImageToolBar::metaObject(void)const " (?metaObject@DSSImageToolBar@@UEBAPEBUQMetaObject@@XZ)
                1>main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl DSSImageToolBar::qt_metacast(char const *)" (?qt_metacast@DSSImageToolBar@@UEAAPEAXPEBD@Z)
                1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl DSSImageToolBar::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@DSSImageToolBar@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
                

                If I put into a separate header it all works fine, but I didn't want to externalise that class definition.

                So why does it fail in the cpp but work in a header?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @Perdrix this is inside main.cpp?

                have you added #include "main.moc" at the end of the file?


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                PerdrixP 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @Perdrix this is inside main.cpp?

                  have you added #include "main.moc" at the end of the file?

                  PerdrixP Offline
                  PerdrixP Offline
                  Perdrix
                  wrote on last edited by
                  #7

                  @J-Hilk said in unresolved external symbol "public: virtual struct QMetaObject const *:

                  @Perdrix this is inside main.cpp?

                  have you added #include "main.moc" at the end of the file?

                  At the end of main.cpp? Looking at the generated main.moc file, it seems probable that this would help. What part of the docs would tell me that I needed to do that? I failed to spot anything in the "Using the Meta-Object Compiler" part

                  J.HilkJ 1 Reply Last reply
                  0
                  • gde23G Offline
                    gde23G Offline
                    gde23
                    wrote on last edited by gde23
                    #8

                    EDIT: I just tested it and it is working for me with class being in .h file.
                    If the class is somewhere in a source file it does not work.

                    1 Reply Last reply
                    0
                    • PerdrixP Perdrix

                      @J-Hilk said in unresolved external symbol "public: virtual struct QMetaObject const *:

                      @Perdrix this is inside main.cpp?

                      have you added #include "main.moc" at the end of the file?

                      At the end of main.cpp? Looking at the generated main.moc file, it seems probable that this would help. What part of the docs would tell me that I needed to do that? I failed to spot anything in the "Using the Meta-Object Compiler" part

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by J.Hilk
                      #9

                      @Perdrix said in unresolved external symbol "public: virtual struct QMetaObject const *:

                      At the end of main.cpp?

                      yes,

                      The problem is, that your class definition is located inside a source file, (main.cpp in this case) and that is messing up qmake

                      take a look at this stack overflow answer, a very good one
                      https://stackoverflow.com/a/34929627

                      I'm not sure if its explicitly featured in the Meta-Object-Compiler documentation 🤷‍♂️


                      edit:
                      just checked, it is mentioned
                      https://doc.qt.io/qt-5/moc.html#writing-make-rules-for-invoking-moc
                      42840479-782c-4340-a73e-9f0079abd226-image.png


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      3
                      • PerdrixP Offline
                        PerdrixP Offline
                        Perdrix
                        wrote on last edited by
                        #10

                        It's sort of documented there but I never wrote a make rule for this as I am using Visual Studio ...

                        Which means I'd never have looked there! Should be clear and up front in the moc documentation ...

                        Thanks for the explanation

                        1 Reply Last reply
                        1
                        • S Offline
                          S Offline
                          snyang
                          wrote on last edited by
                          #11

                          In the visual studio, we need to change the .h propeerty, set Item Type = "Qt Meta-Object Compiler (moc)".

                          1 Reply Last reply
                          1

                          • Login

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