如何在 PyQt5 中更改样式 [英] How to change style in PyQt5

查看:154
本文介绍了如何在 PyQt5 中更改样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt 编写 Qt5 应用程序.我想了解如何更改整个应用程序的样式.

I am writing a Qt5 application by using PyQt. I would like to understand how to change the style of the entire application.

旧的 Qt4 调用如下:

The old Qt4 calls like:

app = QApplication(sys.argv)
app.setStyle(QStyleFactory.create('Cleanlooks'))

和建议的方法此处什么都不做.

and the suggested method here does nothing.

它们被弃用了吗?https:///blog.qt.io/blog/2012/10/30/cleaning-up-styles-in-qt5-and-adding-fusion/

谢谢!

推荐答案

可能是 Cleanlooks 在您的系统上不再可用.通过 QStyleFactory.keys() 您可以询问系统上可用的样式.在 Ubuntu 16.04 和 pyqt5 上我只得到:

may be Cleanlooks is no longer available on your system. By QStyleFactory.keys() you can ask the available styles on your system. On Ubuntu 16.04 and pyqt5 i only get:

['Windows', 'GTK+', 'Fusion']

在这里你可以找到 qstyleplugin

包含6种附加样式,需自行编译

containing 6 additional styles, you have to compile it by yourself

在 ubuntu 16.04、python3.5 上,我通过将样式插件安装到 QT5 并针对此 QT5 从源代码编译 pyqt5 使其工作:

on ubuntu 16.04, python3.5 i got it working by installing the styleplugins to QT5 and compile pyqt5 from source against this QT5:

通过在线安装程序安装 QT 5.7

install QT 5.7 by onlineinstaller

在安装目录搜索qmake,在我的例子中/opt/Qt/5.7/gcc_64/bin/qmake

in installationdirectory search qmake, in my case /opt/Qt/5.7/gcc_64/bin/qmake

将qtstyleplugin下载到任意目录git clone https://code.qt.io/qt/qtstyleplugins.git并安装:

download qtstyleplugin to an arbitrary directory git clone https://code.qt.io/qt/qtstyleplugins.git and install it:

cd qtstyleplugins
/opt/Qt/5.7/gcc_64/bin/qmake # the qmake from the fresh installation
make
make install

现在 /opt/Qt/5.7/gcc_64/plugins/ 中有一个文件夹styles",其中包含其他样式.

now there is a folder „styles" in /opt/Qt/5.7/gcc_64/plugins/ containing the additional styles.

下载sip-source,编译安装

下载pyqt5-source,编译安装,步骤python3 configure.py 通过 --qmake-option 从 QT5-Installation 提供 qmake,并在输出中查找缺少的依赖项.

download pyqt5-source, compile and install it, in the step python3 configure.py provide the qmake from the QT5-Installation by the --qmake-option and look in the output for missing dependencies.

现在可以使用以下样式:

Now the following styles are available:

['bb10dark', 'bb10bright', 'cleanlooks', 'cde', 'motif', 'plastique', 'Windows', 'Fusion']

我有一个 sip 错误:

i got an sip-error:

RuntimeError: the sip module implements API v11.0 to v11.2 but the PyQt5.QtCore module requires API v11.3

为了防止它,在安装 sip 之前运行 sudo apt-get purge python3-sip 如此处所述

to prevent it, run sudo apt-get purge python3-sip before installing sip as described here

这篇关于如何在 PyQt5 中更改样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