Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. CMake + vscode
Forum Updated to NodeBB v4.3 + New Features

CMake + vscode

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 3.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hyderman
    wrote on last edited by
    #1

    Hi,

    I'm trying to understand how to build a qt app on vscode.
    I have a CMakeLists.txt which work but it is complex and I have the Qt Cmake example which build but no widow appears when i run it.

    The CMakeLists.txt which works:

    cmake_minimum_required(VERSION 3.22)
    project(qt-cmake)
    
    set(CMAKE_CXX_STANDARD 14)
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_AUTOUIC ON)
    
    set(CMAKE_PREFIX_PATH "C:/Tools/Qt/6.3.1/mingw_64")
    
    find_package(Qt6 COMPONENTS
            Widgets
            REQUIRED)
    
    add_executable(qt-cmake main.cpp)
    target_link_libraries(qt-cmake
            Qt::Widgets
            )
    
    if (WIN32)
        set(DEBUG_SUFFIX)
        if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
            set(DEBUG_SUFFIX "d")
        endif ()
        set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
            if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
                set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
            endif ()
        endif ()
        if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E make_directory
                    "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy
                    "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                    "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        endif ()
        foreach (QT_LIB Core Gui Widgets)
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy
                    "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                    "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
        endforeach (QT_LIB)
    endif ()
    

    The CMakeLists.txt that i would like to make works:

    cmake_minimum_required(VERSION 3.16)
    
    project(helloworld VERSION 1.0.0 LANGUAGES CXX)
    set(CMAKE_PREFIX_PATH "C:/Tools/Qt/6.3.1/mingw_64")
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 REQUIRED COMPONENTS Widgets)
    qt_standard_project_setup()
    
    add_executable(helloworld
        main.cpp
    )
    
    target_link_libraries(helloworld PRIVATE Qt6::Widgets)
    
    set_target_properties(helloworld PROPERTIES
        WIN32_EXECUTABLE ON
        MACOSX_BUNDLE ON
    )
    

    My setup:
    Windows, mingw, Qt6

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Might be a silly question but do you show any widget in your application ?

      If so, can you share your main.cpp file ?

      Does the application start successfully ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hyderman
        wrote on last edited by
        #3

        My main.cpp

        #include <QApplication>
        #include <QPushButton>
        
        int main(int argc, char *argv[]) {
            QApplication a(argc, argv);
            QPushButton button("Hello world!", nullptr);
            button.resize(200, 100);
            button.show();
            return QApplication::exec();
        }
        

        Both CMakeLists build the project but the second one do nothing when i run the build.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          That looks perfectly fine.

          How do you start your application ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hyderman
            wrote on last edited by
            #5

            I use the .exe in my build folder.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              And you have no error ? Just the application that is hanging ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mchinand
                wrote on last edited by mchinand
                #7

                Your 'working' CMake project is copying the Qt dlls and plugin folders to where your .exe is created. You could copy those post-build steps to your other CMakeLists.txt file; the entire if (WIN32) block at the end of the CMakeLists.txt.

                1 Reply Last reply
                1
                • H Offline
                  H Offline
                  Hyderman
                  wrote on last edited by
                  #8

                  It's not hanging it just runs without error.
                  Why i have to copy all that and can't use the CMake of the tutorial ?

                  M 1 Reply Last reply
                  0
                  • H Hyderman

                    It's not hanging it just runs without error.
                    Why i have to copy all that and can't use the CMake of the tutorial ?

                    M Offline
                    M Offline
                    mchinand
                    wrote on last edited by
                    #9

                    Alternatively, you can add the Qt bin folder to your PATH.

                    1 Reply Last reply
                    1
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      If you take Qt Creator, it will adapt the PATH environment variable so that it will contain the path for the Qt version you are using to build your application. If your current IDE does not provide that automation, then your application will need to have all its dependencies copied beside it. That is nothing Qt specific though, any dependency you have must be findable by your executable,

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • H Offline
                        H Offline
                        Hyderman
                        wrote on last edited by
                        #11

                        Thanks guys it works perfectly now.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved