Qt moc error using CMake

让人想犯罪 __ 提交于 2019-12-02 14:44:37

问题


I am trying to write an OpenCV application creating the GUI with Qt and generating the makefiles using CMake. The problem that I have is that when I try to execute the make command, I get the error:

This file was generated using the moc from 5.2.1. It cannot be used with the include files from this version of Qt. (The moc has changed too much.)

Okay. So I guess I should be trying to use the moc from qt4 rather than qt5 for this (I have both qt4 and qt5 installed). Sure enough I have /usr/bin/moc-qt4 as well as /usr/bin/moc, so I would have thought all I need to do is invoke sudo update-alternatives --config moc, but all I get is update-alternatives: error: no alternatives for moc. Hmmm...

So I think the issue is either with the version of qt or in my CMakeLists.txt, so here is the code I used for that:

cmake_minimum_required(VERSION 2.8.9)
project (Vigil)

include_directories(
    build
    src
    inc
    uis
)

######################### Configure OpenCV inclusion ######################### 

FIND_PATH(CV_INCLUDE_DIR cv.h
    /usr/include/opencv
)

MESSAGE(STATUS "OpenCV: CV Path : " ${CV_INCLUDE_DIR})

FIND_PATH(CVAUX_INCLUDE_DIR cvaux.h
    /usr/include/opencv
    /usr/local/include/opencv
    /usr/local/include/opencv/cvaux/include
    /usr/local/include/opencv/include 
)

MESSAGE(STATUS "OpenCV: CVAux Path : " ${CXCORE_INCLUDE_DIR})

FIND_PATH(CXCORE_INCLUDE_DIR cxcore.h
    /usr/include/opencv
    /usr/local/include/opencv
    /usr/local/include/opencv/cxcore/include
)

MESSAGE(STATUS "OpenCV: CXCore Path : " ${CVAUX_INCLUDE_DIR})

FIND_PATH(HIGHGUI_INCLUDE_DIR highgui.h
    /usr/include/opencv
    /usr/local/include/opencv/otherlibs/highgui
    /opt/local/include/opencv
    /usr/local/include/opencv 
)

MESSAGE(STATUS "OpenCV: HighGUI Path : " ${HIGHGUI_INCLUDE_DIR})

FIND_LIBRARY(CV_LIB NAMES opencv_imgproc PATHS
    /usr/lib
    /usr/local/lib
    /usr/lib64
    /usr/local/lib64
)

MESSAGE(STATUS "OpenCV: CV Lib: " ${CV_LIB})     

FIND_LIBRARY(CXCORE_LIB NAMES opencv_core PATHS
    /usr/lib
    /usr/local/lib
    /usr/lib64
)

MESSAGE(STATUS "OpenCV: CXCore Lib: " ${CXCORE_LIB}) 

FIND_LIBRARY(HIGHGUI_LIB NAMES opencv_highgui PATHS
    /usr/lib
    /usr/local/lib
    /usr/lib64
    /usr/local/lib64
)

MESSAGE(STATUS "OpenCV: HighGUI Lib: " ${HIGHGUI_LIB})      

INCLUDE_DIRECTORIES(
    ${HIGHGUI_INCLUDE_DIR}
    ${OPENCV_INCLUDE_DIR}
    ${CXCORE_INCLUDE_DIR}
    ${CVAUX_INCLUDE_DIR}
)

######################### Configure QT inclusion ######################### 
FIND_PACKAGE(Qt5Widgets)
FIND_PACKAGE(Qt4 REQUIRED)
include_directories( ${QT_INCLUDES} )
IF(QT4_FOUND)
    MESSAGE(STATUS "QT found : YES")
ELSE(QT4_FOUND)
    MESSAGE(FATAL_ERROR
    "Cannot build without Qt4.  Please set Qt4_DIR.")
ENDIF(QT4_FOUND)

######################### Add sources, headers and uis ######################### 

set(SOURCES ${SOURCES}
    main.cpp
    VigilWidget.cpp
)

SET( HEADERS ${HEADERS}
    VigilWidget.h
)

SET( UI_FILES
    bugView.ui
)

######################### Run UIC on .ui files #########################

QT4_WRAP_UI( UI_HDRS ${UI_FILES} )
MESSAGE(STATUS "UI_HEADERS: " ${UI_HDRS})
# and finally this will run moc:
#QT4_AUTOMOC(${HEADERS})
QT4_WRAP_CPP( MOC_HEADERS  ${HEADERS} )

#########################  ######################### 

ADD_EXECUTABLE(Vigil ${UI_HDRS} ${SOURCES} ${HEADERS} ${QtApp_RCC_SRCS} ${MOC_HEADERS})

My source and header files are pretty empty at the moment, but I will post them too. Who knows, maybe the error is there.

main.cpp:

#include <iostream>
#include "VigilWidget.h"
//#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    std::cout << "Testing";
    VigilWidget vigil;
    vigil.show();
}

VigilWidget.cpp:

#include "VigilWidget.h"

VigilWidget::VigilWidget(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
}

VigilWidget.h:

#ifndef _BGSUBTRACT_H_
#define _BGSUBTRACT_H_

#include "ui_bugView.h"
//#include <QObject>

class VigilWidget : public QWidget{
 Q_OBJECT

 public:

 private slots:

 signals:

 private slots:

 private:

 Ui::bugView ui;

};
#endif

Anyways, I have spent the best part of two days getting this far and would really really appreciate some help! Thanks a lot!


回答1:


Config for Qt5:

set(QT_VERSION_REQ "5.2")
find_package(Qt5Core ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Quick ${QT_VERSION_REQ} REQUIRED)
find_package(Qt5Widgets ${QT_VERSION_REQ} REQUIRED)
set(CMAKE_AUTOMOC ON)

add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME}
    Qt5::Core
    Qt5::Quick
    Qt5::Widgets     
)

And better use QtQuick instead of QWidgets if you can.



来源:https://stackoverflow.com/questions/36142407/qt-moc-error-using-cmake

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