我有一个简单的Qt5应用程序,我想用CMake制作它。它只有两个文件:.h文件和.cpp文件。这是代码:
.cpp文件
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QVector>
#include "px_enum.h"
#include <QQuickStyle>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QQuickStyle::setStyle("Material");
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
PX_UI::registerEnums();
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
.h文件
#ifndef PX_ENUM_H
#define PX_ENUM_H
#include <QObject>
#include <QQmlEngine>
namespace PX_UI{
Q_NAMESPACE
enum TOP_BAR_BUTTON_TYPE{
TOP_BAR_CLOSE_BTN = 0,
TOP_BAR_MINIMIZE_BTN ,
TOP_BAR_MAXIMIZE_BTN
};
Q_ENUM_NS(TOP_BAR_BUTTON_TYPE)
static void registerEnums()
{
qmlRegisterUncreatableMetaObject( PX_UI::staticMetaObject,
"PX_UI_ENUMS",1, 0,"PX_UI","Error: only enums");
}
}
#endif // PX_ENUM_H
CMakeList.txt
cmake_minimum_required(VERSION 3.1.0)
project(TestApp)
find_package(Qt5Core CONFIG REQUIRED)
find_package(Qt5Quick CONFIG REQUIRED)
find_package(Qt5QuickControls2 CONFIG REQUIRED)
add_definitions( ${QT_DEFINITIONS} )
set(CMAKE_CXX_STANDARD 14)
set ( SOURCES
main.cpp
)
set ( MOC_HEADERS
px_enum.h
)
set ( RESOURCES
qml.qrc
)
add_library(${PROJECT_NAME}
SHARED
px_enum.h
main.cpp
)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_PREFIX_PATH
/home/sajad/Installed_Softs/Qt/5.11.2/gcc_64/lib/cmake)
# Populate a CMake variable with the sources
# Tell CMake to create the helloworld executable
QT5_WRAP_CPP(MOC_SRCS ${MOC_HEADERS})
set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC TRUE)
# Use the Widgets module from Qt 5
target_link_libraries(${PROJECT_NAME} Qt5::Quick)
target_link_libraries(${PROJECT_NAME} Qt5::QuickControls2)
target_link_libraries(${PROJECT_NAME} Qt5::Core)
现在,当我编译代码时出现此错误:
px_enum.h:8:5:错误:“ Q_NAMESPACE”未命名类型
我已经使用qmake成功编译了此代码。 CMakeList.txt文件的哪一部分错误?