MudCatcher
Posts: 4
Joined: Sun Sep 01, 2019 3:25 pm

PyQt5 on Raspbian: Background color of QWidgets is not displayed

Sun Sep 01, 2019 3:35 pm

I've compiled and installed PyQt5 on a Raspberry Pi 3B+ running Raspbian Buster like described here. It took a few hours, but it worked. Afterwards I uploaded my simple PyQt example program consisting of 2 files (main.py and mainwindow.ui).

The red background color set for QProgressBar and QPushButton are displayed in different shades of red (?) on my PC running Ubuntu 18.04 LTS, but on the Raspberry Pi it is not displayed at all. Why is the red background color not displayed on Raspbian Buster?

Terminal output on Raspbian Buster:

Code: Select all

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

There is no Terminal output on Ubuntu 18.04 LTS.


Screenshot on Raspbian Buster:
Image

Screenshot on Ubuntu 18.04 LTS:
Image


main.py

Code: Select all

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()

mainwindow.ui

Code: Select all

<?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>

User avatar
MrYsLab
Posts: 375
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: PyQt5 on Raspbian: Background color of QWidgets is not displayed

Mon Sep 02, 2019 2:35 pm

I am not sure if this will solve your issue or not, but take a look here: https://raspberrypi.stackexchange.com/ ... -to-libegl

MudCatcher
Posts: 4
Joined: Sun Sep 01, 2019 3:25 pm

Re: PyQt5 on Raspbian: Background color of QWidgets is not displayed

Mon Sep 02, 2019 6:47 pm

Thank you for the link, but the steps described there did not solve the problem.

Paul Hutch
Posts: 390
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: PyQt5 on Raspbian: Background color of QWidgets is not displayed

Wed Sep 04, 2019 2:19 pm

MudCatcher wrote:
Sun Sep 01, 2019 3:35 pm
I've compiled and installed PyQt5 on a Raspberry Pi 3B+ running Raspbian Buster like described here. It took a few hours, but it worked.
That StackExchange post is 2 years old so is likely obsolete in some way (I consider 6 months to be the shelf life of those type of posts).

I haven't used Buster yet, but on Stretch & Jessie PyQT5 is available as an installable package and I've been using it with no issues for well over a year now for cross platform GUI apps. See if Buster has it available and if it does uninstall your custom compiled version and install the packaged version.

MudCatcher
Posts: 4
Joined: Sun Sep 01, 2019 3:25 pm

Re: PyQt5 on Raspbian: Background color of QWidgets is not displayed

Wed Sep 04, 2019 7:12 pm

On my Raspbian Buster there is no installable PyQT5 package neither in the repositories nor in the Python Package Index.

jayben
Posts: 6
Joined: Mon Aug 19, 2019 9:56 pm

Re: PyQt5 on Raspbian: Background color of QWidgets is not displayed

Wed Sep 04, 2019 7:38 pm

sudo apt install python3-pyqt5

That works for me on Buster, no compilation required.

MudCatcher
Posts: 4
Joined: Sun Sep 01, 2019 3:25 pm

Re: PyQt5 on Raspbian: Background color of QWidgets is not displayed

Thu Sep 05, 2019 1:50 pm

The python3-pyqt5 package was found, but even after installation the background color issue remains.

Paul Hutch
Posts: 390
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: PyQt5 on Raspbian: Background color of QWidgets is not displayed

Fri Sep 06, 2019 10:23 am

OK we can now be pretty sure it's not dependancy problem.

I did a Google search and find that for many years this has been a common problem for users with QT (many QT forum posts). This thread seems to point to the root of the issue: https://forum.qt.io/topic/93418/cant-ch ... gressbar/8

From the final post in that thread:
The progressbar color is typically controlled by the window manager. So changing it means Qt no longer uses it's regular drawing routines and instead using the CSS ones. You're pretty much stuck with a custom widget or a full new styling.
Looks like it's going to take a lot of extra work to get the simple effect you want, hopefully I'm wrong.

Return to “Python”