Raspberry 上的 PyQt5:不显示 QTWidgets 的背景颜色

Posted

技术标签:

【中文标题】Raspberry 上的 PyQt5:不显示 QTWidgets 的背景颜色【英文标题】:PyQt5 on Raspbian: Background color of QWidgets is not displayed 【发布时间】:2019-08-24 11:29:25 【问题描述】:

我已经在运行 Raspbian Buster 的 Raspberry Pi 3B+ 上编译并安装了 PyQt5,如 here 所述。花了几个小时,但它奏效了。之后我上传了我的简单 PyQt 示例程序,其中包含 2 个文件(main.pymainwindow.ui)。

QProgressBarQPushButton 设置的红色背景颜色在我的运行Ubuntu 18.04 LTS 的PC 上显示为不同深浅的红色(?),但在Raspberry Pi 上根本不显示。

Raspbian Buster 上的终端输出:

libEGL warning: DRI2: failed to authenticate
qt5ct: using qt5ct plugin
qt5ct: D-Bus global menu: no

Ubuntu 18.04 LTS 上的屏幕截图:

Raspbian Buster 的屏幕截图:

为什么 Raspbian Buster 上不显示红色背景色?

ma​​in.py

import sys
from pathlib import Path
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QColor
from PyQt5.uic import loadUi


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        ui_file = (Path(__file__).parent / "mainwindow.ui").resolve()
        loadUi(ui_file, self)
        self.progressBar.setStyleSheet(f"background-color: QColor('red').name()")


def main():
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

ma​​inwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>200</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="locale">
   <locale language="English" country="UnitedKingdom"/>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <layout class="QGridLayout" name="gridLayout">
      <item row="0" column="0">
       <widget class="QProgressBar" name="progressBar">
        <property name="value">
         <number>0</number>
        </property>
       </widget>
      </item>
      <item row="0" column="1">
       <widget class="QPushButton" name="pushButton">
        <property name="minimumSize">
         <size>
          <width>200</width>
          <height>0</height>
         </size>
        </property>
        <property name="styleSheet">
         <string notr="true">background-color: red;</string>
        </property>
        <property name="text">
         <string>Button</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>28</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

设置export QT_DEBUG_PLUGINS=1后Raspbian Buster的终端输出:

QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms" ...
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqeglfs.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqeglfs.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": 
        "Keys": [
            "eglfs"
        ]
    ,
    "className": "QEglFSIntegrationPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("eglfs")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqlinuxfb.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqlinuxfb.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": 
        "Keys": [
            "linuxfb"
        ]
    ,
    "className": "QLinuxFbIntegrationPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("linuxfb")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqminimal.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqminimal.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": 
        "Keys": [
            "minimal"
        ]
    ,
    "className": "QMinimalIntegrationPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("minimal")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqminimalegl.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqminimalegl.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": 
        "Keys": [
            "minimalegl"
        ]
    ,
    "className": "QMinimalEglIntegrationPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("minimalegl")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqoffscreen.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqoffscreen.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": 
        "Keys": [
            "offscreen"
        ]
    ,
    "className": "QOffscreenIntegrationPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("offscreen")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqvnc.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqvnc.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": 
        "Keys": [
            "vnc"
        ]
    ,
    "className": "QVncIntegrationPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("vnc")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqxcb.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqxcb.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": 
        "Keys": [
            "xcb"
        ]
    ,
    "className": "QXcbIntegrationPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("xcb")
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/platforms" ...
loaded library "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforms/libqxcb.so"
loaded library "Xcursor"
QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/arm-linux-gnueabihf/qt5/plugins/xcbglintegrations" ...
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/xcbglintegrations/libqxcb-egl-integration.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/xcbglintegrations/libqxcb-egl-integration.so, metadata=

    "IID": "org.qt-project.Qt.QPA.Xcb.QXcbGlIntegrationFactoryInterface.5.5",
    "MetaData": 
        "Keys": [
            "xcb_egl"
        ]
    ,
    "className": "QXcbEglIntegrationPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("xcb_egl")
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/xcbglintegrations" ...
loaded library "/usr/lib/arm-linux-gnueabihf/qt5/plugins/xcbglintegrations/libqxcb-egl-integration.so"
libEGL warning: DRI2: failed to authenticate
QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platformthemes" ...
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platformthemes/libqgtk2.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platformthemes/libqgtk2.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformThemeFactoryInterface.5.1",
    "MetaData": 
        "Keys": [
            "gtk2"
        ]
    ,
    "className": "QGtk2ThemePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("gtk2")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platformthemes/libqgtk3.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platformthemes/libqgtk3.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformThemeFactoryInterface.5.1",
    "MetaData": 
        "Keys": [
            "gtk3"
        ]
    ,
    "className": "QGtk3ThemePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("gtk3")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platformthemes/libqt5ct.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platformthemes/libqt5ct.so, metadata=

    "IID": "org.qt-project.Qt.QPA.QPlatformThemeFactoryInterface.5.1",
    "MetaData": 
        "Keys": [
            "qt5ct"
        ]
    ,
    "className": "Qt5CTPlatformThemePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("qt5ct")
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/platformthemes" ...
loaded library "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platformthemes/libqt5ct.so"
qt5ct: using qt5ct plugin
QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles" ...
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libbb10styleplugin.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libbb10styleplugin.so, metadata=

    "IID": "org.qt-project.Qt.QStyleFactoryInterface",
    "MetaData": 
        "Keys": [
            "bb10bright",
            "bb10dark"
        ]
    ,
    "className": "QBB10StylePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("bb10bright", "bb10dark")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqcleanlooksstyle.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqcleanlooksstyle.so, metadata=

    "IID": "org.qt-project.Qt.QStyleFactoryInterface",
    "MetaData": 
        "Keys": [
            "cleanlooks"
        ]
    ,
    "className": "QCleanlooksStylePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("cleanlooks")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqgtk2style.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqgtk2style.so, metadata=

    "IID": "org.qt-project.Qt.QStyleFactoryInterface",
    "MetaData": 
        "Keys": [
            "gtk2"
        ]
    ,
    "className": "QGtk2StylePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("gtk2")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqmotifstyle.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqmotifstyle.so, metadata=

    "IID": "org.qt-project.Qt.QStyleFactoryInterface",
    "MetaData": 
        "Keys": [
            "motif",
            "cde"
        ]
    ,
    "className": "QMotifStylePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("motif", "cde")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqplastiquestyle.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqplastiquestyle.so, metadata=

    "IID": "org.qt-project.Qt.QStyleFactoryInterface",
    "MetaData": 
        "Keys": [
            "plastique"
        ]
    ,
    "className": "QPlastiqueStylePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("plastique")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqt5ct-style.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqt5ct-style.so, metadata=

    "IID": "org.qt-project.Qt.QStyleFactoryInterface",
    "MetaData": 
        "Keys": [
            "qt5ct-style"
        ]
    ,
    "className": "Qt5CTStylePlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("qt5ct-style")
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/styles" ...
QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforminputcontexts" ...
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforminputcontexts/libcomposeplatforminputcontextplugin.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforminputcontexts/libcomposeplatforminputcontextplugin.so, metadata=

    "IID": "org.qt-project.Qt.QPlatformInputContextFactoryInterface.5.1",
    "MetaData": 
        "Keys": [
            "compose",
            "xim"
        ]
    ,
    "className": "QComposePlatformInputContextPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("compose", "xim")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforminputcontexts/libibusplatforminputcontextplugin.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/platforminputcontexts/libibusplatforminputcontextplugin.so, metadata=

    "IID": "org.qt-project.Qt.QPlatformInputContextFactoryInterface.5.1",
    "MetaData": 
        "Keys": [
            "ibus"
        ]
    ,
    "className": "QIbusPlatformInputContextPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("ibus")
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/platforminputcontexts" ...
loaded library "/usr/lib/arm-linux-gnueabihf/qt5/plugins/platforminputcontexts/libcomposeplatforminputcontextplugin.so"
loaded library "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqt5ct-style.so"
loaded library "/usr/lib/arm-linux-gnueabihf/qt5/plugins/styles/libqgtk2style.so"
Cannot load library gtk-x11-2.0: (gtk-x11-2.0: Kann die Shared-Object-Datei nicht öffnen: Datei oder Verzeichnis nicht gefunden)
qt5ct: D-Bus global menu: no
QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/arm-linux-gnueabihf/qt5/plugins/iconengines" ...
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/arm-linux-gnueabihf/qt5/plugins/iconengines/libqsvgicon.so"
Found metadata in lib /usr/lib/arm-linux-gnueabihf/qt5/plugins/iconengines/libqsvgicon.so, metadata=

    "IID": "org.qt-project.Qt.QIconEngineFactoryInterface",
    "MetaData": 
        "Keys": [
            "svg",
            "svgz",
            "svg.gz"
        ]
    ,
    "className": "QSvgIconPlugin",
    "debug": false,
    "version": 330499



