Q_OBJECT linker error!

空扰寡人 提交于 2019-11-30 12:24:18

Such errors usually mean that you haven't added the header of your class to "HEADERS" variable in pro file (meta object compiler generates moc_ files only for headers listed in this variable). Remember to run qmake after you change .pro file!

I had a similar problem and it was solved using andref's feedback. Within QT Creator I simply:

  1. Build/Clean all
  2. Build/Run qmake
  3. Build/Run

Whenever you change QObject inheritance be sure to do a clean, qmake then build. The qmake is important since it updates moc* files for any new Qt changes in your .h files including QObject inheritance, ie Q_OBJECT. In fact, in some cases, you may even be able to simply do qmake then build for an incremental build.

Carlos Nieto

Check in the file MakeFile.debug and maybe HIMyClass don't exists.

I just rename MakeFile.debug, Clean the Project and Rebuild All and it compiles.

Tomek

I had the same problem but in my case it wasn't enough to clean -> build. So I had to delete manually all files created in build process (Mekefiles, ui descriptionns in cpp, and generally whole directory created by build process) and only then build succeded.

rubenvb

Check that the necessary Qt config options are present in the pro file (QT += core gui at least. Also try manually deleting everything built/created in the build directory. It sometimes happens that moc fails to run for some reason.

You can also try running the moc command yourself, and see what it outputs (you can find the command line in the tab "Compile output" in QtCreator.

UPDATE: this related problem seems to suggest you don't define QT_DLL when compiling. Can you try a fresh and new simple QtCreator project (with a widget that subclasses mainwindow for example) and try that. It should contain a Q_OBJECT header automatically and try to compare the .pro files and compiler output.

jimmy

on my osx box this was due to missing moc* files. i fixed this by removing the bom from my utf-8 encoded .pro file. i will file a bug with qt.

error for goggle searches... NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

"vtable for MainWindow", referenced from:

 MainWindow::MainWindow(QWidget*)in mainwindow.o

I found another possible cause and solution to this error

This error will also occur if one has declared the slot in .h file but has not defined its body in the implementation

I had this problem. Verify whether there is a description of the implementation of the slot in .cpp file.

I had removed #include "main.moc" from my main file, and forgot to re-add it... That was a fun time-waster!

Answers from @chalup and @ierax helped me. I had to close the Qt creator and open it again though for qmake to take effect. I followed these steps: 1. Moved the class definition to a header file. 2. Added header file to project and ensured it is listed against HEADERS += \ list in .pro file. 3. Clean-all 4. close QtCreator (on Windows 10) 5. Delete Makefiles from the project directory 6. Open QtCreator and open the project. 7. Qmake to ensure makefiles are generated. 8. Rebuild-all

These steps helped me solve this issue - I struggled for over an hour with various other answers and methods nothing worked. Before you run qmake ensure you delete makefiles and close QtCreator (applicable atleast on windows 10).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!