The Qt program interface created by Clion is old style

environment:

  • Clion version: CLion 2023.1.1
  • Qt 5.15.2

question

When using Clion to create a Qt program, whether you choose the MinGW compiler or the MSVC compiler, the displayed interface is very old style

That is, the old-fashioned interface style, as shown in the figure:
insert image description here

reason

The reason for this problem is the lack of qwindowsvistastyle.dlldynamic libraries, because the CMakeLists.txt file generated by Clion only copies the necessary dynamic library files, it does not copy qwindowsvistastyle.dllthe files.

if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    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/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()

solution

According to the selected compiler, go to the corresponding Qt installation path and stylescopy the folder to the executable program path

MinGW

D:\Qt\5.15.2\mingw81_64\plugins\styles

MSVC

D:\Qt\5.15.2\msvc2019_64\plugins\styles

Run it again, you can find that the interface style is normal. This should be considered a small flaw of Clion.

The effect is as shown in the figure:
insert image description here

Of course, we can also modify CMakeLists.txt, which is also the most recommended approach. Just add the following statement

    if (EXISTS "${QT_INSTALL_PATH}/plugins/styles/qwindowsvistastyle${DEBUG_SUFFIX}.dll")
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E make_directory
            "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/styles/")
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
            "${QT_INSTALL_PATH}/plugins/styles/qwindowsvistastyle${DEBUG_SUFFIX}.dll"
            "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/styles/")
    endif ()

Note: Due to the difference between Clion and Qt versions, the path will be different, so modify it yourself.

Qt GUI theme looks old-fashioned

Guess you like

Origin blog.csdn.net/no_say_you_know/article/details/130228735