问题
When I cross-compile Qt 5.8.0 from source myself and use it to build the Qt "Dynamic Layouts" example from Qt Widgets for Microsoft Windows, it looks old-fashioned, as if it were running on Windows 2000, as shown in the left window below. However, if I compile the exact same program using the pre-built mingw-w64-x86_64-qt5-static package (version 5.8.0-1) from MSYS2, it looks like a nice modern Windows application, as shown in the right window below. How can I fix my version of Qt so that GUIs built with it will look nice? Is there a configuration option I am missing?
One thing to look into is the QWindowsVistaStyle
class. I noticed that when I compiled my program in MSYS2, I had to add -luxtheme
because that class referred to some functions from there, like GetThemeColor
. Those functions are only available in Windows Vista and later. When I cross-compiled using my own Qt on Linux, I didn't need that library, so perhaps that class was somehow disabled in my build.
Cross-compiled Qt build details
I built the cross-compiled Qt and the old-looking Dynamic Layouts example using commit f51d834 of my nixcrpkgs repository. If you look at the code in that commit, it will show you exactly what commands were run to build the mingw-w64 cross-compiler, and build Qt, and build the Qt examples, including Dynamic Layouts. You can build it yourself if you install Nix, download nixcrkpkgs, and then run nix-build -A pkgs.i686-w64-mingw32.qt.base-examples
in the nixcrpkgs directory. You can see my build recipe for the Qt package in that commit.
MSYS2 build details
I ran this script in a MINGW64 shell in MSYS2 to build the example and got good results. It relies on the mingw-w64-x86_64-qt5-static package from MSYS2. You can see the build recipe for that package.
回答1:
I had the same issue after upgrading from Qt5.9.1 to Qt5.12.
Placing qwindowsvistastyle.dll, which I found to be in Qt\5.12.0\msvc2017_64\plugins\styles, needed to be placed in my applicationDir\styles directory.
回答2:
Widgets
Qt-based GUIs will automatically select the default theme depending on your OS. If you want to override it, you are looking for QApplication's setStyle method, which lets you define the style used for your application, regardless of the OS it runs on.
QApplication::setStyle("fusion");
From the documentation, the following are supported:
- "windows"
- "fusion"
- "windowsxp"
- "macintosh"
The supported themes depend on your platform, and can be extended with plugins.
EDIT: This answer targets situations where the OS is correctly detected by Qt configuration step, and you just want to change the theme used. In the situation described, the incorrect theme was selected, which is fixed in OP's answer.
Qt Quick
If your GUI is based on QtQuick instead of Widgets, you can use QQuickStyle::setStyle("Material");
instead to customize the theme that you want to see used.
For more details, you can look at that documentation page:
- https://doc.qt.io/qt-5/qtquickcontrols2-styles.html#using-styles-in-qt-quick-controls-2
回答3:
If you look in the src/widgets/configure.json
file from Qt 5.8.0, you can see that it checks for the existence of uxtheme.h
, and that uxtheme.h
is a precondition for compiling the windowsxp
style, which is a precondition for compiling the windowsvista
style. By looking in Qt's config.log
, I saw that the uxtheme.h
test failed. I am not sure why, but it's probably because uxtheme.h
cannot be included on its own; you need to include windows.h
beforehand. I verified that the windowsxp
and windowsvista
styles were indeed not enabled by looking in the src/widgets/qtwidgets-config.pri
file after configuring Qt. It has a list of styles that are going to get compiled, and windowsvista
is not the list.
I tried adding the -style-windowsxp -style-windowsvista
options to Qt's configure command, but those options just cause errors because the uxtheme.h
test is failing and it is a prerequisite for compiling those themes.
My solution was to apply this patch to Qt 5.8.0 to skip the uxtheme
test altogether:
diff -ur qt58-orig/src/widgets/configure.json qt58/src/widgets/configure.json
--- qt58-orig/src/widgets/configure.json 2017-05-28 02:07:07.625626151 -0700
+++ qt58/src/widgets/configure.json 2017-06-27 21:25:52.752628339 -0700
@@ -28,11 +28,6 @@
},
"tests": {
- "uxtheme": {
- "label": "uxtheme.h",
- "type": "files",
- "files": [ "uxtheme.h" ]
- }
},
"features": {
@@ -57,7 +52,7 @@
},
"style-windowsxp": {
"label": "WindowsXP",
- "condition": "features.style-windows && config.win32 && !config.winrt && tests.uxtheme",
+ "condition": "features.style-windows && config.win32 && !config.winrt",
"output": [ "styles" ]
},
"style-windowsvista": {
I'm not sure why the MSYS2 package worked well, since I don't see any patch like this in their build script.
回答4:
I had this same issue when upgrading from a static Qt 5.6.3 to static 5.12.0. I solved it after I tried to manually set the "windowsvista" style with QApplication::setStyle() and noticed that it returned null when debugging. It turns out in 5.12.0 the windowsvista style is its own separate plugin and you need to link against plugins/styles/qwindowsvistastyle.lib and add Q_IMPORT_PLUGIN(QWindowsVistaStylePlugin) to the cpp file containing your main function. After that there is no need to manually set the style, it is chosen automatically.
来源:https://stackoverflow.com/questions/44784414/qt-gui-theme-looks-old-fashioned