我有一个非常简单的正确的 Qt QML项目,用Qt 5.9.1和qmake编写并运行良好。但我想使用CLion来编辑c ++代码,所以我创建了CMake项目配置,如Internet中的许多教程所述。构建正常,但是当应用程序启动时会产生非常奇怪的QQmlApplicationEngine错误,就像我的qml写得完全错误一样:
QQmlApplicationEngine failed to load component
main.qml:1 Expected token `numeric literal'
main.qml:1 Expected a qualified name id
以下是我在MacOS X下的配置:(注意:我不故意使用QRC!)
的CMakeLists.txt :
cmake_minimum_required(VERSION 3.8)
project(simple_project)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5 5.9.1 REQUIRED Core Widgets Gui Qml Quick)
set(SOURCE_FILES
main.cpp
main.qml)
add_executable(simple_project ${SOURCE_FILES})
target_link_libraries(simple_project
Qt5::Core
Qt5::Widgets
Qt5::Gui
Qt5::Qml
Qt5::Quick)
的main.cpp :
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load("main.qml");
return app.exec();
}
main.qml 位于可执行文件所在的目录中:
import QtQuick.Controls 2.1
import QtQuick 2.7
import QtQuick.Layouts 1.2
ApplicationWindow {
visible: true
width: 480
height: 320
}