环境:
- Clion版本:CLion 2023.1.1
- Qt 5.15.2
问题
当使用Clion创建Qt程序,不管是选择MinGW编译器还是MSVC编译器,显示出来的界面都很old style
即老式的界面风格,如图所示:
原因
产生这个问题的原因是缺少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
"$<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 ()
解决方案
根据选择的编译器,去对应Qt安装路径下将styles
文件夹复制到可执行程序路径下
MinGW
D:\Qt\5.15.2\mingw81_64\plugins\styles
MSVC
D:\Qt\5.15.2\msvc2019_64\plugins\styles
再次运行,即可发现,界面风格正常了。这应该算Clion的小瑕疵。
效果如图:
当然,我们还可以对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
"$<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 ()
注意:由于Clion、Qt版本有差异,会导致路径不同,自行修改。
Qt GUI theme looks old-fashioned