Clion 创建的Qt程序界面是老式风格

环境:

  • Clion版本:CLion 2023.1.1
  • Qt 5.15.2

问题

当使用Clion创建Qt程序,不管是选择MinGW编译器还是MSVC编译器,显示出来的界面都很old style

即老式的界面风格,如图所示:
Clion 创建的Qt程序界面是老式风格_第1张图片

原因

产生这个问题的原因是缺少qwindowsvistastyle.dll动态库,因为Clion生成的CMakeLists.txt文件只拷贝了必要的动态库文件,它并没有将qwindowsvistastyle.dll文件拷贝过来。

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
                "$/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$/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"
                "$")
    endforeach (QT_LIB)
endif ()

解决方案

根据选择的编译器,去对应Qt安装路径下将styles文件夹复制到可执行程序路径下

MinGW

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

MSVC

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

再次运行,即可发现,界面风格正常了。这应该算Clion的小瑕疵。

效果如图:
Clion 创建的Qt程序界面是老式风格_第2张图片

当然,我们还可以对CMakeLists.txt进行修改,也是最推荐的做法。添加以下语句即可

    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
            "$/plugins/styles/")
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy
            "${QT_INSTALL_PATH}/plugins/styles/qwindowsvistastyle${DEBUG_SUFFIX}.dll"
            "$/plugins/styles/")
    endif ()

注意:由于Clion、Qt版本有差异,会导致路径不同,自行修改。

Qt GUI theme looks old-fashioned

你可能感兴趣的:(qt,开发语言,c++)