Profile Picture

Changing the background color of a widget

Posted By RobertoColombo 2 Months Ago
You don't have permission to rate!
Author
Message
RobertoColombo
RobertoColombo
Posted 2 Months Ago
View Quick Profile
Distinguished Member

Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)

Group: Forum Members
Last Active: 2 Months Ago
Posts: 1.6K, Visits: 3.0K
HI,

I am trying, with no luck, to change teh background color of a widget.
The code apparenly should be easy: for example, here given I create a label and try to set the background to RED, but nothing happens.
Basically I am following the method described here: https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget
I tried also with QPalette.Background for setColor() (this value is anyway considered obsolete in QT doc) but still no change in color.

import PySide2, RLPy
from PySide2 import *
from PySide2.shiboken2 import wrapInstance

#-- Make a global variable contain dialog --#
sample_dialog = None

def run_script():

    global sample_dialog

    sample_dialog = RLPy.RUi.CreateRDialog()
    sample_dialog.SetWindowTitle("Sample Dialog")

    #-- Create Pyside layout for RDialog --#
    pyside_dialog = wrapInstance(int(sample_dialog.GetWindow()), PySide2.QtWidgets.QDialog)
    pyside_dialog.setFixedWidth(200)
    sample_layout = pyside_dialog.layout()
 
    #-- Create the Label --#
    label = PySide2.QtWidgets.QLabel("Test")

    #-- Set the widget color --#
    pal = PySide2.QtGui.QPalette()

    color_red = PySide2.QtGui.QColor('red')
    label.setAutoFillBackground(True)
    pal.setColor(PySide2.QtGui.QPalette.Window, color_red)
    label.setPalette(pal)

    #-- Add Label --#
    sample_layout.addWidget(label)

    sample_dialog.Show()    
    
Any ideas ?

Thanks

  Roberto


My PC:
OS: Windows 10 Pro English 64-bit / CPU: Intel i7-9700 3.6GHz / MB: ASUS ROG Strix Z390  RAM: 32GB DDR4 2.6GHz / HD: 2TB+3TB  /  
SSD: 2x512GB Samsung 860 EVO / 
VB: Palit GTX2080 TI GamingPro 11GB / AB: embedded in the MB and VB (audio from the VB with an HDMI splitter) / DirectX: 12

Edited
2 Months Ago by RobertoColombo
RobertoColombo
RobertoColombo
Posted 2 Months Ago
View Quick Profile
Distinguished Member

Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)Distinguished Member (6.5K reputation)

Group: Forum Members
Last Active: 2 Months Ago
Posts: 1.6K, Visits: 3.0K
Solved!

It works if the style sheet is used rather than a QPalette.
This code makes its job (RGB values are just an example):

import PySide2, RLPy
from PySide2 import *
from PySide2.shiboken2 import wrapInstance

#-- Make a global variable contain dialog --#
sample_dialog = None

def run_script():

    global sample_dialog

    sample_dialog = RLPy.RUi.CreateRDialog()
    sample_dialog.SetWindowTitle("Sample Dialog")

    #-- Create Pyside layout for RDialog --#
    pyside_dialog = wrapInstance(int(sample_dialog.GetWindow()), PySide2.QtWidgets.QDialog)
    pyside_dialog.setFixedWidth(200)
    sample_layout = pyside_dialog.layout()
 
    #-- Create the Label --#
    label = PySide2.QtWidgets.QLabel("Test")
    
    col = PySide2.QtGui.QColor(111, 100, 8)
    label.setStyleSheet("QWidget { background-color: %s }" % col.name()) 

    #-- Add Label --#
    sample_layout.addWidget(label)

    sample_dialog.Show()    



My PC:
OS: Windows 10 Pro English 64-bit / CPU: Intel i7-9700 3.6GHz / MB: ASUS ROG Strix Z390  RAM: 32GB DDR4 2.6GHz / HD: 2TB+3TB  /  
SSD: 2x512GB Samsung 860 EVO / 
VB: Palit GTX2080 TI GamingPro 11GB / AB: embedded in the MB and VB (audio from the VB with an HDMI splitter) / DirectX: 12

SeanMac
SeanMac
Posted 2 Months Ago
View Quick Profile
Distinguished Member

Distinguished Member (1.9K reputation)Distinguished Member (1.9K reputation)Distinguished Member (1.9K reputation)Distinguished Member (1.9K reputation)Distinguished Member (1.9K reputation)Distinguished Member (1.9K reputation)Distinguished Member (1.9K reputation)Distinguished Member (1.9K reputation)Distinguished Member (1.9K reputation)

Group: Forum Members
Last Active: 2 hours ago
Posts: 344, Visits: 2.7K
That is a fast answer to your problem, Roberto.

I hope you will thank Roberto very much for his assistance;-)



Reading This Topic