Jump to content
Sign in to follow this  
asnowcappedromance

PySide2 override CSS

Recommended Posts

Hi guys,

I'm having a simple shelf tool in Houdini:

from PySide2 import QtCore, QtGui, QtWidgets

class PysideTest(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(PysideTest, self).__init__(parent)
        
        test = QtWidgets.QWidget()
        self.resize(800, 800)
        self.setWindowTitle("This is a test")
        
        hbox = QtWidgets.QHBoxLayout()
        edit = QtWidgets.QLineEdit()
        edit.setObjectName("QLineEditCustom")
        print edit.styleSheet()
        hbox.addWidget(edit)
      
        test.setLayout(hbox)
                
        self.setCentralWidget(test)
        #self.setBackgroundRole(QtGui.QPalette.ColorRole.Base)
        self.setBackgroundRole(QtGui.QPalette.Dark)
        self.setAutoFillBackground(True)
        self.show()
                
def run():
    mainWidget = hou.qt.mainWindow()
    css = mainWidget.styleSheet()
    css_filter_str = 'QLineEdit#QLineEditCustom\n{background: rgb(255, 255, 255); color: rgb(255, 0, 255); selection-background-color: red}'
    css = "\n".join([css, css_filter_str])
    mainWidget.setStyleSheet(css)
    obj = PysideTest(mainWidget)
    
run()

When I press on the shelf tool button I get my UI popping up nicely. This only works if I give the class "PysideTest" the parent "mainWidget", otherwise the UI disappears immediately after clicking on the shelf tool.
Now because I'm giving it the "hou.qt.mainWindow()" as a parent, the UI inherits the CSS from its parent. In order to override the CSS on the QLineEdit for instance, I can't just simply write edit.setStyleSheet(), this is being ignored. Instead, I apparently have to grab the CSS from the mainWidget (which is a string) and append my own line using the identifier "#QLineEditCustom".
This way it's possible for me to adjust the text color (in my case pink) as well as the selection background color (in my case red), however I'm unable to change the background color of the QLineEdit, which is what I actually want to do.

> background: rgb(255, 255, 255) does not work
> background-color: rgb(255, 255, 255) does not work either

What am I missing here guys? I really like the fact that I can just inherit the CSS from Houdini, but it would be great if there was a more straight forward way of customizing it.
The other question I have, what if I wanted to change the background color based on the user text input from color A to color B dynamically, how would I approach that?


Thanks in advance,
Manu

 

Edited by asnowcappedromance

Share this post


Link to post
Share on other sites

you can set color or MainWindow right after you create it. 

self.setStyleSheet("QMainWindow {background: 'green';}")

Not sure how to change the color of background dynamically, but you can use another widget and connect it's signal to update the color. Cheers!

Share this post


Link to post
Share on other sites

Hi Kishen,

Thanks for your reply. In my case I want to change the background color of the QLineEdit, not the QMainWindow. Any idea how to do that?

Cheers!

 

Share this post


Link to post
Share on other sites

Create your widget first, and then set the stylesheet on your widget and not the main window.

 obj = PysideTest(hou.qt.mainWindow())
 css_filter_str = 'QLineEdit#QLineEditCustom\n{background: rgb(255, 255, 255); color: rgb(255, 0, 255); selection-background-color: red}'
 obj.setStyleSheet(css)

 

Share this post


Link to post
Share on other sites

Hi Alex,

Cool to see that you're in Vancouver! Our office is just a few blocks from Sony!
You were right, I can directly set a styleSheet on my PysideTest class, but like in the above example the background color gets simply ignored for some reason.
In the below code my text appears in green but the background remains black instead of red.

Here's the current code:
 

from PySide2 import QtCore, QtGui, QtWidgets

class PysideTest(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(PysideTest, self).__init__(parent)
        
        test = QtWidgets.QWidget()
        self.resize(800, 800)
        self.setWindowTitle("This is a test")
        
        hbox = QtWidgets.QHBoxLayout()
        edit = QtWidgets.QLineEdit()
        edit.setObjectName("QLineEditCustom")
        hbox.addWidget(edit)
      
        test.setLayout(hbox)      
        self.setCentralWidget(test)        
        self.show()
        
                
def run():
    mainWidget = hou.qt.mainWindow()
    obj = PysideTest(mainWidget)
    obj.setStyleSheet("QLineEdit#QLineEditCustom {background-color: 'red'; color: 'green';}")
    
run()

Thanks for your help guys!

Best,

Manu

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×