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. Error: undefined reference to 'vtable for Widget'
Forum Update on Tuesday, May 27th 2025

Error: undefined reference to 'vtable for Widget'

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.5k 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.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by
    #1

    Hi,

    How to solve the error error: undefined reference to 'vtable for Widget' in the example:

    #include <QApplication>
    #include <QWidget>
    #include <QTimer>
    #include <QDebug>
    
    class Widget : public QWidget
    {
        Q_OBJECT
    public:
        Widget(QWidget *parent = nullptr) : QWidget(parent) {
            connect(&m_timer, &QTimer::timeout, this, &Widget::printMessage);
        }
    private slots:
        void printMessage() {
            qDebug() << "timer";
        }
    private:
        QTimer m_timer;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
        return a.exec();
    }
    
    
    JonBJ J.HilkJ 2 Replies Last reply
    0
    • 8Observer88 8Observer8

      Hi,

      How to solve the error error: undefined reference to 'vtable for Widget' in the example:

      #include <QApplication>
      #include <QWidget>
      #include <QTimer>
      #include <QDebug>
      
      class Widget : public QWidget
      {
          Q_OBJECT
      public:
          Widget(QWidget *parent = nullptr) : QWidget(parent) {
              connect(&m_timer, &QTimer::timeout, this, &Widget::printMessage);
          }
      private slots:
          void printMessage() {
              qDebug() << "timer";
          }
      private:
          QTimer m_timer;
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          Widget w;
          w.show();
          return a.exec();
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @8Observer8
      If you added the Q_OBJECT macro at some point, I would advise clearing out the debug/release directory and trying a complete rebuild. Does that solve it?

      The other possibility is that you need a virtual ~Widget() { }, at one point I thought that was required for QObjects but I don't actually think you have to have it now....

      1 Reply Last reply
      3
      • 8Observer88 8Observer8

        Hi,

        How to solve the error error: undefined reference to 'vtable for Widget' in the example:

        #include <QApplication>
        #include <QWidget>
        #include <QTimer>
        #include <QDebug>
        
        class Widget : public QWidget
        {
            Q_OBJECT
        public:
            Widget(QWidget *parent = nullptr) : QWidget(parent) {
                connect(&m_timer, &QTimer::timeout, this, &Widget::printMessage);
            }
        private slots:
            void printMessage() {
                qDebug() << "timer";
            }
        private:
            QTimer m_timer;
        };
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            Widget w;
            w.show();
            return a.exec();
        }
        
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @8Observer8

        you're trying to create a QObject based class inside main.cpp

        that's possible, but not intended.

        Like @JonB said, definitely add the Q_OBJECT macro,

        then explicitly include the moc file inside your main

        #include "main.moc"

        AFTER the class declaration


        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.

        8Observer88 1 Reply Last reply
        3
        • 8Observer88 Offline
          8Observer88 Offline
          8Observer8
          wrote on last edited by
          #4

          Clear/Rebuild - does not work.

          Clear/Run qmake/Rebuild/ -does not wok.

          Closing QtCreater and deleting the "build..." folder - does not work.

          This error accrues on these lines:

          class Widget : public QWidget
          
              Widget(QWidget *parent = nullptr) : QWidget(parent) {
          
          J.HilkJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @8Observer8

            you're trying to create a QObject based class inside main.cpp

            that's possible, but not intended.

            Like @JonB said, definitely add the Q_OBJECT macro,

            then explicitly include the moc file inside your main

            #include "main.moc"

            AFTER the class declaration

            8Observer88 Offline
            8Observer88 Offline
            8Observer8
            wrote on last edited by 8Observer8
            #5

            @J-Hilk said in Error: undefined reference to 'vtable for Widget':

            then explicitly include the moc file inside your main

            Error: main.cpp:22: error: main.moc: No such file or directory #include "main.moc" ^~~~~~~~~~

            1 Reply Last reply
            0
            • 8Observer88 8Observer8

              Clear/Rebuild - does not work.

              Clear/Run qmake/Rebuild/ -does not wok.

              Closing QtCreater and deleting the "build..." folder - does not work.

              This error accrues on these lines:

              class Widget : public QWidget
              
                  Widget(QWidget *parent = nullptr) : QWidget(parent) {
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              @8Observer8

              class Widget : public QWidget
              {
                  Q_OBJECT
              public:
                  Widget(QWidget *parent = nullptr) : QWidget(parent) {
                      connect(&m_timer, &QTimer::timeout, this, &Widget::printMessage);
                      m_timer.start(100);
                  }
              private slots:
                  void printMessage() {
                      qDebug() << "timer";
                  }
              private:
                  QTimer m_timer;
              };
              
              #include "main.moc"
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  Widget w;
                  w.show();
                  return a.exec();
              }
              

              explicitly execute qmake, as the moc file does not yet exists,
              than run normal build


              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
              • 8Observer88 Offline
                8Observer88 Offline
                8Observer8
                wrote on last edited by
                #7

                RMB on the project > "Run qmake" > but I see error: main.moc: No such file or directory #include "main.moc"

                J.HilkJ 1 Reply Last reply
                0
                • 8Observer88 8Observer8

                  RMB on the project > "Run qmake" > but I see error: main.moc: No such file or directory #include "main.moc"

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

                  @8Observer8 QtCreator is not updated (like I said its not the normal way to do this),
                  if you executed qmake, simply build the project


                  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
                  • 8Observer88 Offline
                    8Observer88 Offline
                    8Observer8
                    wrote on last edited by
                    #9

                    Yes, it works! Thank you very much!

                    #include <QApplication>
                    #include <QWidget>
                    #include <QTimer>
                    #include <QDebug>
                    
                    class Widget : public QWidget
                    {
                        Q_OBJECT
                    public:
                        Widget(QWidget *parent = nullptr) : QWidget(parent) {
                            connect(&m_timer, &QTimer::timeout, this, &Widget::printMessage);
                            m_timer.start(1000);
                        }
                    private slots:
                        void printMessage() {
                            qDebug() << "timer";
                        }
                    private:
                        QTimer m_timer;
                    };
                    
                    #include "main.moc"
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        Widget w;
                        w.show();
                        return a.exec();
                    }
                    
                    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