Got keys from plugin meta data ("svg", "svgz", "svg.gz")
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/iconengines" ...
QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/arm-linux-gnueabihf/qt5/plugins/accessible" ...
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/accessible" ...
QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/arm-linux-gnueabihf/qt5/plugins/accessiblebridge" ...
QFactoryLoader::QFactoryLoader() checking directory path "/usr/bin/accessiblebridge" ...

【问题讨论】:

当您从终端运行文件时,您会收到错误消息吗?在您的 raspbian 和 ubuntu 中,print(app.style().metaObject().className()) 的输出是什么? 在 Ubuntu 上没有终端输出,但在 Raspbian 上显示以下内容。 ``` libEGL 警告:DRI2:无法验证 qt5ct:使用 qt5ct 插件 qt5ct:D-Bus 全局菜单:否 ```Python 命令在 Ubuntu 和 Raspbian 上都不会导致输出。 export QT_DEBUG_PLUGINS=1python your_script.py的输出是什么 @eyllanesc 设置export QT_DEBUG_PLUGINS=1后有很多终端输出(见上)。 【参考方案1】:

我今天一直在与类似的问题作斗争(这就是我找到您的帖子的方式)。要摆脱 libEGL 警告,请运行 raspi-config 并从 Advanced Options->GL Driver 中选择“Full KMS”选项(然后重新启动)。不过,这可能无法解决您的样式问题 - 我看到 Pi(我的目标系统)和我的 Linux Mint 开发系统之间的样式差异。

【讨论】:

【参考方案2】:

我也有类似的问题,按照下面的方式运行就可以解决了。

sudo python3.7 name.py

我想 IDE 或当前登录的用户需要一些权限。 虽然颜色仍然比 Windows 和 Ubuntu 更不透明。

【讨论】:

以上是关于Raspberry 上的 PyQt5:不显示 QTWidgets 的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

gnome 上的 PyQt5 有奇怪的文件对话框

如何在 Raspberry Pi3 上的 Qt5 上使用 qt-gstreamer 和 qtquick2videosink

pyqt5 Qt Designer + matpoltlib画图显示动态曲线

在树莓派中安装 QtVirtualkeyboard?

无法在 Qt 设计器(macos)中显示自定义 PyQt5 小部件插件

PyQt5 之QToolButton工具按钮