Jump to content

PySide2 override CSS


asnowcappedromance

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
Link to comment
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)

 

Link to comment
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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...