Q_OBJECT Problem in Visual C++

风格不统一 提交于 2019-12-11 10:57:58

问题


I have received a Visual C++ QT based project from our customer. I have installed QT libraries then I have compiled the project. The Project was compiled without any problems.

Now, I need to include a new additional GUI interface with the existing project. I have created a GUI in QT designer then saved in the source directory of VC++ project. Then I have written .h and .cpp file for new GUI and could call this interface. Now I need to include SIGNALS and SLOTS, when I include Q_OBJECT in .h file. I have compilation error.

These are the errors, please help me to solve this problem:

unresolved external symbol "public: virtual struct QMetaObject const * __thiscall BetaLineServer::metaObject(void)const " (?metaObject)

unresolved external symbol "public: virtual void * __thiscall BetaLineServer::qt_metacast(char const *)"

unresolved external symbol "public: virtual int __thiscall BetaLineServer::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@

回答1:


You probably just need to add your .ui, .cpp and .h files to the project file.

Qt will run 'moc' on the ui file if it's listed there, which creates source code to supply your missing symbols.




回答2:


Any time you add a Q_OBJECT macro to a class you need to be sure to rerun qmake and then compile.




回答3:


All you need is to compile the header files with moc that contain Q_OBJECT macro. And how to do that? here it is

  1. You can manually type commands for compiling moc or uic files OR

  2. -> Install Qt-VS addin. http://qt.nokia.com/downloads/visual-studio-add-in

    -> Now, open visual studio and create a new Qt project as described in here and then

    -> Right click on a header file that contains the Q_OBJECT macro you should find something like below. Copy these commands into your project.

    -> Replace the header file name with yours in the 'Command Line' command

    -> compile once and that should generate moc_xxxxx.cpp files, include them in your project.

  1. For future reference, if you create a Qt project in VS using this plugin you should have these commands automatically added



回答4:


Exclude .h file from the project and include it again - moc_.cpp will appear in the "Generated Files", and linking errors are disapeared.



来源:https://stackoverflow.com/questions/7272040/q-object-problem-in-visual-c

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