0

This is my first time asking a question, so please be chill if I do something wrong.

Hello,

  • My Visual Studio solution is generated by CMake.
  • Code is in C++ (and QML).
  • I'm creating a QtQuick window which uses .qml files to generate its content.

When I run the built application from Visual Studio (2017 RC) the app gives a common

"Component not ready" error message.

But if I run the same .exe file manually, everything is working perfectly.

I suspect it must be some kind of a mistake in my CMakeLists causing Visual Studio searching for files in the wrong place but I have no idea what that mistake could be.

C++ code fail part:

...
QQmlComponent guiComponent(qmlEngine,
    QUrl::fromLocalFile(APP_RESOURCES"/qml/GUI.qml"));

guiRoot = qobject_cast<QQuickItem *>(guiComponent.create());
guiRoot->setParentItem(mainWindow->contentItem());
qDebug() << guiComponent.errors();
...

My CMakeLists:

cmake_minimum_required(VERSION 3.8)

set(PROJECT_NAME "[MyProjectName]")
set(APP_NAME "[MyProjectName]")

project(${PROJECT_NAME})

set(CMAKE_AUTORCC ON )
set(CMAKE_AUTOUIC ON )
set(CMAKE_AUTOMOC ON )

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules;${CMAKE_MODULE_PATH}")

find_package(Qt5 REQUIRED Quick QuickWidgets)
find_package(QtImageLoader REQUIRED HINTS ${CMAKE_SOURCE_DIR}/addons/QtImageLoader/cmake)
find_package(AssimpModelLoader REQUIRED HINTS ${CMAKE_SOURCE_DIR}/addons/AssimpModelLoader/cmake)
find_package(GPUEngine REQUIRED geGL geSG)

set(SRC_ROOT "${CMAKE_SOURCE_DIR}/src")
set(SRC_FILES "")
set(INCLUDE_DIRS "")

add_subdirectory(src)
list(APPEND SRC_FILES ${src_SRC_FILES})
list(APPEND INCLUDE_DIRS ${src_INCLUDE_DIRS})

message("Located source files:")
FOREACH(_FILE ${SRC_FILES})
    message("- ${_FILE}")
ENDFOREACH()
message("Located include directories:")
FOREACH(_DIRE ${INCLUDE_DIRS})
    message("- ${_DIRE}")
ENDFOREACH()

source_group(TREE ${SRC_ROOT} PREFIX "src" FILES ${SRC_FILES})

add_executable(${APP_NAME} ${SRC_FILES})

target_include_directories(${APP_NAME} PUBLIC ${INCLUDE_DIRS})

target_link_libraries(${APP_NAME} Qt5::Quick Qt5::QuickWidgets geGL geSG AssimpModelLoader QtImageLoader)

set_target_properties(${APP_NAME} PROPERTIES COMPILE_DEFINITIONS "APP_RESOURCES=\"Data\"")

set_property(TARGET ${APP_NAME} PROPERTY FOLDER "${APP_NAME}")

add_custom_command(TARGET ${APP_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory  "${CMAKE_SOURCE_DIR}/libs/$<CONFIG>"
                                                "$<TARGET_FILE_DIR:[MyProjectName]>"

    COMMAND ${CMAKE_COMMAND} -E copy_directory  "${CMAKE_SOURCE_DIR}/resources"
                                                "$<TARGET_FILE_DIR:[MyProjectName]>/Data"
)

Edit: I had this problem for a long time now (months) and didn't bother to fix it because running the application manually was sufficient. But now I want to use Visual Studio's debugging tools and I can't.

Blacknet
  • 11
  • 5
  • The error is thrown, when your qml file cannot be found. Visual Studio sets its working directory to the internal variable $(ProjectDir) (the folder containing the sln/vcxproj file). Try to set in Visual Studio the Properties->Debugging->Working Directory to the path where your executable is generated. – vre Apr 13 '18 at 13:30
  • Thanks. Works fine as a local fix. I tried to find out how to do this in CMake and altough there were some answers, they either didn't work for my situation or were just too cumbersome to even bother. So I will be just using your suggestion for now. – Blacknet Apr 13 '18 at 13:58
  • Generate the vcxproj.user file. See if [this](https://stackoverflow.com/questions/23950887/does-cmake-offer-a-method-to-set-the-working-directory-for-a-given-build-system) and [this](https://stackoverflow.com/questions/1005901/how-to-set-path-environment-variable-using-cmake-and-visual-studio-to-run-test/1307784#1307784) help you further. – vre Apr 13 '18 at 14:05
  • Another solution is to change your project layout. Place the executables of all configs (Debug, Release, ...) in a common bin folder and place your qml files relative to this folder. Common folders can be created this way: set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) – vre Apr 13 '18 at 14:16
  • Yeah, that's what I saw too. I didn't like the option of writing custom files. I tried setting VS_DEBUGGER_WORKING_DIRECTORY but I fcked up something else and thought it didn't work. I tried again and it's working now. Thanks again for your time. – Blacknet Apr 13 '18 at 14:17

1 Answers1

1

Adding

VS_DEBUGGER_WORKING_DIRECTORY "$(OutDir)"

to my target properties like this

set_target_properties(${APP_NAME} PROPERTIES COMPILE_DEFINITIONS "APP_RESOURCES=\"Data\""
                                             VS_DEBUGGER_WORKING_DIRECTORY "$(OutDir)")

fixed the issue.

For additional solutions, look at vre's comments above.

Blacknet
  • 11
  • 5