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 *
Forum Updated to NodeBB v4.3 + New Features

unresolved external symbol "public: virtual struct QMetaObject const *

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 11.1k 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.
  • P Offline
    P Offline
    Perdrix
    wrote on 28 Aug 2020, 08:54 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 1 Reply Last reply 28 Aug 2020, 09:12
    0
    • P Perdrix
      28 Aug 2020, 09:20

      @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 Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 28 Aug 2020, 09:32 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
      • G Offline
        G Offline
        gde23
        wrote on 28 Aug 2020, 08:58 last edited by
        #2

        Try to clean the project and rebuild

        P 1 Reply Last reply 28 Aug 2020, 09:10
        0
        • B Offline
          B Offline
          Bonnie
          wrote on 28 Aug 2020, 09:06 last edited by
          #3

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

          P 1 Reply Last reply 28 Aug 2020, 09:11
          0
          • G gde23
            28 Aug 2020, 08:58

            Try to clean the project and rebuild

            P Offline
            P Offline
            Perdrix
            wrote on 28 Aug 2020, 09:10 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
              28 Aug 2020, 09:06

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

              P Offline
              P Offline
              Perdrix
              wrote on 28 Aug 2020, 09:11 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
              • P Perdrix
                28 Aug 2020, 08:54

                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 Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 28 Aug 2020, 09:12 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.

                P 1 Reply Last reply 28 Aug 2020, 09:20
                0
                • J J.Hilk
                  28 Aug 2020, 09:12

                  @Perdrix this is inside main.cpp?

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

                  P Offline
                  P Offline
                  Perdrix
                  wrote on 28 Aug 2020, 09:20 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 1 Reply Last reply 28 Aug 2020, 09:32
                  0
                  • G Offline
                    G Offline
                    gde23
                    wrote on 28 Aug 2020, 09:29 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
                    • P Perdrix
                      28 Aug 2020, 09:20

                      @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 Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 28 Aug 2020, 09:32 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
                      • P Offline
                        P Offline
                        Perdrix
                        wrote on 28 Aug 2020, 10:20 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 22 Mar 2023, 08:02 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