我用 Qt 5.9.1 和 qmake 编写并构建了非常简单的正确Qt QML 项目,运行良好。但是我想用 CLion 来编辑 c++ 代码,所以我创建了 CMake 项目配置,如互联网上许多教程中所述。构建进展顺利,但是当应用程序启动时,它会产生非常奇怪的 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)
主.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
}